Skip to content

Commit

Permalink
Add dynamic price calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Nov 16, 2024
1 parent 5807997 commit 5d03faa
Show file tree
Hide file tree
Showing 51 changed files with 5,120 additions and 7,641 deletions.
1,483 changes: 709 additions & 774 deletions api/package-lock.json

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,66 @@
"start": "npm run build && node dist/src",
"test": "rimraf coverage && npm run build && cross-env NODE_OPTIONS=--experimental-vm-modules jest --coverage --no-cache",
"lint": "eslint --ext .ts .",
"ncu": "ncu -u -x typescript,eslint,@types/express"
"ncu": "ncu -u -x typescript,eslint,@types/express",
"db:script": "npm run build && node dist/scripts/db.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@babel/cli": "^7.25.7",
"@babel/core": "^7.25.8",
"@babel/plugin-transform-modules-commonjs": "^7.25.7",
"@babel/preset-env": "^7.25.8",
"@babel/preset-typescript": "^7.25.7",
"@babel/cli": "^7.25.9",
"@babel/core": "^7.26.0",
"@babel/plugin-transform-modules-commonjs": "^7.25.9",
"@babel/preset-env": "^7.26.0",
"@babel/preset-typescript": "^7.26.0",
"@types/bcrypt": "^5.0.2",
"@types/compression": "^1.7.5",
"@types/cookie-parser": "^1.4.7",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.13",
"@types/jest": "^29.5.14",
"@types/multer": "^1.4.12",
"@types/node": "^22.7.5",
"@types/node": "^22.9.0",
"@types/nodemailer": "^6.4.16",
"@types/supertest": "^6.0.2",
"@types/uuid": "^10.0.0",
"@types/validator": "^13.12.2",
"axios": "^1.7.7",
"babel-jest": "^29.7.0",
"babel-plugin-add-import-extension": "^1.6.0",
"babel-plugin-module-resolver": "^5.0.2",
"bcrypt": "^5.1.1",
"compression": "^1.7.4",
"compression": "^1.7.5",
"cookie-parser": "^1.4.7",
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"dotenv": "^16.4.5",
"escape-string-regexp": "^5.0.0",
"expo-server-sdk": "^3.11.0",
"expo-server-sdk": "^3.12.0",
"express": "^4.21.1",
"helmet": "^8.0.0",
"i18n-js": "^4.4.3",
"i18n-js": "^4.5.0",
"jest": "^29.7.0",
"jose": "^5.9.4",
"mongoose": "^8.7.1",
"jose": "^5.9.6",
"mongoose": "^8.8.1",
"multer": "^1.4.5-lts.1",
"nanoid": "^5.0.8",
"nocache": "^4.0.0",
"nodemailer": "^6.9.15",
"nodemailer": "^6.9.16",
"rimraf": "^6.0.1",
"stripe": "^17.2.0",
"stripe": "^17.3.1",
"supertest": "^7.0.0",
"typescript": "^5.4.5",
"uuid": "^10.0.0",
"validator": "^13.12.0",
"winston": "^3.15.0"
"winston": "^3.17.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^8.8.1",
"@typescript-eslint/parser": "^8.8.1",
"@typescript-eslint/eslint-plugin": "^8.14.0",
"@typescript-eslint/parser": "^8.14.0",
"eslint": "^8.57.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.31.0",
"nodemon": "^3.1.7",
"npm-check-updates": "^17.1.3",
"tsx": "^4.19.1"
"npm-check-updates": "^17.1.11",
"tsx": "^4.19.2"
}
}
31 changes: 31 additions & 0 deletions api/scripts/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'dotenv/config'
import * as env from '../src/config/env.config'
import * as logger from '../src/common/logger'
import * as databaseHelper from '../src/common/databaseHelper'
import Car from '../src/models/Car'

if (
await databaseHelper.connect(env.DB_URI, env.DB_SSL, env.DB_DEBUG)
) {
const cars = await Car.find({})

for (const car of cars) {
if (car.price) {
car.dailyPrice = car.price
car.discountedDailyPrice = null
car.biWeeklyPrice = null
car.discountedBiWeeklyPrice = null
car.weeklyPrice = null
car.discountedWeeklyPrice = null
car.monthlyPrice = null
car.discountedMonthlyPrice = null
car.price = undefined
await car.save()
console.log(`${car.id} affected`)
}
}

await databaseHelper.close()
logger.info('MongoDB connection closed')
process.exit(0)
}
4 changes: 2 additions & 2 deletions api/src/common/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path'
import mongoose from 'mongoose'
import validator from 'validator'
import Stripe from 'stripe'
import { v1 as uuid } from 'uuid'
import { nanoid } from 'nanoid'
import axios from 'axios'
import * as bookcarsTypes from ':bookcars-types'

