Skip to content

Commit

Permalink
Add property sort per daily price
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed May 18, 2024
1 parent 95f02bc commit 2085374
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
4 changes: 2 additions & 2 deletions api/src/controllers/locationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
36 changes: 35 additions & 1 deletion api/src/controllers/propertyController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down

0 comments on commit 2085374

Please sign in to comment.