diff --git a/api/src/controllers/locationController.ts b/api/src/controllers/locationController.ts index e49341e4..3ee5e44a 100644 --- a/api/src/controllers/locationController.ts +++ b/api/src/controllers/locationController.ts @@ -51,14 +51,14 @@ export const create = async (req: Request, res: Response) => { const names = body try { - const values = [] + const values: string[] = [] for (const name of names) { const locationValue = new LocationValue({ language: name.language, value: name.name, }) await locationValue.save() - values.push(locationValue._id) + values.push(locationValue.id) } const location = new Location({ values }) diff --git a/api/src/controllers/propertyController.ts b/api/src/controllers/propertyController.ts index 5a3ca286..7925be5a 100644 --- a/api/src/controllers/propertyController.ts +++ b/api/src/controllers/propertyController.ts @@ -705,9 +705,43 @@ export const getFrontendProperties = async (req: Request, res: Response) => { as: 'location', }, }, + { + $addFields: + { + dailyPrice: + { + $function: + { + // eslint-disable-next-line func-names, object-shorthand + body: function (price, rentalTerm) { + let dailyPrice = 0 + const now = new Date() + if (rentalTerm === 'MONTHLY') { + dailyPrice = price / new Date(now.getFullYear(), now.getMonth(), 0).getDate() + } else if (rentalTerm === 'WEEKLY') { + dailyPrice = price / 7 + } else if (rentalTerm === 'DAILY') { + dailyPrice = price + } else if (rentalTerm === 'YEARLY') { + const year = now.getFullYear() + dailyPrice = price / (((year % 4 === 0 && year % 100 > 0) || year % 400 === 0) ? 366 : 365) + } + + dailyPrice = Number((Math.trunc(dailyPrice * 100) / 100).toFixed(2)) + if (dailyPrice % 1 === 0) { + return Math.round(dailyPrice) + } + return dailyPrice + }, + args: ['$price', '$rentalTerm'], + lang: 'js', + }, + }, + }, + }, { $facet: { - resultData: [{ $sort: { name: 1 } }, { $skip: (page - 1) * size }, { $limit: size }], + resultData: [{ $sort: { dailyPrice: 1 } }, { $skip: (page - 1) * size }, { $limit: size }], pageInfo: [ { $count: 'totalRecords', diff --git a/api/tsconfig.json b/api/tsconfig.json index 3090b786..cc3e5e89 100644 --- a/api/tsconfig.json +++ b/api/tsconfig.json @@ -85,7 +85,7 @@ /* Type Checking */ "strict": true, /* Enable all strict type-checking options. */ - "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ + "noImplicitAny": false, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */