From 737fedc3145820dc8e1df066169e0d0a7f7803f5 Mon Sep 17 00:00:00 2001 From: Nekitech Date: Thu, 2 May 2024 22:14:49 +0300 Subject: [PATCH] fix --- Makefile | 9 ++ package-lock.json | 12 +++ package.json | 3 +- prisma/schema.prisma | 8 +- src/app.ts | 19 ++-- src/app/models/Equipment/Equipment.ts | 1 - src/app/repositories/BookingRepo.ts | 2 + src/app/repositories/UserRepo.ts | 1 + .../services/BookingService/BookingService.ts | 24 ++--- .../Notifications/NotificationsService.ts | 40 ++++++++ .../Equipment/EquipmentController.ts | 1 - .../PostgresQL/BookingRepoImplement.ts | 8 ++ .../PostgresQL/EquipmentRepoImplement.ts | 2 - .../PostgresQL/UserRepoImplement.ts | 93 ++++++++++--------- src/infrastructure/helpers/Date.ts | 15 +++ .../validations/Booking/BookingValidations.ts | 1 - 16 files changed, 155 insertions(+), 84 deletions(-) create mode 100644 src/app/services/Notifications/NotificationsService.ts create mode 100644 src/infrastructure/helpers/Date.ts diff --git a/Makefile b/Makefile index 45d8ea0..b568d60 100644 --- a/Makefile +++ b/Makefile @@ -10,9 +10,18 @@ docker_create_volume: build: docker_create_volume docker_build run_db_push +backup_db: + docker exec db sh -c "pg_dumpall -c -U postgres > dump.sql" + +restore_db: + cat dump_.sql | docker exec -i db psql -U postgres -d reeq-db + run_db_push: npx prisma db push +cp_dump_file: + docker cp 77725cc46d2b:/dump.sql ./prisma/dumps + run_migrations: npx prisma migrate deploy ## Для загрузки дампа diff --git a/package-lock.json b/package-lock.json index 5cafc25..6978ea4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,6 +25,7 @@ "jsonwebtoken": "^9.0.2", "module-alias": "^2.2.3", "moment": "^2.30.1", + "moment-timezone": "^0.5.45", "multer": "^1.4.5-lts.1", "pg": "^8.11.3", "socket.io": "^4.7.5" @@ -2323,6 +2324,17 @@ "node": "*" } }, + "node_modules/moment-timezone": { + "version": "0.5.45", + "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.45.tgz", + "integrity": "sha512-HIWmqA86KcmCAhnMAN0wuDOARV/525R2+lOLotuGFzn4HO+FH+/645z2wx0Dt3iDv6/p61SIvKnDstISainhLQ==", + "dependencies": { + "moment": "^2.29.4" + }, + "engines": { + "node": "*" + } + }, "node_modules/ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", diff --git a/package.json b/package.json index 58d1916..8e2c884 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "build": "rimraf build && tsc", "start": "npm run build && node --es-module-specifier-resolution=node build/src/app.js", "docker_build": "docker-compose up -d --build", - "migrate": "npx prisma migrate dev --name", + "migrate:dev": "npx dotenv-cli -e .env.development -- npx prisma migrate dev --name", "seed": "node --loader ts-node/esm prisma/seeds/seeds.ts" }, "type": "module", @@ -48,6 +48,7 @@ "jsonwebtoken": "^9.0.2", "module-alias": "^2.2.3", "moment": "^2.30.1", + "moment-timezone": "^0.5.45", "multer": "^1.4.5-lts.1", "pg": "^8.11.3", "socket.io": "^4.7.5" diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 6c5157d..66a4fb3 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -41,7 +41,6 @@ model equipments { name String @db.VarChar(255) description String? count Int - status StatusEquipment @default(FREE) img_hrefs String[] created_at DateTime @default(now()) @db.Timestamptz(6) updated_at DateTime @default(now()) @db.Timestamptz(6) @@ -94,9 +93,4 @@ enum StatusBooking { CREATED COMPLETE EXPIRED -} - -enum StatusEquipment { - FREE - BOOKED -} +} \ No newline at end of file diff --git a/src/app.ts b/src/app.ts index 6cdbc36..53e03d5 100644 --- a/src/app.ts +++ b/src/app.ts @@ -12,13 +12,12 @@ import {bookingRoutes} from "./infrastructure/routes/rest/BookingRoutes"; import {uploadRoutes} from "./infrastructure/routes/rest/UploadRoutes"; import bodyParser from "body-parser"; import multer from "multer"; -import {Server} from "socket.io"; import * as http from "node:http"; dotenv.config(); const app = express(); const router = express.Router(); -const server = new http.Server(app) +const server = http.createServer(app) app.use(express.json()) app.use(cookieParser()) @@ -47,19 +46,15 @@ app.use(ErrorMiddleware) const port = process.env.PORT; -// Websocket init - -const io = new Server(server) -io.on('connection', (socket) => { - console.log('a user connected'); -}); - - app.get('/api', (req, res) => { res.send('Express + TypeScript Server'); }); -app.listen(port, () => { + +server.listen(port, () => { console.log(`⚡️[server]: Server is running at http://localhost:${port}/api`); -}); \ No newline at end of file +}); + + + diff --git a/src/app/models/Equipment/Equipment.ts b/src/app/models/Equipment/Equipment.ts index 54de9d1..0f52da4 100644 --- a/src/app/models/Equipment/Equipment.ts +++ b/src/app/models/Equipment/Equipment.ts @@ -7,7 +7,6 @@ export class Equipment { public name: string, public description: string | null, public count: number, - public status: keyof typeof EquipmentStatus, public img_hrefs: string[], public created_at?: Date, public updated_at?: Date, diff --git a/src/app/repositories/BookingRepo.ts b/src/app/repositories/BookingRepo.ts index cac0951..d6402e3 100644 --- a/src/app/repositories/BookingRepo.ts +++ b/src/app/repositories/BookingRepo.ts @@ -1,6 +1,7 @@ import {Booking} from "../models/Booking/Booking"; import {addBookingDto, getBookingByParamsDtoType} from "./dto/addBookingDto"; import {updateBookingDto} from "./dto/updateBookingDto"; +import {BookingValidations} from "../../infrastructure/validations/Booking/BookingValidations"; export interface BookingRepo { getByFilter(date: string, skip: number| undefined, take: number| undefined): Promise @@ -9,4 +10,5 @@ export interface BookingRepo { getByField(fields: object): Promise<{}[]> getByParams(params: getBookingByParamsDtoType, include: object): any getListsTimeReservation(equipment_id: number): any + getBySelect(where: object, select: object): Promise } \ No newline at end of file diff --git a/src/app/repositories/UserRepo.ts b/src/app/repositories/UserRepo.ts index 9aa99da..9582d3d 100644 --- a/src/app/repositories/UserRepo.ts +++ b/src/app/repositories/UserRepo.ts @@ -6,6 +6,7 @@ export interface UserRepo { getById(id: number): Promise getByFieldName(fieldName: string, typeField: string): Promise add(user: addUserDto): Promise + getBySelect(where: object, select: object): any // delete(id: number): string // update(user: updateUserDto): User } \ No newline at end of file diff --git a/src/app/services/BookingService/BookingService.ts b/src/app/services/BookingService/BookingService.ts index 2e11989..c6279f5 100644 --- a/src/app/services/BookingService/BookingService.ts +++ b/src/app/services/BookingService/BookingService.ts @@ -12,10 +12,10 @@ export class BookingService { constructor(private bookingRepo: BookingRepo, private equipmentRepo: EquipmentRepo) { } - async isBooking(id: number) { - const booking = await this.equipmentRepo.getById(id) - return booking?.status === EquipmentStatus.BOOKED - } + // async isBooking(id: number) { + // const booking = await this.equipmentRepo.getById(id) + // return booking?.status === EquipmentStatus.BOOKED + // } /** * Создание брони конкретного оборудования в БД @@ -25,17 +25,12 @@ export class BookingService { async CreateBooking(bookingData: addBookingDto) { const {equipment_id} = bookingData - if(await this.isBooking(equipment_id)) { - throw ErrorsHandler.BadRequest("Оборудование уже забронировано!" ) - } + // if(await this.isBooking(equipment_id)) { + // throw ErrorsHandler.BadRequest("Оборудование уже забронировано!" ) + // } const addedBooking = this.bookingRepo.add(bookingData) - // await this.equipmentRepo.update({ - // id: equipment_id, - // status: EquipmentStatus.BOOKED - // }) - return addedBooking } @@ -62,10 +57,7 @@ export class BookingService { } async closeBooking(bookingId: number, equipmentId: number) { - await this.equipmentRepo.update({ - id: equipmentId, - status: EquipmentStatus.FREE - }) + return this.bookingRepo.update({ id: bookingId, status: BookingStatus.COMPLETE, diff --git a/src/app/services/Notifications/NotificationsService.ts b/src/app/services/Notifications/NotificationsService.ts new file mode 100644 index 0000000..b7abfa5 --- /dev/null +++ b/src/app/services/Notifications/NotificationsService.ts @@ -0,0 +1,40 @@ +import {BookingStatus} from "../../../infrastructure/shared/types/Booking"; +import {equalDateTimeBookingExpired} from "../../../infrastructure/helpers/Date"; +import {BookingRepo} from "../../repositories/BookingRepo"; +import {UserRepo} from "../../repositories/UserRepo"; + +export class NotificationsService { + + constructor( + private BookingRepo: BookingRepo, + private UserRepo: UserRepo, + ) { + } + + + async sendAlertReservationNotification() { + const listUsers = await this.UserRepo.getBySelect({}, { + id: true + }) + listUsers.forEach(async ({id: user_id}: {id: number}) => { + + const listBookings = await this.BookingRepo.getBySelect({ + status: BookingStatus.CREATED, + user_id + }, { + time_from: true, + time_to: true, + date: true + }) + + listBookings?.forEach((booking: any) => { + const {date, time_to} = booking + if(equalDateTimeBookingExpired(date, time_to)) { + console.log("check") + } + }) + + }) + } +} + diff --git a/src/infrastructure/controllers/Equipment/EquipmentController.ts b/src/infrastructure/controllers/Equipment/EquipmentController.ts index d47238c..4d10bcd 100644 --- a/src/infrastructure/controllers/Equipment/EquipmentController.ts +++ b/src/infrastructure/controllers/Equipment/EquipmentController.ts @@ -13,7 +13,6 @@ export class EquipmentController { const {name, status, skip, take} = req.query; const {data, count} = await this.equipmentService.getAllEquipments({ name: name as string, - status: status as EquipmentStatus, }, Number(skip), Number(take)) res.send({ diff --git a/src/infrastructure/db/repository/PostgresQL/BookingRepoImplement.ts b/src/infrastructure/db/repository/PostgresQL/BookingRepoImplement.ts index faff34c..2975114 100644 --- a/src/infrastructure/db/repository/PostgresQL/BookingRepoImplement.ts +++ b/src/infrastructure/db/repository/PostgresQL/BookingRepoImplement.ts @@ -3,6 +3,7 @@ import {Booking} from "../../../../app/models/Booking/Booking"; import {addBookingDto, getBookingByParamsDtoType} from "../../../../app/repositories/dto/addBookingDto"; import {prisma} from "../../orm/prisma/PrismaClient"; import {updateBookingDto} from "../../../../app/repositories/dto/updateBookingDto"; +import {BookingValidations} from "../../../validations/Booking/BookingValidations"; class BookingRepoImplement implements BookingRepo { @@ -20,6 +21,13 @@ class BookingRepoImplement implements BookingRepo { }) } + async getBySelect(where: object, select: object): Promise { + return prisma.booking.findMany({ + where, + select + }) as Promise + } + async getByParams(params: getBookingByParamsDtoType, include: object) { return prisma.booking.findMany({ where: { diff --git a/src/infrastructure/db/repository/PostgresQL/EquipmentRepoImplement.ts b/src/infrastructure/db/repository/PostgresQL/EquipmentRepoImplement.ts index fadc696..756e44d 100644 --- a/src/infrastructure/db/repository/PostgresQL/EquipmentRepoImplement.ts +++ b/src/infrastructure/db/repository/PostgresQL/EquipmentRepoImplement.ts @@ -12,7 +12,6 @@ const defaultReturnObj: Equipment = { id: 0, description: "", area_id: 0, - status: EquipmentStatus.FREE, img_hrefs: [] } @@ -51,7 +50,6 @@ export class EquipmentRepoImplement implements EquipmentRepo { name: { contains: filter?.name }, - status: filter?.status }, skip, take, diff --git a/src/infrastructure/db/repository/PostgresQL/UserRepoImplement.ts b/src/infrastructure/db/repository/PostgresQL/UserRepoImplement.ts index 6aa25c5..9875d8f 100644 --- a/src/infrastructure/db/repository/PostgresQL/UserRepoImplement.ts +++ b/src/infrastructure/db/repository/PostgresQL/UserRepoImplement.ts @@ -5,49 +5,56 @@ import {addUserDto} from "../../../../app/repositories/dto/addUserDto"; export class UserRepoImplement implements UserRepo { - async getByFieldName(fieldName: string, typeField: string): Promise { - return prisma.users.findFirst({ - where: { - [typeField]: fieldName, - }, - include: { - roles: { - select: { - role: true - } - } - } - }) - } - - async getById(id: number) { - return prisma.users.findUnique({ - where: { - id - }, - include: { - roles: { - select: { - role: true - } - } - } - }) - } - - async add(user: addUserDto) { - return prisma.users.create({ - data: user, - include: { - roles: { - select: { - role: true - } - } - } - }) - } - + async getByFieldName(fieldName: string, typeField: string): Promise { + return prisma.users.findFirst({ + where: { + [typeField]: fieldName, + }, + include: { + roles: { + select: { + role: true + } + } + }, + }) + } + + async getBySelect(where: object, select: object) { + return prisma.users.findMany({ + select, + where + }) + } + + async getById(id: number) { + return prisma.users.findUnique({ + where: { + id + }, + include: { + roles: { + select: { + role: true + } + } + } + }) + } + + async add(user: addUserDto) { + return prisma.users.create({ + data: user, + include: { + roles: { + select: { + role: true + } + } + } + }) + } + } export const postgresUserRepository = new UserRepoImplement() \ No newline at end of file diff --git a/src/infrastructure/helpers/Date.ts b/src/infrastructure/helpers/Date.ts new file mode 100644 index 0000000..67b084b --- /dev/null +++ b/src/infrastructure/helpers/Date.ts @@ -0,0 +1,15 @@ +import moment from "moment"; + + +export const equalDateTimeBookingExpired = (date: Date, time_to: string) => { + const currDate = moment().tz("Europe/Moscow").format("YYYY-MM-DD") + const comparisonDate = moment(date).format("YYYY-MM-DD") + if(currDate > comparisonDate) { + return true + } else if(currDate == comparisonDate) { + const currTime = moment().tz("Europe/Moscow").format("HH:mm") + return currTime > time_to + } + + return false +} diff --git a/src/infrastructure/validations/Booking/BookingValidations.ts b/src/infrastructure/validations/Booking/BookingValidations.ts index 6c7a305..25d0762 100644 --- a/src/infrastructure/validations/Booking/BookingValidations.ts +++ b/src/infrastructure/validations/Booking/BookingValidations.ts @@ -1,4 +1,3 @@ -import {BookingService} from "../../../app/services/BookingService/BookingService"; import {BookingRepo} from "../../../app/repositories/BookingRepo"; import {postgresBookingRepository} from "../../db/repository/PostgresQL/BookingRepoImplement"; import {formatDateTime, rangesIntersect} from "../../helpers/Time";