Refactor code structure for improved readability and maintainability
Test Suite / test (24.15.0) (push) Has been cancelled
Test Suite / test (24.15.0) (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
export declare class CreateMealPlanEntryDto {
|
||||
date: string;
|
||||
recipeId: number;
|
||||
servings?: number;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
"use strict";
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
var __metadata = (this && this.__metadata) || function (k, v) {
|
||||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.CreateMealPlanEntryDto = void 0;
|
||||
const class_validator_1 = require("class-validator");
|
||||
class CreateMealPlanEntryDto {
|
||||
}
|
||||
exports.CreateMealPlanEntryDto = CreateMealPlanEntryDto;
|
||||
__decorate([
|
||||
(0, class_validator_1.IsDateString)(),
|
||||
__metadata("design:type", String)
|
||||
], CreateMealPlanEntryDto.prototype, "date", void 0);
|
||||
__decorate([
|
||||
(0, class_validator_1.IsInt)(),
|
||||
(0, class_validator_1.IsPositive)(),
|
||||
__metadata("design:type", Number)
|
||||
], CreateMealPlanEntryDto.prototype, "recipeId", void 0);
|
||||
__decorate([
|
||||
(0, class_validator_1.IsOptional)(),
|
||||
(0, class_validator_1.IsInt)(),
|
||||
(0, class_validator_1.Min)(1),
|
||||
__metadata("design:type", Number)
|
||||
], CreateMealPlanEntryDto.prototype, "servings", void 0);
|
||||
//# sourceMappingURL=create-meal-plan-entry.dto.js.map
|
||||
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"create-meal-plan-entry.dto.js","sourceRoot":"","sources":["../../../src/meal-plan/dto/create-meal-plan-entry.dto.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAAmF;AAEnF,MAAa,sBAAsB;CAYlC;AAZD,wDAYC;AAVC;IADC,IAAA,8BAAY,GAAE;;oDACF;AAIb;IAFC,IAAA,uBAAK,GAAE;IACP,IAAA,4BAAU,GAAE;;wDACI;AAKjB;IAHC,IAAA,4BAAU,GAAE;IACZ,IAAA,uBAAK,GAAE;IACP,IAAA,qBAAG,EAAC,CAAC,CAAC;;wDACW"}
|
||||
@@ -0,0 +1,92 @@
|
||||
import { MealPlanService } from './meal-plan.service';
|
||||
import { CreateMealPlanEntryDto } from './dto/create-meal-plan-entry.dto';
|
||||
export declare class MealPlanController {
|
||||
private readonly mealPlanService;
|
||||
constructor(mealPlanService: MealPlanService);
|
||||
findByRange(user: {
|
||||
userId: number;
|
||||
}, from: string, to: string): Promise<({
|
||||
recipe: {
|
||||
name: string;
|
||||
id: number;
|
||||
imageUrl: string | null;
|
||||
servings: number | null;
|
||||
ingredients: {
|
||||
product: {
|
||||
name: string;
|
||||
canonicalName: string | null;
|
||||
id: number;
|
||||
} | null;
|
||||
quantity: import("@prisma/client/runtime/library").Decimal | null;
|
||||
unit: string | null;
|
||||
note: string | null;
|
||||
}[];
|
||||
};
|
||||
} & {
|
||||
id: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
servings: number | null;
|
||||
userId: number;
|
||||
recipeId: number;
|
||||
date: Date;
|
||||
})[]>;
|
||||
shoppingList(user: {
|
||||
userId: number;
|
||||
}, from: string, to: string): Promise<{
|
||||
productId: number;
|
||||
name: string;
|
||||
quantity: number;
|
||||
unit: string;
|
||||
}[]>;
|
||||
inventoryCompare(user: {
|
||||
userId: number;
|
||||
}, from: string, to: string): Promise<{
|
||||
productId: number;
|
||||
name: string;
|
||||
required: number;
|
||||
unit: string;
|
||||
available: number;
|
||||
missing: number;
|
||||
status: "enough" | "missing" | "pantry";
|
||||
}[]>;
|
||||
upsert(user: {
|
||||
userId: number;
|
||||
}, dto: CreateMealPlanEntryDto): Promise<{
|
||||
recipe: {
|
||||
name: string;
|
||||
id: number;
|
||||
imageUrl: string | null;
|
||||
servings: number | null;
|
||||
ingredients: {
|
||||
product: {
|
||||
name: string;
|
||||
canonicalName: string | null;
|
||||
id: number;
|
||||
} | null;
|
||||
quantity: import("@prisma/client/runtime/library").Decimal | null;
|
||||
unit: string | null;
|
||||
note: string | null;
|
||||
}[];
|
||||
};
|
||||
} & {
|
||||
id: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
servings: number | null;
|
||||
userId: number;
|
||||
recipeId: number;
|
||||
date: Date;
|
||||
}>;
|
||||
removeByDate(user: {
|
||||
userId: number;
|
||||
}, date: string): Promise<{
|
||||
id: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
servings: number | null;
|
||||
userId: number;
|
||||
recipeId: number;
|
||||
date: Date;
|
||||
}>;
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
"use strict";
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
var __metadata = (this && this.__metadata) || function (k, v) {
|
||||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
||||
};
|
||||
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||
return function (target, key) { decorator(target, key, paramIndex); }
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.MealPlanController = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const meal_plan_service_1 = require("./meal-plan.service");
|
||||
const create_meal_plan_entry_dto_1 = require("./dto/create-meal-plan-entry.dto");
|
||||
const current_user_decorator_1 = require("../auth/decorators/current-user.decorator");
|
||||
let MealPlanController = class MealPlanController {
|
||||
constructor(mealPlanService) {
|
||||
this.mealPlanService = mealPlanService;
|
||||
}
|
||||
findByRange(user, from, to) {
|
||||
return this.mealPlanService.findByRange(user.userId, from, to);
|
||||
}
|
||||
shoppingList(user, from, to) {
|
||||
return this.mealPlanService.shoppingList(user.userId, from, to);
|
||||
}
|
||||
inventoryCompare(user, from, to) {
|
||||
return this.mealPlanService.inventoryCompare(user.userId, from, to);
|
||||
}
|
||||
upsert(user, dto) {
|
||||
return this.mealPlanService.upsert(user.userId, dto);
|
||||
}
|
||||
removeByDate(user, date) {
|
||||
return this.mealPlanService.removeByDate(user.userId, date);
|
||||
}
|
||||
};
|
||||
exports.MealPlanController = MealPlanController;
|
||||
__decorate([
|
||||
(0, common_1.Get)(),
|
||||
__param(0, (0, current_user_decorator_1.CurrentUser)()),
|
||||
__param(1, (0, common_1.Query)('from')),
|
||||
__param(2, (0, common_1.Query)('to')),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Object, String, String]),
|
||||
__metadata("design:returntype", void 0)
|
||||
], MealPlanController.prototype, "findByRange", null);
|
||||
__decorate([
|
||||
(0, common_1.Get)('shopping-list'),
|
||||
__param(0, (0, current_user_decorator_1.CurrentUser)()),
|
||||
__param(1, (0, common_1.Query)('from')),
|
||||
__param(2, (0, common_1.Query)('to')),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Object, String, String]),
|
||||
__metadata("design:returntype", void 0)
|
||||
], MealPlanController.prototype, "shoppingList", null);
|
||||
__decorate([
|
||||
(0, common_1.Get)('inventory-compare'),
|
||||
__param(0, (0, current_user_decorator_1.CurrentUser)()),
|
||||
__param(1, (0, common_1.Query)('from')),
|
||||
__param(2, (0, common_1.Query)('to')),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Object, String, String]),
|
||||
__metadata("design:returntype", void 0)
|
||||
], MealPlanController.prototype, "inventoryCompare", null);
|
||||
__decorate([
|
||||
(0, common_1.Post)(),
|
||||
__param(0, (0, current_user_decorator_1.CurrentUser)()),
|
||||
__param(1, (0, common_1.Body)()),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Object, create_meal_plan_entry_dto_1.CreateMealPlanEntryDto]),
|
||||
__metadata("design:returntype", void 0)
|
||||
], MealPlanController.prototype, "upsert", null);
|
||||
__decorate([
|
||||
(0, common_1.Delete)(':date'),
|
||||
__param(0, (0, current_user_decorator_1.CurrentUser)()),
|
||||
__param(1, (0, common_1.Param)('date')),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Object, String]),
|
||||
__metadata("design:returntype", void 0)
|
||||
], MealPlanController.prototype, "removeByDate", null);
|
||||
exports.MealPlanController = MealPlanController = __decorate([
|
||||
(0, common_1.Controller)('meal-plan'),
|
||||
__metadata("design:paramtypes", [meal_plan_service_1.MealPlanService])
|
||||
], MealPlanController);
|
||||
//# sourceMappingURL=meal-plan.controller.js.map
|
||||
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"meal-plan.controller.js","sourceRoot":"","sources":["../../src/meal-plan/meal-plan.controller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAmF;AACnF,2DAAsD;AACtD,iFAA0E;AAC1E,sFAAwE;AAGjE,IAAM,kBAAkB,GAAxB,MAAM,kBAAkB;IAC7B,YAA6B,eAAgC;QAAhC,oBAAe,GAAf,eAAe,CAAiB;IAAG,CAAC;IAGjE,WAAW,CACM,IAAwB,EACxB,IAAY,EACd,EAAU;QAEvB,OAAO,IAAI,CAAC,eAAe,CAAC,WAAW,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;IACjE,CAAC;IAGD,YAAY,CACK,IAAwB,EACxB,IAAY,EACd,EAAU;QAEvB,OAAO,IAAI,CAAC,eAAe,CAAC,YAAY,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;IAClE,CAAC;IAGD,gBAAgB,CACC,IAAwB,EACxB,IAAY,EACd,EAAU;QAEvB,OAAO,IAAI,CAAC,eAAe,CAAC,gBAAgB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;IACtE,CAAC;IAGD,MAAM,CACW,IAAwB,EAC/B,GAA2B;QAEnC,OAAO,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACvD,CAAC;IAGD,YAAY,CACK,IAAwB,EACxB,IAAY;QAE3B,OAAO,IAAI,CAAC,eAAe,CAAC,YAAY,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAC9D,CAAC;CACF,CAAA;AA7CY,gDAAkB;AAI7B;IADC,IAAA,YAAG,GAAE;IAEH,WAAA,IAAA,oCAAW,GAAE,CAAA;IACb,WAAA,IAAA,cAAK,EAAC,MAAM,CAAC,CAAA;IACb,WAAA,IAAA,cAAK,EAAC,IAAI,CAAC,CAAA;;;;qDAGb;AAGD;IADC,IAAA,YAAG,EAAC,eAAe,CAAC;IAElB,WAAA,IAAA,oCAAW,GAAE,CAAA;IACb,WAAA,IAAA,cAAK,EAAC,MAAM,CAAC,CAAA;IACb,WAAA,IAAA,cAAK,EAAC,IAAI,CAAC,CAAA;;;;sDAGb;AAGD;IADC,IAAA,YAAG,EAAC,mBAAmB,CAAC;IAEtB,WAAA,IAAA,oCAAW,GAAE,CAAA;IACb,WAAA,IAAA,cAAK,EAAC,MAAM,CAAC,CAAA;IACb,WAAA,IAAA,cAAK,EAAC,IAAI,CAAC,CAAA;;;;0DAGb;AAGD;IADC,IAAA,aAAI,GAAE;IAEJ,WAAA,IAAA,oCAAW,GAAE,CAAA;IACb,WAAA,IAAA,aAAI,GAAE,CAAA;;6CAAM,mDAAsB;;gDAGpC;AAGD;IADC,IAAA,eAAM,EAAC,OAAO,CAAC;IAEb,WAAA,IAAA,oCAAW,GAAE,CAAA;IACb,WAAA,IAAA,cAAK,EAAC,MAAM,CAAC,CAAA;;;;sDAGf;6BA5CU,kBAAkB;IAD9B,IAAA,mBAAU,EAAC,WAAW,CAAC;qCAEwB,mCAAe;GADlD,kBAAkB,CA6C9B"}
|
||||
@@ -0,0 +1,2 @@
|
||||
export declare class MealPlanModule {
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
"use strict";
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.MealPlanModule = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const meal_plan_controller_1 = require("./meal-plan.controller");
|
||||
const meal_plan_service_1 = require("./meal-plan.service");
|
||||
const prisma_module_1 = require("../prisma/prisma.module");
|
||||
let MealPlanModule = class MealPlanModule {
|
||||
};
|
||||
exports.MealPlanModule = MealPlanModule;
|
||||
exports.MealPlanModule = MealPlanModule = __decorate([
|
||||
(0, common_1.Module)({
|
||||
controllers: [meal_plan_controller_1.MealPlanController],
|
||||
providers: [meal_plan_service_1.MealPlanService],
|
||||
imports: [prisma_module_1.PrismaModule],
|
||||
})
|
||||
], MealPlanModule);
|
||||
//# sourceMappingURL=meal-plan.module.js.map
|
||||
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"meal-plan.module.js","sourceRoot":"","sources":["../../src/meal-plan/meal-plan.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAwC;AACxC,iEAA4D;AAC5D,2DAAsD;AACtD,2DAAuD;AAOhD,IAAM,cAAc,GAApB,MAAM,cAAc;CAAG,CAAA;AAAjB,wCAAc;yBAAd,cAAc;IAL1B,IAAA,eAAM,EAAC;QACN,WAAW,EAAE,CAAC,yCAAkB,CAAC;QACjC,SAAS,EAAE,CAAC,mCAAe,CAAC;QAC5B,OAAO,EAAE,CAAC,4BAAY,CAAC;KACxB,CAAC;GACW,cAAc,CAAG"}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { CreateMealPlanEntryDto } from './dto/create-meal-plan-entry.dto';
|
||||
export declare class MealPlanService {
|
||||
private readonly prisma;
|
||||
constructor(prisma: PrismaService);
|
||||
findByRange(userId: number, from: string, to: string): Promise<({
|
||||
recipe: {
|
||||
name: string;
|
||||
id: number;
|
||||
imageUrl: string | null;
|
||||
servings: number | null;
|
||||
ingredients: {
|
||||
product: {
|
||||
name: string;
|
||||
canonicalName: string | null;
|
||||
id: number;
|
||||
} | null;
|
||||
quantity: import("@prisma/client/runtime/library").Decimal | null;
|
||||
unit: string | null;
|
||||
note: string | null;
|
||||
}[];
|
||||
};
|
||||
} & {
|
||||
id: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
servings: number | null;
|
||||
userId: number;
|
||||
recipeId: number;
|
||||
date: Date;
|
||||
})[]>;
|
||||
upsert(userId: number, dto: CreateMealPlanEntryDto): Promise<{
|
||||
recipe: {
|
||||
name: string;
|
||||
id: number;
|
||||
imageUrl: string | null;
|
||||
servings: number | null;
|
||||
ingredients: {
|
||||
product: {
|
||||
name: string;
|
||||
canonicalName: string | null;
|
||||
id: number;
|
||||
} | null;
|
||||
quantity: import("@prisma/client/runtime/library").Decimal | null;
|
||||
unit: string | null;
|
||||
note: string | null;
|
||||
}[];
|
||||
};
|
||||
} & {
|
||||
id: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
servings: number | null;
|
||||
userId: number;
|
||||
recipeId: number;
|
||||
date: Date;
|
||||
}>;
|
||||
removeByDate(userId: number, date: string): Promise<{
|
||||
id: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
servings: number | null;
|
||||
userId: number;
|
||||
recipeId: number;
|
||||
date: Date;
|
||||
}>;
|
||||
private aggregateIngredients;
|
||||
shoppingList(userId: number, from: string, to: string): Promise<{
|
||||
productId: number;
|
||||
name: string;
|
||||
quantity: number;
|
||||
unit: string;
|
||||
}[]>;
|
||||
inventoryCompare(userId: number, from: string, to: string): Promise<{
|
||||
productId: number;
|
||||
name: string;
|
||||
required: number;
|
||||
unit: string;
|
||||
available: number;
|
||||
missing: number;
|
||||
status: "enough" | "missing" | "pantry";
|
||||
}[]>;
|
||||
}
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
"use strict";
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
var __metadata = (this && this.__metadata) || function (k, v) {
|
||||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.MealPlanService = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const prisma_service_1 = require("../prisma/prisma.service");
|
||||
const recipeSelect = {
|
||||
id: true,
|
||||
name: true,
|
||||
imageUrl: true,
|
||||
servings: true,
|
||||
ingredients: {
|
||||
select: {
|
||||
quantity: true,
|
||||
unit: true,
|
||||
note: true,
|
||||
product: { select: { id: true, name: true, canonicalName: true } },
|
||||
},
|
||||
},
|
||||
};
|
||||
let MealPlanService = class MealPlanService {
|
||||
constructor(prisma) {
|
||||
this.prisma = prisma;
|
||||
}
|
||||
async findByRange(userId, from, to) {
|
||||
return this.prisma.mealPlanEntry.findMany({
|
||||
where: {
|
||||
userId,
|
||||
date: { gte: new Date(from), lte: new Date(to) },
|
||||
},
|
||||
include: { recipe: { select: recipeSelect } },
|
||||
orderBy: { date: 'asc' },
|
||||
});
|
||||
}
|
||||
async upsert(userId, dto) {
|
||||
const date = new Date(dto.date);
|
||||
return this.prisma.mealPlanEntry.upsert({
|
||||
where: {
|
||||
userId_date: {
|
||||
userId,
|
||||
date,
|
||||
},
|
||||
},
|
||||
create: {
|
||||
userId,
|
||||
date,
|
||||
recipeId: dto.recipeId,
|
||||
servings: dto.servings ?? null,
|
||||
},
|
||||
update: { recipeId: dto.recipeId, servings: dto.servings ?? null },
|
||||
include: { recipe: { select: recipeSelect } },
|
||||
});
|
||||
}
|
||||
async removeByDate(userId, date) {
|
||||
const entry = await this.prisma.mealPlanEntry.findUnique({
|
||||
where: {
|
||||
userId_date: {
|
||||
userId,
|
||||
date: new Date(date),
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!entry)
|
||||
throw new common_1.NotFoundException('Ingen matplanspost för detta datum');
|
||||
return this.prisma.mealPlanEntry.delete({ where: { id: entry.id } });
|
||||
}
|
||||
aggregateIngredients(entries) {
|
||||
const map = new Map();
|
||||
for (const entry of entries) {
|
||||
const recipeServings = entry.recipe.servings;
|
||||
const entryServings = entry.servings;
|
||||
const scale = recipeServings && entryServings ? entryServings / recipeServings : 1;
|
||||
for (const ing of entry.recipe.ingredients) {
|
||||
if (!ing.product || !ing.unit) {
|
||||
continue;
|
||||
}
|
||||
const key = `${ing.product.id}-${ing.unit}`;
|
||||
const qty = Number(ing.quantity ?? 0) * scale;
|
||||
const existing = map.get(key);
|
||||
if (existing) {
|
||||
existing.quantity += qty;
|
||||
}
|
||||
else {
|
||||
map.set(key, {
|
||||
productId: ing.product.id,
|
||||
name: ing.product.canonicalName || ing.product.name,
|
||||
quantity: qty,
|
||||
unit: ing.unit,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return Array.from(map.values());
|
||||
}
|
||||
async shoppingList(userId, from, to) {
|
||||
const entries = await this.findByRange(userId, from, to);
|
||||
return this.aggregateIngredients(entries).sort((a, b) => a.name.localeCompare(b.name, 'sv'));
|
||||
}
|
||||
async inventoryCompare(userId, from, to) {
|
||||
const entries = await this.findByRange(userId, from, to);
|
||||
const pantryItems = await this.prisma.pantryItem.findMany({
|
||||
where: { userId },
|
||||
select: { productId: true },
|
||||
});
|
||||
const pantryProductIds = new Set(pantryItems.map((p) => p.productId));
|
||||
const aggregated = this.aggregateIngredients(entries).map((item) => ({
|
||||
productId: item.productId,
|
||||
name: item.name,
|
||||
required: item.quantity,
|
||||
unit: item.unit,
|
||||
}));
|
||||
const result = await Promise.all(aggregated.map(async (item) => {
|
||||
if (pantryProductIds.has(item.productId)) {
|
||||
return {
|
||||
productId: item.productId,
|
||||
name: item.name,
|
||||
required: item.required,
|
||||
unit: item.unit,
|
||||
available: item.required,
|
||||
missing: 0,
|
||||
status: 'pantry',
|
||||
};
|
||||
}
|
||||
const inventoryItems = await this.prisma.inventoryItem.findMany({
|
||||
where: { productId: item.productId },
|
||||
});
|
||||
const available = inventoryItems
|
||||
.filter((i) => i.unit.trim().toLowerCase() === item.unit.trim().toLowerCase())
|
||||
.reduce((sum, i) => sum + Number(i.quantity), 0);
|
||||
return {
|
||||
productId: item.productId,
|
||||
name: item.name,
|
||||
required: item.required,
|
||||
unit: item.unit,
|
||||
available,
|
||||
missing: Math.max(0, item.required - available),
|
||||
status: (available >= item.required ? 'enough' : 'missing'),
|
||||
};
|
||||
}));
|
||||
const statusOrder = { missing: 0, enough: 1, pantry: 2 };
|
||||
return result.sort((a, b) => {
|
||||
const diff = statusOrder[a.status] - statusOrder[b.status];
|
||||
if (diff !== 0)
|
||||
return diff;
|
||||
return a.name.localeCompare(b.name, 'sv');
|
||||
});
|
||||
}
|
||||
};
|
||||
exports.MealPlanService = MealPlanService;
|
||||
exports.MealPlanService = MealPlanService = __decorate([
|
||||
(0, common_1.Injectable)(),
|
||||
__metadata("design:paramtypes", [prisma_service_1.PrismaService])
|
||||
], MealPlanService);
|
||||
//# sourceMappingURL=meal-plan.service.js.map
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user