Kaynağa Gözat

Allow custom recipes in backend

Tatiana Inama 6 yıl önce
ebeveyn
işleme
e4b8b9eb60
2 değiştirilmiş dosya ile 16 ekleme ve 11 silme
  1. 9 6
      ktchn/src/planner/controller.ts
  2. 7 5
      ktchn/src/planner/model.ts

+ 9 - 6
ktchn/src/planner/controller.ts

@@ -9,14 +9,15 @@ type Controller<U, T> = (db: IMongoService) => ChainPController<U, T>;
 export const getWeekDay = (day: Moment): Weekday => day.format('dddd').toLowerCase() as Weekday;
 
 const validPlan = (mbPlan: any): Promise<Plan> => {
-  if (mbPlan.date && mbPlan.week && mbPlan.recipe && mbPlan.meal) {
+  if (mbPlan.date && mbPlan.week && (mbPlan.recipe || mbPlan.custom) && mbPlan.meal) {
     return Promise.resolve({
       ...mbPlan,
       date: new Date(mbPlan.date),
-      recipe: new ObjectId(mbPlan.recipe)
+      ...mbPlan.recipe && { recipe: new ObjectId(mbPlan.recipe) },
+      ...mbPlan.custom && { custom: mbPlan.custom }
     } as Plan)
   } else {
-    return Promise.reject('Required fields: day, week, recipe and meal');
+    return Promise.reject('Required fields: day, week, meal and recipe/custom');
   }
 }
 
@@ -43,7 +44,8 @@ const getPlannerByRange: Controller<{from: Date, to: Date}, WeeklyPlanner> = db
       }
     }, {
       '$unwind': {
-        'path': '$recipe'
+        'path': '$recipe',
+        'preserveNullAndEmptyArrays': true
       }
     }, {
       '$project': {
@@ -61,7 +63,7 @@ const getPlannerByRange: Controller<{from: Date, to: Date}, WeeklyPlanner> = db
       [getWeekDay(date)]: {
         ...planner[getWeekDay(date)],
         date: date,
-        [plan.meal]: plan.recipe
+        [plan.meal]: plan.custom || plan.recipe
       }
     };
   }, { week: moment(from).isoWeek() } as WeeklyPlanner))
@@ -81,7 +83,8 @@ type PlannerRequest = {
 const toPlan = (plan: any): Plan => ({
   ...plan,
   date: new Date(plan.date),
-  recipe: new ObjectId(plan.recipe),
+  ...plan.recipe && { recipe: new ObjectId(plan.recipe) },
+  ...plan.custom && { custom: plan.custom }
 })
 
 const validatePlanner: Controller<any, PlannerRequest> = () => ({ body }) => async () => {

+ 7 - 5
ktchn/src/planner/model.ts

@@ -19,9 +19,9 @@ export type Weekday =
 
 export type DayPlan = {
   date: Moment,
-  [Meal.Lunch]?: RecipePlan,
-  [Meal.Dinner]?: RecipePlan,
-  [Meal.Dessert]?: RecipePlan
+  [Meal.Lunch]?: RecipePlan | string,
+  [Meal.Dinner]?: RecipePlan | string,
+  [Meal.Dessert]?: RecipePlan | string
 }
 
 export type WeekPlan = {
@@ -34,8 +34,9 @@ export interface WeeklyPlanner extends WeekPlan {
 
 export interface Plan {
   date: Date,
-  recipe: ObjectID,
   meal: Meal
+  recipe?: ObjectID,
+  custom?: string,
 }
 
 export default interface PlanDB extends Plan {
@@ -48,5 +49,6 @@ export interface RecipePlan {
 }
 
 export interface CompletePlanDB extends Omit<PlanDB, 'recipe'> {
-  recipe: RecipePlan
+  recipe: RecipePlan,
+  custom?: string
 }