From ec98f3ac26a3ad3e0826922bdb9effce22f6c270 Mon Sep 17 00:00:00 2001 From: OxSaitama Date: Thu, 4 May 2023 12:17:03 +0200 Subject: [PATCH] refacto hydratation on post requests --- src/app/api/customer/DocumentsController.ts | 3 +- .../api/super-admin/CustomersController.ts | 5 +-- .../api/super-admin/DeedTypesController.ts | 7 +--- .../super-admin/DocumentTypesController.ts | 7 +--- .../api/super-admin/DocumentsController.ts | 7 +--- .../super-admin/OfficeFoldersController.ts | 11 ++--- src/app/api/super-admin/OfficesController.ts | 7 +--- src/app/api/super-admin/UsersController.ts | 7 +--- .../20230426144239_init/migration.sql | 3 ++ src/common/databases/schema.prisma | 2 +- .../repositories/CustomersRepository.ts | 42 +++++++------------ .../super-admin/CustomersService.test.ts | 6 +-- .../services/super-admin/UsersService.test.ts | 10 ++--- 13 files changed, 42 insertions(+), 75 deletions(-) diff --git a/src/app/api/customer/DocumentsController.ts b/src/app/api/customer/DocumentsController.ts index 40713c15..ebfccb15 100644 --- a/src/app/api/customer/DocumentsController.ts +++ b/src/app/api/customer/DocumentsController.ts @@ -46,8 +46,7 @@ export default class DocumentsController extends ApiController { protected async post(req: Request, response: Response) { try { //init Document resource with request body values - const documentEntity = new Document(); - Document.hydrate(documentEntity, req.body); + const documentEntity = Document.hydrate(req.body); //validate document await validateOrReject(documentEntity, { groups: ["createDocument"] }); diff --git a/src/app/api/super-admin/CustomersController.ts b/src/app/api/super-admin/CustomersController.ts index 9e4a1732..0508eb1b 100644 --- a/src/app/api/super-admin/CustomersController.ts +++ b/src/app/api/super-admin/CustomersController.ts @@ -44,15 +44,12 @@ export default class CustomersController extends ApiController { protected async post(req: Request, response: Response) { try { //init IUser resource with request body values - const customerEntity = new Customer(); - Customer.hydrate(customerEntity, req.body); - + const customerEntity = Customer.hydrate(req.body); //validate user await validateOrReject(customerEntity, { groups: ["createCustomer"], forbidUnknownValues: false }); //call service to get prisma entity const prismaEntityCreated = await this.customersService.create(customerEntity); - //Hydrate ressource with prisma entity const customerEntityCreated = Customer.hydrate(prismaEntityCreated, { strategy: "excludeAll", diff --git a/src/app/api/super-admin/DeedTypesController.ts b/src/app/api/super-admin/DeedTypesController.ts index 1a5f1a0d..aba5721c 100644 --- a/src/app/api/super-admin/DeedTypesController.ts +++ b/src/app/api/super-admin/DeedTypesController.ts @@ -4,7 +4,6 @@ import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; import DeedTypesService from "@Services/super-admin/DeedTypesService/DeedTypesService"; import { DeedTypes } from "@prisma/client"; -import ObjectHydrate from "@Common/helpers/ObjectHydrate"; import { DeedType } from "le-coffre-resources/dist/SuperAdmin"; import { validateOrReject } from "class-validator"; @@ -46,8 +45,7 @@ export default class DeedTypesController extends ApiController { protected async post(req: Request, response: Response) { try { //init DeedType resource with request body values - const deedTypeEntity = new DeedType(); - ObjectHydrate.hydrate(deedTypeEntity, req.body); + const deedTypeEntity = DeedType.hydrate(req.body); //validate deed type await validateOrReject(deedTypeEntity, { groups: ["createDeedType"], forbidUnknownValues: false }); @@ -80,8 +78,7 @@ export default class DeedTypesController extends ApiController { throw new Error("No uid provided"); } //init DeedType resource with request body values - const deedTypeEntity = new DeedType(); - ObjectHydrate.hydrate(deedTypeEntity, req.body); + const deedTypeEntity = DeedType.hydrate(req.body); //validate deed type await validateOrReject(deedTypeEntity, { groups: ["updateDeedType"] }); diff --git a/src/app/api/super-admin/DocumentTypesController.ts b/src/app/api/super-admin/DocumentTypesController.ts index e5ebfa64..3ef79a93 100644 --- a/src/app/api/super-admin/DocumentTypesController.ts +++ b/src/app/api/super-admin/DocumentTypesController.ts @@ -7,7 +7,6 @@ import { DocumentTypes } from "@prisma/client"; import ObjectHydrate from "@Common/helpers/ObjectHydrate"; import { DocumentType } from "le-coffre-resources/dist/SuperAdmin"; import { validateOrReject } from "class-validator"; -//import { validateOrReject } from "class-validator"; @Controller() @Service() @@ -48,8 +47,7 @@ export default class DocumentTypesController extends ApiController { protected async post(req: Request, response: Response) { try { //init DocumentType resource with request body values - const documentTypeEntity = new DocumentType(); - ObjectHydrate.hydrate(documentTypeEntity, req.body); + const documentTypeEntity = DocumentType.hydrate(req.body); //validate user await validateOrReject(documentTypeEntity, { groups: ["createDocumentType"], forbidUnknownValues: false }); //call service to get prisma entity @@ -77,8 +75,7 @@ export default class DocumentTypesController extends ApiController { throw new Error("No uid provided"); } //init DocumentType resource with request body values - const documentTypeEntity = new DocumentType(); - ObjectHydrate.hydrate(documentTypeEntity, req.body); + const documentTypeEntity = DocumentType.hydrate(req.body); //validate user await validateOrReject(documentTypeEntity, { groups: ["update"] }); diff --git a/src/app/api/super-admin/DocumentsController.ts b/src/app/api/super-admin/DocumentsController.ts index 35dea328..cfe412ec 100644 --- a/src/app/api/super-admin/DocumentsController.ts +++ b/src/app/api/super-admin/DocumentsController.ts @@ -4,7 +4,6 @@ import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService"; import { Documents } from "@prisma/client"; -import ObjectHydrate from "@Common/helpers/ObjectHydrate"; import { Document } from "le-coffre-resources/dist/SuperAdmin"; import { validateOrReject } from "class-validator"; @@ -47,8 +46,7 @@ export default class DocumentsController extends ApiController { protected async post(req: Request, response: Response) { try { //init Document resource with request body values - const documentEntity = new Document(); - ObjectHydrate.hydrate(documentEntity, req.body); + const documentEntity = Document.hydrate(req.body); //validate document await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false }); @@ -81,8 +79,7 @@ export default class DocumentsController extends ApiController { } //init Document resource with request body values - const documentEntity = new Document(); - ObjectHydrate.hydrate(documentEntity, req.body); + const documentEntity = Document.hydrate(req.body); //validate document await validateOrReject(documentEntity, { groups: ["createDocument"] }); diff --git a/src/app/api/super-admin/OfficeFoldersController.ts b/src/app/api/super-admin/OfficeFoldersController.ts index 94265252..8ab10aaa 100644 --- a/src/app/api/super-admin/OfficeFoldersController.ts +++ b/src/app/api/super-admin/OfficeFoldersController.ts @@ -4,7 +4,6 @@ import ApiController from "@Common/system/controller-pattern/ApiController"; import OfficeFoldersService from "@Services/super-admin/OfficeFoldersService/OfficeFoldersService"; import { Service } from "typedi"; import { OfficeFolders } from "@prisma/client"; -import ObjectHydrate from "@Common/helpers/ObjectHydrate"; import { OfficeFolder } from "le-coffre-resources/dist/SuperAdmin"; import { validateOrReject } from "class-validator"; @@ -44,8 +43,7 @@ export default class OfficeFoldersController extends ApiController { protected async post(req: Request, response: Response) { try { //init OfficeFolder resource with request body values - const officeFolderEntity = new OfficeFolder(); - ObjectHydrate.hydrate(officeFolderEntity, req.body); + const officeFolderEntity = OfficeFolder.hydrate(req.body); //validate folder await validateOrReject(officeFolderEntity, { groups: ["create"] }); @@ -73,11 +71,10 @@ export default class OfficeFoldersController extends ApiController { if (!uid) { throw new Error("No uid provided"); } - //init IUser resource with request body values - const officeFolderEntity = new OfficeFolder(); - ObjectHydrate.hydrate(officeFolderEntity, req.body); + //init OfficeFolder resource with request body values + const officeFolderEntity = OfficeFolder.hydrate(req.body); - //validate user + //validate folder await validateOrReject(officeFolderEntity, { groups: ["updateFolder"], forbidUnknownValues: false }); //call service to get prisma entity diff --git a/src/app/api/super-admin/OfficesController.ts b/src/app/api/super-admin/OfficesController.ts index 05b3af1b..8520fe49 100644 --- a/src/app/api/super-admin/OfficesController.ts +++ b/src/app/api/super-admin/OfficesController.ts @@ -4,7 +4,6 @@ import ApiController from "@Common/system/controller-pattern/ApiController"; import OfficesService from "@Services/super-admin/OfficesService/OfficesService"; import { Service } from "typedi"; import { Offices } from "@prisma/client"; -import ObjectHydrate from "@Common/helpers/ObjectHydrate"; import { Office as OfficeResource } from "le-coffre-resources/dist/SuperAdmin"; import { validateOrReject } from "class-validator"; @@ -40,8 +39,7 @@ export default class OfficesController extends ApiController { protected async post(req: Request, response: Response) { try { //init IUser resource with request body values - const officeEntity = new OfficeResource(); - ObjectHydrate.hydrate(officeEntity, req.body); + const officeEntity = OfficeResource.hydrate(req.body); //validate user await validateOrReject(officeEntity, { groups: ["createOffice"], forbidUnknownValues: false }); //call service to get prisma entity @@ -68,8 +66,7 @@ export default class OfficesController extends ApiController { throw new Error("No uid provided"); } //init IUser resource with request body values - const officeEntity = new OfficeResource(); - ObjectHydrate.hydrate(officeEntity, req.body); + const officeEntity = OfficeResource.hydrate(req.body); //validate user await validateOrReject(officeEntity, { groups: ["update"] }); //call service to get prisma entity diff --git a/src/app/api/super-admin/UsersController.ts b/src/app/api/super-admin/UsersController.ts index 13b2c8c3..aee392e8 100644 --- a/src/app/api/super-admin/UsersController.ts +++ b/src/app/api/super-admin/UsersController.ts @@ -3,7 +3,6 @@ import { Controller, Get, Post, Put } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import UsersService from "@Services/super-admin/UsersService/UsersService"; import { Service } from "typedi"; -import ObjectHydrate from "@Common/helpers/ObjectHydrate"; import { validateOrReject } from "class-validator"; import User from "le-coffre-resources/dist/Notary"; import { Users } from "@prisma/client"; @@ -45,8 +44,7 @@ export default class UsersController extends ApiController { protected async getAddresses(req: Request, response: Response) { try { //init IUser resource with request body values - const userEntity = new User(); - ObjectHydrate.hydrate(userEntity, req.body); + const userEntity = User.hydrate(req.body); //validate user await validateOrReject(userEntity, { groups: ["createUser"] }); @@ -78,8 +76,7 @@ export default class UsersController extends ApiController { throw new Error("No uid provided"); } //init IUser resource with request body values - const userEntity = new User(); - ObjectHydrate.hydrate(userEntity, req.body); + const userEntity = User.hydrate(req.body); //validate user await validateOrReject(userEntity, { groups: ["update"] }); diff --git a/src/common/databases/migrations/20230426144239_init/migration.sql b/src/common/databases/migrations/20230426144239_init/migration.sql index 5244e927..d017a9c4 100644 --- a/src/common/databases/migrations/20230426144239_init/migration.sql +++ b/src/common/databases/migrations/20230426144239_init/migration.sql @@ -444,3 +444,6 @@ ALTER TABLE "deed_type_has_document_types" ADD CONSTRAINT "deed_type_has_documen -- AddForeignKey ALTER TABLE "deed_type_has_document_types" ADD CONSTRAINT "deed_type_has_document_types_deed_type_uid_fkey" FOREIGN KEY ("deed_type_uid") REFERENCES "deed_types"("uid") ON DELETE CASCADE ON UPDATE CASCADE; +-- AlterTable +ALTER TABLE "contacts" ALTER COLUMN "cell_phone_number" SET NOT NULL, +ALTER COLUMN "address_uid" DROP NOT NULL; \ No newline at end of file diff --git a/src/common/databases/schema.prisma b/src/common/databases/schema.prisma index bace5ed4..08317b33 100644 --- a/src/common/databases/schema.prisma +++ b/src/common/databases/schema.prisma @@ -39,7 +39,7 @@ model Contacts { cell_phone_number String @unique @db.VarChar(50) civility ECivility @default(MALE) address Addresses? @relation(fields: [address_uid], references: [uid], onDelete: Cascade) - address_uid String @unique @db.VarChar(255) + address_uid String? @unique @db.VarChar(255) birthdate DateTime? created_at DateTime? @default(now()) updated_at DateTime? @updatedAt diff --git a/src/common/repositories/CustomersRepository.ts b/src/common/repositories/CustomersRepository.ts index ddca2087..4f846795 100644 --- a/src/common/repositories/CustomersRepository.ts +++ b/src/common/repositories/CustomersRepository.ts @@ -32,11 +32,7 @@ export default class CustomersRepository extends BaseRepository { /** * @description : Create a customer */ - public async create(customer: Customer): Promise< - Customers & { - contact: Contacts; - } - > { + public async create(customer: Customer): Promise { const createArgs: Prisma.CustomersCreateArgs = { data: { status: ECustomerStatus.PENDING, @@ -46,7 +42,7 @@ export default class CustomersRepository extends BaseRepository { last_name: customer.contact!.last_name, email: customer.contact!.email, phone_number: customer.contact!.phone_number, - cell_phone_number: customer.contact!?.cell_phone_number, + cell_phone_number: customer.contact!.cell_phone_number, civility: ECivility[customer.contact!.civility as keyof typeof ECivility], address: {}, }, @@ -55,10 +51,12 @@ export default class CustomersRepository extends BaseRepository { }; if (customer.contact!.address) { - createArgs.data.contact!.create!.address!.create = { - address: customer.contact!.address!.address, - zip_code: customer.contact!.address!.zip_code, - city: customer.contact!.address!.city, + createArgs.data.contact!.create!.address = { + create: { + address: customer.contact!.address!.address, + zip_code: customer.contact!.address!.zip_code, + city: customer.contact!.address!.city, + }, }; } return this.model.create({ ...createArgs, include: { contact: true } }); @@ -67,14 +65,7 @@ export default class CustomersRepository extends BaseRepository { /** * @description : Update data from a customer */ - public async update( - uid: string, - customer: Customer, - ): Promise< - Customers & { - contact: Contacts; - } - > { + public async update(uid: string, customer: Customer): Promise { const updateArgs: Prisma.CustomersUpdateArgs = { where: { uid: uid, @@ -107,21 +98,16 @@ export default class CustomersRepository extends BaseRepository { /** * @description : Find unique customer */ - public async findOneByUid( - uid: string, - query?: any, - ): Promise< - Customers & { - contact: Contacts; - } - > { + public async findOneByUid(uid: string, query?: any): Promise { const findOneArgs: Prisma.CustomersFindUniqueArgs = { where: { uid: uid, }, }; - const customerEntity = await this.model.findUnique({ ...findOneArgs, include: { contact: true } }); - + if (query) { + findOneArgs.include = query; + } + const customerEntity = await this.model.findUnique(findOneArgs); if (!customerEntity) { throw new Error("Customer not found"); } diff --git a/src/test/services/super-admin/CustomersService.test.ts b/src/test/services/super-admin/CustomersService.test.ts index 84172325..7ea5944c 100644 --- a/src/test/services/super-admin/CustomersService.test.ts +++ b/src/test/services/super-admin/CustomersService.test.ts @@ -37,7 +37,7 @@ describe("test create function", () => { expect(contactCreated?.email).toEqual(customer.contact!.email); // verify if customer address is created in db - const addressForContactCreated = await prisma.addresses.findUnique({ where: { uid: contactCreated?.address_uid } }); + const addressForContactCreated = await prisma.addresses.findUnique({ where: { uid: contactCreated?.address_uid! } }); expect(addressForContactCreated?.address).toEqual(customer.contact!.address?.address); expect(addressForContactCreated?.zip_code).toEqual(customer.contact!.address?.zip_code); expect(addressForContactCreated?.city).toEqual(customer.contact!.address?.city); @@ -92,7 +92,7 @@ describe("test create function", () => { expect(contactCreated?.email).toEqual(customer_.contact!.email); // verify if customer_ address is created in db - const addressForContactCreated = await prisma.addresses.findUnique({ where: { uid: contactCreated?.address_uid } }); + const addressForContactCreated = await prisma.addresses.findUnique({ where: { uid: contactCreated?.address_uid! } }); expect(addressForContactCreated?.address).toEqual(customer.contact!.address?.address); expect(addressForContactCreated?.zip_code).toEqual(customer.contact!.address?.zip_code); expect(addressForContactCreated?.city).toEqual(customer.contact!.address?.city); @@ -118,7 +118,7 @@ describe("test update function", () => { expect(existingContact?.email).toEqual(customer_.contact!.email); // verify if customer_ address is created in db - const addressForExistingContact = await prisma.addresses.findUnique({ where: { uid: existingContact?.address_uid } }); + const addressForExistingContact = await prisma.addresses.findUnique({ where: { uid: existingContact?.address_uid! } }); expect(addressForExistingContact?.address).toEqual(customer_.contact!.address?.address); expect(addressForExistingContact?.zip_code).toEqual(customer_.contact!.address?.zip_code); expect(addressForExistingContact?.city).toEqual(customer_.contact!.address?.city); diff --git a/src/test/services/super-admin/UsersService.test.ts b/src/test/services/super-admin/UsersService.test.ts index a6255c4c..5b7d6708 100644 --- a/src/test/services/super-admin/UsersService.test.ts +++ b/src/test/services/super-admin/UsersService.test.ts @@ -37,7 +37,7 @@ describe("test create function", () => { expect(contactCreated?.email).toEqual(user.contact!.email); // verify if user address is created in db - const addressForContactCreated = await prisma.addresses.findUnique({ where: { uid: contactCreated?.address_uid } }); + const addressForContactCreated = await prisma.addresses.findUnique({ where: { uid: contactCreated?.address_uid! } }); expect(addressForContactCreated?.address).toEqual(user.contact!.address?.address); expect(addressForContactCreated?.zip_code).toEqual(user.contact!.address?.zip_code); expect(addressForContactCreated?.city).toEqual(user.contact!.address?.city); @@ -50,7 +50,7 @@ describe("test create function", () => { expect(officeCreated?.office_status).toEqual("DESACTIVATED"); // verify if user office's address is created in db - const addressForOfficeCreated = await prisma.addresses.findUnique({ where: { uid: officeCreated?.address_uid } }); + const addressForOfficeCreated = await prisma.addresses.findUnique({ where: { uid: officeCreated?.address_uid! } }); expect(addressForOfficeCreated?.address).toEqual(user.office_membership!.address!.address); expect(addressForOfficeCreated?.zip_code).toEqual(user.office_membership!.address!.zip_code); expect(addressForOfficeCreated?.city).toEqual(user.office_membership!.address!.city); @@ -106,7 +106,7 @@ describe("test create function", () => { expect(contactCreated?.email).toEqual(user_.contact!.email); // verify if user_ address is created in db - const addressForContactCreated = await prisma.addresses.findUnique({ where: { uid: contactCreated?.address_uid } }); + const addressForContactCreated = await prisma.addresses.findUnique({ where: { uid: contactCreated?.address_uid! } }); expect(addressForContactCreated?.address).toEqual(user.contact!.address?.address); expect(addressForContactCreated?.zip_code).toEqual(user.contact!.address?.zip_code); expect(addressForContactCreated?.city).toEqual(user.contact!.address?.city); @@ -145,7 +145,7 @@ describe("test update function", () => { expect(existingContact?.email).toEqual(user_.contact!.email); // verify if user_ address is created in db - const addressForExistingContact = await prisma.addresses.findUnique({ where: { uid: existingContact?.address_uid } }); + const addressForExistingContact = await prisma.addresses.findUnique({ where: { uid: existingContact?.address_uid! } }); expect(addressForExistingContact?.address).toEqual(user_.contact!.address?.address); expect(addressForExistingContact?.zip_code).toEqual(user_.contact!.address?.zip_code); expect(addressForExistingContact?.city).toEqual(user_.contact!.address?.city); @@ -158,7 +158,7 @@ describe("test update function", () => { expect(officeCreated?.office_status).toEqual("DESACTIVATED"); // verify is user_ office's address is created in db - const addressForOfficeCreated = await prisma.addresses.findUnique({ where: { uid: officeCreated?.address_uid } }); + const addressForOfficeCreated = await prisma.addresses.findUnique({ where: { uid: officeCreated?.address_uid! } }); expect(addressForOfficeCreated?.address).toEqual(user_.office_membership!.address!.address); expect(addressForOfficeCreated?.zip_code).toEqual(user_.office_membership!.address!.zip_code); expect(addressForOfficeCreated?.city).toEqual(user_.office_membership!.address!.city);