Expand Down Expand Up @@ -155,7 +155,7 @@ export const isValidEmail = (email?: string) => !!email && validator.isEmail(ema
*
* @returns {string}
*/
export const generateToken = () => `${uuid()}-${Date.now()}`
export const generateToken = () => `${nanoid()}-${Date.now()}`

/**
* The IETF language tag of the locale Checkout is displayed in.
Expand Down
12 changes: 11 additions & 1 deletion api/src/config/env.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,17 @@ export interface Car extends Document {
supplier: Types.ObjectId
minimumAge: number
locations: Types.ObjectId[]
price: number

price?: number // TODO remove
dailyPrice: number
discountedDailyPrice: number | null
biWeeklyPrice: number | null
discountedBiWeeklyPrice: number | null
weeklyPrice: number | null
discountedWeeklyPrice: number | null
monthlyPrice: number | null
discountedMonthlyPrice: number | null

deposit: number
available: boolean
type: bookcarsTypes.CarType
Expand Down
22 changes: 18 additions & 4 deletions api/src/controllers/carController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { v1 as uuid } from 'uuid'
import { nanoid } from 'nanoid'
import escapeStringRegexp from 'escape-string-regexp'
import mongoose from 'mongoose'
import { Request, Response } from 'express'
Expand Down Expand Up @@ -82,7 +82,14 @@ export const update = async (req: Request, res: Response) => {
available,
type,
locations,
price,
dailyPrice,
discountedDailyPrice,
biWeeklyPrice,
discountedBiWeeklyPrice,
weeklyPrice,
discountedWeeklyPrice,
monthlyPrice,
discountedMonthlyPrice,
deposit,
seats,
doors,
Expand All @@ -108,7 +115,14 @@ export const update = async (req: Request, res: Response) => {
car.name = name
car.available = available
car.type = type as bookcarsTypes.CarType
car.price = price
car.dailyPrice = dailyPrice
car.discountedDailyPrice = discountedDailyPrice
car.biWeeklyPrice = biWeeklyPrice
car.discountedBiWeeklyPrice = discountedBiWeeklyPrice
car.weeklyPrice = weeklyPrice
car.discountedWeeklyPrice = discountedWeeklyPrice
car.monthlyPrice = monthlyPrice
car.discountedMonthlyPrice = discountedMonthlyPrice
car.deposit = deposit
car.seats = seats
car.doors = doors
Expand Down Expand Up @@ -218,7 +232,7 @@ export const createImage = async (req: Request, res: Response) => {
throw new Error('[car.createImage] req.file not found')
}

const filename = `${helper.getFilenameWithoutExtension(req.file.originalname)}_${uuid()}_${Date.now()}${path.extname(req.file.originalname)}`
const filename = `${helper.getFilenameWithoutExtension(req.file.originalname)}_${nanoid()}_${Date.now()}${path.extname(req.file.originalname)}`
const filepath = path.join(env.CDN_TEMP_CARS, filename)

await fs.writeFile(filepath, req.file.buffer)
Expand Down
4 changes: 2 additions & 2 deletions api/src/controllers/locationController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { v1 as uuid } from 'uuid'
import { nanoid } from 'nanoid'
import escapeStringRegexp from 'escape-string-regexp'
import mongoose from 'mongoose'
import { Request, Response } from 'express'
Expand Down Expand Up @@ -596,7 +596,7 @@ export const createImage = async (req: Request, res: Response) => {
throw new Error('[location.createImage] req.file not found')
}

const filename = `${helper.getFilenameWithoutExtension(req.file.originalname)}_${uuid()}_${Date.now()}${path.extname(req.file.originalname)}`
const filename = `${helper.getFilenameWithoutExtension(req.file.originalname)}_${nanoid()}_${Date.now()}${path.extname(req.file.originalname)}`
const filepath = path.join(env.CDN_TEMP_LOCATIONS, filename)

await fs.writeFile(filepath, req.file.buffer)
Expand Down
4 changes: 2 additions & 2 deletions api/src/controllers/userController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path'
import fs from 'node:fs/promises'
import bcrypt from 'bcrypt'
import { v1 as uuid } from 'uuid'
import { nanoid } from 'nanoid'
import escapeStringRegexp from 'escape-string-regexp'
import mongoose from 'mongoose'
import { CookieOptions, Request, Response } from 'express'
Expand Down Expand Up @@ -1082,7 +1082,7 @@ export const createAvatar = async (req: Request, res: Response) => {
throw new Error('[user.createAvatar] req.file not found')
}

const filename = `${helper.getFilenameWithoutExtension(req.file.originalname)}_${uuid()}_${Date.now()}${path.extname(req.file.originalname)}`
const filename = `${helper.getFilenameWithoutExtension(req.file.originalname)}_${nanoid()}_${Date.now()}${path.extname(req.file.originalname)}`
const filepath = path.join(env.CDN_TEMP_USERS, filename)

await fs.writeFile(filepath, req.file.buffer)
Expand Down
29 changes: 29 additions & 0 deletions api/src/models/Car.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,39 @@ const carSchema = new Schema<env.Car>(
ref: 'Location',
validate: (value: any): boolean => Array.isArray(value) && value.length > 0,
},

// --------- price fields ---------
// TODO remove price field after demo db
price: {
type: Number,
},
dailyPrice: {
type: Number,
required: [true, "can't be blank"],
},
discountedDailyPrice: {
type: Number,
},
biWeeklyPrice: {
type: Number,
},
discountedBiWeeklyPrice: {
type: Number,
},
weeklyPrice: {
type: Number,
},
discountedWeeklyPrice: {
type: Number,
},
monthlyPrice: {
type: Number,
},
discountedMonthlyPrice: {
type: Number,
},
// --------- end of price fields ---------

deposit: {
type: Number,
required: [true, "can't be blank"],
Expand Down
Loading

0 comments on commit 5d03faa

Please sign in to comment.