|
@@ -2,7 +2,7 @@
|
|
|
import { Request, Response, NextFunction } from 'express';
|
|
import { Request, Response, NextFunction } from 'express';
|
|
|
import { IMongoService } from '../mongo';
|
|
import { IMongoService } from '../mongo';
|
|
|
import { ChainPController } from '../promise-all-middleware';
|
|
import { ChainPController } from '../promise-all-middleware';
|
|
|
-import PlanDB, { WeeklyPlanner, CompletePlanDB, Weekday } from './model';
|
|
|
|
|
|
|
+import PlanDB, { WeeklyPlanner, CompletePlanDB, Weekday, Plan } from './model';
|
|
|
import moment, { Moment } from 'moment';
|
|
import moment, { Moment } from 'moment';
|
|
|
import { ObjectId } from 'bson';
|
|
import { ObjectId } from 'bson';
|
|
|
import { RecipeDB } from '../recipes/model';
|
|
import { RecipeDB } from '../recipes/model';
|
|
@@ -15,25 +15,21 @@ const mkWeekQuery = (week: string) => ({
|
|
|
week: parseInt(week) || moment().isoWeek()
|
|
week: parseInt(week) || moment().isoWeek()
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+const validPlan = (mbPlan: any) => {
|
|
|
|
|
+ if (mbPlan.day && mbPlan.week && mbPlan.recipe && mbPlan.meal) {
|
|
|
|
|
+ return Promise.resolve({
|
|
|
|
|
+ ...mbPlan,
|
|
|
|
|
+ day: new Date(mbPlan.day),
|
|
|
|
|
+ recipe: new ObjectId(mbPlan.recipe)
|
|
|
|
|
+ } as Plan)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return Promise.reject('Required fields: day, week, recipe and meal');
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
const getWeekPlanner: Controller<void, PlanDB[]> =
|
|
const getWeekPlanner: Controller<void, PlanDB[]> =
|
|
|
db => req => () => db.find<PlanDB>(mkWeekQuery(req.params.week));
|
|
db => req => () => db.find<PlanDB>(mkWeekQuery(req.params.week));
|
|
|
|
|
|
|
|
-// const getWeekPlannerComplete: Controller<WeeklyPlanner> =
|
|
|
|
|
-// db => req => () => db.find<PlanDB>(mkWeekQuery(req.params.week)).then(
|
|
|
|
|
-// plans => plans.reduce((planner, plan) => {
|
|
|
|
|
-// const date = moment(plan.day);
|
|
|
|
|
-// return {
|
|
|
|
|
-// ...planner,
|
|
|
|
|
-// [getWeekDay(date)]: {
|
|
|
|
|
-// date,
|
|
|
|
|
-// [plan.meal]: plan.recipe
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-// }, {
|
|
|
|
|
-// week: mkWeekQuery(req.params.week).week
|
|
|
|
|
-// } as WeeklyPlanner)
|
|
|
|
|
-// )
|
|
|
|
|
-
|
|
|
|
|
const formatPlanner: Controller<PlanDB[], WeeklyPlanner> = db => req => prevResult => {
|
|
const formatPlanner: Controller<PlanDB[], WeeklyPlanner> = db => req => prevResult => {
|
|
|
const week = mkWeekQuery(req.params.week).week;
|
|
const week = mkWeekQuery(req.params.week).week;
|
|
|
return db.aggregate<CompletePlanDB>([
|
|
return db.aggregate<CompletePlanDB>([
|
|
@@ -67,7 +63,13 @@ const formatPlanner: Controller<PlanDB[], WeeklyPlanner> = db => req => prevResu
|
|
|
}, { week } as WeeklyPlanner))
|
|
}, { week } as WeeklyPlanner))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+const savePlan: Controller<void, PlanDB> = db => req => prevResult => {
|
|
|
|
|
+ const plan = req.body;
|
|
|
|
|
+ return validPlan(plan).then(db.insertOne)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
export {
|
|
export {
|
|
|
getWeekPlanner,
|
|
getWeekPlanner,
|
|
|
formatPlanner,
|
|
formatPlanner,
|
|
|
|
|
+ savePlan,
|
|
|
}
|
|
}
|