refacto hydratation on post requests
This commit is contained in:
parent
22d0239306
commit
ec98f3ac26
@ -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<Document>(req.body);
|
||||
|
||||
//validate document
|
||||
await validateOrReject(documentEntity, { groups: ["createDocument"] });
|
||||
|
@ -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<Customer>(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<Customer>(prismaEntityCreated, {
|
||||
strategy: "excludeAll",
|
||||
|
@ -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<DeedType>(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<DeedType>(req.body);
|
||||
|
||||
//validate deed type
|
||||
await validateOrReject(deedTypeEntity, { groups: ["updateDeedType"] });
|
||||
|
@ -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<DocumentType>(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<DocumentType>(req.body);
|
||||
|
||||
//validate user
|
||||
await validateOrReject(documentTypeEntity, { groups: ["update"] });
|
||||
|
@ -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<Document>(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<Document>(req.body);
|
||||
|
||||
//validate document
|
||||
await validateOrReject(documentEntity, { groups: ["createDocument"] });
|
||||
|
@ -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<OfficeFolder>(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<OfficeFolder>(req.body);
|
||||
|
||||
//validate user
|
||||
//validate folder
|
||||
await validateOrReject(officeFolderEntity, { groups: ["updateFolder"], forbidUnknownValues: false });
|
||||
|
||||
//call service to get prisma entity
|
||||
|
@ -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<OfficeResource>(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<OfficeResource>(req.body);
|
||||
//validate user
|
||||
await validateOrReject(officeEntity, { groups: ["update"] });
|
||||
//call service to get prisma entity
|
||||
|
@ -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<User>(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<User>(req.body);
|
||||
|
||||
//validate user
|
||||
await validateOrReject(userEntity, { groups: ["update"] });
|
||||
|
@ -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;
|
@ -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
|
||||
|
@ -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<Customers> {
|
||||
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<Customers> {
|
||||
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<Customers> {
|
||||
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");
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user