Feature/services tests (#21)

This commit is contained in:
Arnaud D. Natali 2023-04-28 10:52:43 +02:00 committed by GitHub
commit 806e15f2db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 868 additions and 836 deletions

View File

@ -1,4 +1,7 @@
DATABASE_URL="postgresql://prisma:prisma@localhost:5433/tests" DATABASE_URL="postgresql://prisma:prisma@localhost:5433/tests"
POSTGRES_USER=prisma DATABASE_PORT="5433"
POSTGRES_PASSWORD=prisma DATABASE_USER="prisma"
POSTGRES_DB=tests DATABASE_PASSWORD="prisma"
DATABASE_NAME="tests"
DATABASE_HOSTNAME="localhost"
DEV_PRISMA_STUDIO_DB_URL="postgresql://prisma:prisma@localhost:5433/tests"

View File

@ -25,11 +25,12 @@
"api:dev": "nodemon", "api:dev": "nodemon",
"build:test": "tsc && mocha ./dist/entries/Test.js", "build:test": "tsc && mocha ./dist/entries/Test.js",
"format": "prettier --write src", "format": "prettier --write src",
"migrate:test": "dotenv -e .env.test -- npx prisma migrate deploy",
"migrate": "npx prisma migrate deploy", "migrate": "npx prisma migrate deploy",
"docker:up": "docker-compose up -d", "docker:up": "docker-compose up -d",
"docker:up:test": "docker-compose -f docker-compose-test.yml up -d", "docker:up:test": "docker-compose -f docker-compose-test.yml up -d",
"docker:down": "docker-compose -f docker-compose-test.yml down", "docker:down": "docker-compose -f docker-compose-test.yml down",
"test": "tsc && npm run docker:up:test && npm run migrate && jest -i --verbose ./dist/test/* && npm run docker:down" "test": "tsc && npm run docker:up:test && npm run migrate:test && dotenv -e .env.test -- jest -i --verbose ./dist/test/* && npm run docker:down"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -47,10 +48,9 @@
"class-validator": "^0.14.0", "class-validator": "^0.14.0",
"classnames": "^2.3.2", "classnames": "^2.3.2",
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2", "express": "^4.18.2",
"jsonwebtoken": "^9.0.0", "jsonwebtoken": "^9.0.0",
"le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.21", "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.24",
"module-alias": "^2.2.2", "module-alias": "^2.2.2",
"next": "^13.1.5", "next": "^13.1.5",
"node-cache": "^5.1.2", "node-cache": "^5.1.2",
@ -71,6 +71,7 @@
"@types/node": "^18.11.18", "@types/node": "^18.11.18",
"@types/node-schedule": "^2.1.0", "@types/node-schedule": "^2.1.0",
"@types/uuid": "^9.0.0", "@types/uuid": "^9.0.0",
"dotenv": "^16.0.3",
"jest": "^29.5.0", "jest": "^29.5.0",
"nodemon": "^2.0.20", "nodemon": "^2.0.20",
"prettier": "2.8.4", "prettier": "2.8.4",

View File

@ -137,6 +137,7 @@ model OfficeFolders {
office_folder_has_stakeholder OfficeFolderHasStakeholders[] office_folder_has_stakeholder OfficeFolderHasStakeholders[]
documents Documents[] documents Documents[]
@@unique([folder_number, office_uuid])
@@map("office_folders") @@map("office_folders")
} }
@ -149,7 +150,7 @@ model OfficeFolderHasCustomers {
created_at DateTime? @default(now()) created_at DateTime? @default(now())
updated_at DateTime? @updatedAt updated_at DateTime? @updatedAt
@@unique([customer_uuid, office_folder_uuid]) @@unique([office_folder_uuid, customer_uuid])
@@map("office_folder_has_customers") @@map("office_folder_has_customers")
} }

View File

@ -1,7 +1,7 @@
import Database from "@Common/databases/database"; import Database from "@Common/databases/database";
import BaseRepository from "@Repositories/BaseRepository"; import BaseRepository from "@Repositories/BaseRepository";
import { Service } from "typedi"; import { Service } from "typedi";
import { Customers, ECivility, ECustomerStatus } from "@prisma/client"; import { Customers, ECivility, ECustomerStatus, Prisma } from "@prisma/client";
import { Customer } from "le-coffre-resources/dist/SuperAdmin"; import { Customer } from "le-coffre-resources/dist/SuperAdmin";
@Service() @Service()
@ -28,7 +28,7 @@ export default class CustomersRepository extends BaseRepository {
* @description : Create a customer * @description : Create a customer
*/ */
public async create(customer: Customer): Promise<Customers> { public async create(customer: Customer): Promise<Customers> {
return this.model.create({ const createArgs: Prisma.CustomersCreateArgs = {
data: { data: {
status: ECustomerStatus.PENDING, status: ECustomerStatus.PENDING,
contact: { contact: {
@ -39,24 +39,27 @@ export default class CustomersRepository extends BaseRepository {
phone_number: customer.contact.phone_number, 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], civility: ECivility[customer.contact.civility as keyof typeof ECivility],
address: { address: {}
create: {
address: customer.contact.address?.address,
zip_code: customer.contact.address.zip_code,
city: customer.contact.address.city,
}, },
}, },
}, },
}, };
},
}); 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,
};
}
return this.model.create(createArgs);
} }
/** /**
* @description : Update data from a customer * @description : Update data from a customer
*/ */
public async update(uid: string, customer: Customer): Promise<Customers> { public async update(uid: string, customer: Customer): Promise<Customers> {
return this.model.update({ const updateArgs: Prisma.CustomersUpdateArgs = {
where: { where: {
uuid: uid, uuid: uid,
}, },
@ -70,17 +73,19 @@ export default class CustomersRepository extends BaseRepository {
phone_number: customer.contact.phone_number, 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], civility: ECivility[customer.contact.civility as keyof typeof ECivility],
address: { address: {}
update: {
address: customer.contact.address.address,
zip_code: customer.contact.address.zip_code,
city: customer.contact.address.city,
}, },
}, },
}, },
}, }
}, if (customer.contact.address) {
}); updateArgs.data.contact!.update!.address!.update = {
address: customer.contact.address!.address,
zip_code: customer.contact.address!.zip_code,
city: customer.contact.address!.city,
};
}
return this.model.update(updateArgs);
} }
/** /**

View File

@ -28,7 +28,7 @@ export default class DeedTypesRepository extends BaseRepository {
* @description : Create new deed type * @description : Create new deed type
*/ */
public async create(deedType: DeedType): Promise<DeedTypes> { public async create(deedType: DeedType): Promise<DeedTypes> {
return this.model.create({ const createArgs: Prisma.DeedTypesCreateArgs = {
data: { data: {
name: deedType.name, name: deedType.name,
description: deedType.description, description: deedType.description,
@ -37,8 +37,19 @@ export default class DeedTypesRepository extends BaseRepository {
uuid: deedType.office.uid, uuid: deedType.office.uid,
}, },
}, },
}
};
if (deedType.deed_type_has_document_types) {
createArgs.data.deed_type_has_document_types = {
createMany: {
data: deedType.deed_type_has_document_types.map((relation) => ({
document_type_uuid: relation.document_type.uid!,
})),
skipDuplicates: true,
}, },
}); };
}
return this.model.create(createArgs);
} }
/** /**
@ -68,7 +79,7 @@ export default class DeedTypesRepository extends BaseRepository {
deleteMany: { deed_type_uuid: uid }, deleteMany: { deed_type_uuid: uid },
createMany: { createMany: {
data: deedType.deed_type_has_document_types.map((relation) => ({ data: deedType.deed_type_has_document_types.map((relation) => ({
document_type_uuid: relation.document_type.uid, document_type_uuid: relation.document_type.uid!,
})), })),
skipDuplicates: true, skipDuplicates: true,
}, },

View File

@ -28,7 +28,7 @@ export default class DeedsRepository extends BaseRepository {
* @description : Create a deed based on a deed type * @description : Create a deed based on a deed type
*/ */
public async create(deed: Deed): Promise<Deeds> { public async create(deed: Deed): Promise<Deeds> {
return this.model.create({ const createArgs: Prisma.DeedsCreateArgs = {
data: { data: {
deed_type: { deed_type: {
connect: { connect: {
@ -36,7 +36,27 @@ export default class DeedsRepository extends BaseRepository {
}, },
}, },
}, },
};
const deedTypeWithDocumentTypes = await this.instanceDb.deedTypes.findUniqueOrThrow({
where: {
uuid: deed.deed_type.uid,
},
include: { deed_type_has_document_types: true },
}); });
if (deedTypeWithDocumentTypes.archived_at) throw new Error("deed type is archived");
if (deedTypeWithDocumentTypes.deed_type_has_document_types) {
createArgs.data.deed_has_document_types = {
createMany: {
data: deedTypeWithDocumentTypes.deed_type_has_document_types.map((relation) => ({
document_type_uuid: relation.document_type_uuid,
})),
skipDuplicates: true,
},
};
}
return this.model.create(createArgs);
} }
/** /**
@ -57,7 +77,7 @@ export default class DeedsRepository extends BaseRepository {
deleteMany: { deed_uuid: uid }, deleteMany: { deed_uuid: uid },
createMany: { createMany: {
data: deed.deed_has_document_types.map((relation) => ({ data: deed.deed_has_document_types.map((relation) => ({
document_type_uuid: relation.document_type.uid, document_type_uuid: relation.document_type.uid!,
})), })),
skipDuplicates: true, skipDuplicates: true,
}, },

View File

@ -67,9 +67,9 @@ export default class DocumentsRepository extends BaseRepository {
public async createMany(documents: Document[]): Promise<Prisma.BatchPayload> { public async createMany(documents: Document[]): Promise<Prisma.BatchPayload> {
return this.model.createMany({ return this.model.createMany({
data: documents.map((document) => ({ data: documents.map((document) => ({
folder_uuid: document.folder.uid, folder_uuid: document.folder.uid!,
depositor_uuid: document.depositor.uid, depositor_uuid: document.depositor.uid!,
document_type_uuid: document.document_type.uid, document_type_uuid: document.document_type.uid!,
})), })),
skipDuplicates: true, skipDuplicates: true,
}); });

View File

@ -25,7 +25,7 @@ export default class OfficeFoldersRepository extends BaseRepository {
} }
/** /**
* @description : Create new office folder without customers nor stakeholders * @description : Create new office folder with stakeholders
*/ */
public async create(officeFolder: OfficeFolder): Promise<OfficeFolders> { public async create(officeFolder: OfficeFolder): Promise<OfficeFolders> {
const createArgs: Prisma.OfficeFoldersCreateArgs = { const createArgs: Prisma.OfficeFoldersCreateArgs = {
@ -57,7 +57,7 @@ export default class OfficeFoldersRepository extends BaseRepository {
createArgs.data.office_folder_has_stakeholder = { createArgs.data.office_folder_has_stakeholder = {
createMany: { createMany: {
data: officeFolder.office_folder_has_stakeholder.map((relation) => ({ data: officeFolder.office_folder_has_stakeholder.map((relation) => ({
user_stakeholder_uuid: relation.user_stakeholder.uid, user_stakeholder_uuid: relation.user_stakeholder.uid!,
})), })),
skipDuplicates: true skipDuplicates: true
}, },
@ -92,7 +92,7 @@ export default class OfficeFoldersRepository extends BaseRepository {
deleteMany: { office_folder_uuid: officeFolderUuid }, deleteMany: { office_folder_uuid: officeFolderUuid },
createMany: { createMany: {
data: officeFolder.office_folder_has_stakeholder.map((relation) => ({ data: officeFolder.office_folder_has_stakeholder.map((relation) => ({
user_stakeholder_uuid: relation.user_stakeholder.uid, user_stakeholder_uuid: relation.user_stakeholder.uid!,
})), })),
skipDuplicates: true skipDuplicates: true
}, },
@ -103,7 +103,7 @@ export default class OfficeFoldersRepository extends BaseRepository {
deleteMany: { office_folder_uuid: officeFolderUuid }, deleteMany: { office_folder_uuid: officeFolderUuid },
createMany: { createMany: {
data: officeFolder.office_folder_has_customers.map((relation) => ({ data: officeFolder.office_folder_has_customers.map((relation) => ({
customer_uuid: relation.customer.uid, customer_uuid: relation.customer.uid!,
})), })),
skipDuplicates: true skipDuplicates: true
}, },
@ -113,8 +113,8 @@ export default class OfficeFoldersRepository extends BaseRepository {
updateArgs.data.documents = { updateArgs.data.documents = {
createMany: { createMany: {
data: officeFolder.documents.map((relation) => ({ data: officeFolder.documents.map((relation) => ({
document_type_uuid: relation.document_type.uid, document_type_uuid: relation.document_type.uid!,
depositor_uuid: relation.depositor.uid depositor_uuid: relation.depositor.uid!
})), })),
skipDuplicates: true skipDuplicates: true
}, },

View File

@ -1,7 +1,7 @@
import Database from "@Common/databases/database"; import Database from "@Common/databases/database";
import BaseRepository from "@Repositories/BaseRepository"; import BaseRepository from "@Repositories/BaseRepository";
import { Service } from "typedi"; import { Service } from "typedi";
import { ECivility, Users } from "@prisma/client"; import { ECivility, Prisma, Users } from "@prisma/client";
import User from "le-coffre-resources/dist/SuperAdmin"; import User from "le-coffre-resources/dist/SuperAdmin";
@Service() @Service()
@ -28,7 +28,7 @@ export default class UsersRepository extends BaseRepository {
* @description : Create a user * @description : Create a user
*/ */
public async create(user: User): Promise<Users> { public async create(user: User): Promise<Users> {
return this.model.create({ const createArgs: Prisma.UsersCreateArgs = {
data: { data: {
idNot: user.idNot, idNot: user.idNot,
office_membership: { office_membership: {
@ -58,24 +58,26 @@ export default class UsersRepository extends BaseRepository {
phone_number: user.contact.phone_number, phone_number: user.contact.phone_number,
cell_phone_number: user.contact.cell_phone_number, cell_phone_number: user.contact.cell_phone_number,
civility: ECivility[user.contact.civility as keyof typeof ECivility], civility: ECivility[user.contact.civility as keyof typeof ECivility],
address: { address: {},
create: {
address: user.contact.address.address,
zip_code: user.contact.address.zip_code,
city: user.contact.address.city,
}, },
}, },
}, },
}, }
}, if (user.contact.address) {
}); createArgs.data.contact!.create!.address!.create = {
address: user.contact.address!.address,
zip_code: user.contact.address!.zip_code,
city: user.contact.address!.city,
};
}
return this.model.create(createArgs);
} }
/** /**
* @description : Update data from a user * @description : Update data from a user
*/ */
public async update(uid: string, user: User): Promise<Users> { public async update(uid: string, user: User): Promise<Users> {
return this.model.update({ const updateArgs: Prisma.UsersUpdateArgs = {
where: { where: {
uuid: uid, uuid: uid,
}, },
@ -108,17 +110,19 @@ export default class UsersRepository extends BaseRepository {
phone_number: user.contact.phone_number, phone_number: user.contact.phone_number,
cell_phone_number: user.contact.cell_phone_number, cell_phone_number: user.contact.cell_phone_number,
civility: ECivility[user.contact.civility as keyof typeof ECivility], civility: ECivility[user.contact.civility as keyof typeof ECivility],
address: { address: {}
update: {
address: user.contact.address.address,
zip_code: user.contact.address.zip_code,
city: user.contact.address.city,
}, },
}, },
}, },
}, };
}, if (user.contact.address) {
}); updateArgs.data.contact!.update!.address!.update = {
address: user.contact.address!.address,
zip_code: user.contact.address!.zip_code,
city: user.contact.address!.city,
};
}
return this.model.update(updateArgs);
} }
/** /**

View File

@ -3,12 +3,14 @@ import OfficeFoldersRepository from "@Repositories/OfficeFoldersRepository";
import BaseService from "@Services/BaseService"; import BaseService from "@Services/BaseService";
import { OfficeFolder } from "le-coffre-resources/dist/SuperAdmin"; import { OfficeFolder } from "le-coffre-resources/dist/SuperAdmin";
import { Service } from "typedi"; import { Service } from "typedi";
import DeedTypesService from "../DeedTypesService/DeedTypesService";
@Service() @Service()
export default class OfficeFoldersService extends BaseService { export default class OfficeFoldersService extends BaseService {
constructor( constructor(
private officeFoldersRepository: OfficeFoldersRepository private officeFoldersRepository: OfficeFoldersRepository,
private deedTypeService: DeedTypesService
) { ) {
super(); super();
} }
@ -26,6 +28,8 @@ export default class OfficeFoldersService extends BaseService {
* @throws {Error} If folder cannot be created * @throws {Error} If folder cannot be created
*/ */
public async create(officeFolderEntity: OfficeFolder): Promise<OfficeFolders> { public async create(officeFolderEntity: OfficeFolder): Promise<OfficeFolders> {
const deedType = await this.deedTypeService.getByUid(officeFolderEntity.deed.deed_type.uid!);
if(deedType.archived_at) throw new Error('deed type is archived');
return this.officeFoldersRepository.create(officeFolderEntity); return this.officeFoldersRepository.create(officeFolderEntity);
} }

121
src/test/config/Init.ts Normal file
View File

@ -0,0 +1,121 @@
import { Customers, DeedTypes, DocumentTypes, ECivility, ECustomerStatus, Offices, PrismaClient, Users } from "@prisma/client";
import User, { Customer, DeedType, DocumentType, Office } from "le-coffre-resources/dist/SuperAdmin";
const prisma = new PrismaClient();
export const initOffice = (office: Office): Promise<Offices> => {
return prisma.offices.create({
data: {
idNot: office.idNot,
name: office.name,
crpcen: office.crpcen,
address: {
create: {
address: office.address.address,
zip_code: office.address.zip_code,
city: office.address.city,
},
},
},
});
};
export const initDocumentType = (documentType: DocumentType, office: Office): Promise<DocumentTypes> => {
return prisma.documentTypes.create({
data: {
name: documentType.name,
public_description: documentType.public_description,
private_description: documentType.private_description,
archived_at: null,
office_uuid: office.uid!,
},
});
};
export const initDeedType = (deedType: DeedType, office: Office, documentTypes?: string[]): Promise<DeedTypes> => {
return prisma.deedTypes.create({
data: {
name: deedType.name,
description: deedType.description,
archived_at: null,
office_uuid: office.uid!,
deed_type_has_document_types: {
createMany: {
data: documentTypes!.map((documentType) => ({
document_type_uuid: documentType,
})),
skipDuplicates: true,
},
},
},
});
};
export const initCustomers = (customer: Customer): Promise<Customers> => {
return prisma.customers.create({
data: {
status: ECustomerStatus.PENDING,
contact: {
create: {
first_name: customer.contact.first_name,
last_name: customer.contact.last_name,
email: customer.contact.email,
phone_number: customer.contact.phone_number,
cell_phone_number: customer.contact?.cell_phone_number,
civility: ECivility[customer.contact.civility as keyof typeof ECivility],
address: {
create: {
address: customer.contact.address!.address,
zip_code: customer.contact.address!.zip_code,
city: customer.contact.address!.city,
},
},
},
},
},
});
};
export const initUsers = (user: User): Promise<Users> => {
return prisma.users.create({
data: {
idNot: user.idNot,
office_membership: {
connectOrCreate: {
where: {
idNot: user.office_membership.idNot,
},
create: {
idNot: user.office_membership.idNot,
name: user.office_membership.name,
crpcen: user.office_membership.crpcen,
address: {
create: {
address: user.office_membership.address.address,
zip_code: user.office_membership.address.zip_code,
city: user.office_membership.address.city,
},
},
},
},
},
contact: {
create: {
first_name: user.contact.first_name,
last_name: user.contact.last_name,
email: user.contact.email,
phone_number: user.contact.phone_number,
cell_phone_number: user.contact.cell_phone_number,
civility: ECivility[user.contact.civility as keyof typeof ECivility],
address: {
create: {
address: user.contact.address!.address,
zip_code: user.contact.address!.zip_code,
city: user.contact.address!.city,
},
},
},
},
},
});
};

View File

@ -2,7 +2,6 @@ import { EOfficeStatus } from "le-coffre-resources/dist/Customer/Office";
import User, { Address, Contact, Office, DeedType, DocumentType, Customer, OfficeFolder, Deed } from "le-coffre-resources/dist/SuperAdmin"; import User, { Address, Contact, Office, DeedType, DocumentType, Customer, OfficeFolder, Deed } from "le-coffre-resources/dist/SuperAdmin";
export const userAddress: Address = { export const userAddress: Address = {
uid: "",
address: "1 avenue des champs élysées", address: "1 avenue des champs élysées",
zip_code: 75008, zip_code: 75008,
city: "paris", city: "paris",
@ -11,7 +10,6 @@ export const userAddress: Address = {
}; };
export const userAddress_: Address = { export const userAddress_: Address = {
uid: "",
address: "1 rue Victor Hugo", address: "1 rue Victor Hugo",
zip_code: 75001, zip_code: 75001,
city: "paris", city: "paris",
@ -20,7 +18,6 @@ export const userAddress_: Address = {
}; };
export const userContact: Contact = { export const userContact: Contact = {
uid: "",
first_name: "Philippe", first_name: "Philippe",
last_name: "le Bel", last_name: "le Bel",
address: userAddress, address: userAddress,
@ -33,7 +30,6 @@ export const userContact: Contact = {
}; };
export const userContact_: Contact = { export const userContact_: Contact = {
uid: "",
first_name: "Saint", first_name: "Saint",
last_name: "Louise", last_name: "Louise",
address: userAddress_, address: userAddress_,
@ -45,8 +41,31 @@ export const userContact_: Contact = {
updated_at: null, updated_at: null,
}; };
export const customerContact: Contact = {
first_name: "John",
last_name: "Doe",
address: userAddress,
email: "john.doe@customer.fr",
phone_number: "+3313847505",
cell_phone_number: "+3313847505",
civility: "MALE",
created_at: null,
updated_at: null,
};
export const customerContact_: Contact = {
first_name: "Jocelyne",
last_name: "Doe",
address: userAddress,
email: "jocelyne.doe@customer.fr",
phone_number: "+331384894505",
cell_phone_number: "+331384894505",
civility: "FEMALE",
created_at: null,
updated_at: null,
};
export const officeAddress: Address = { export const officeAddress: Address = {
uid: "",
address: "1 rue Rivoli", address: "1 rue Rivoli",
zip_code: 75001, zip_code: 75001,
city: "paris", city: "paris",
@ -55,7 +74,6 @@ export const officeAddress: Address = {
}; };
export const officeAddress_: Address = { export const officeAddress_: Address = {
uid: "",
address: "1 rue de la paix", address: "1 rue de la paix",
zip_code: 75008, zip_code: 75008,
city: "paris", city: "paris",
@ -64,7 +82,6 @@ export const officeAddress_: Address = {
}; };
export const office: Office = { export const office: Office = {
uid: "",
idNot: "123456789", idNot: "123456789",
name: "first office", name: "first office",
crpcen: "0123456789CRPCEN", crpcen: "0123456789CRPCEN",
@ -75,7 +92,6 @@ export const office: Office = {
}; };
export const office_: Office = { export const office_: Office = {
uid: "",
idNot: "789101112", idNot: "789101112",
name: "second office", name: "second office",
crpcen: "987654321CRPCEN", crpcen: "987654321CRPCEN",
@ -86,7 +102,6 @@ export const office_: Office = {
}; };
export const user: User = { export const user: User = {
uid: "",
idNot: "123456_123456789", idNot: "123456_123456789",
contact: userContact, contact: userContact,
office_membership: office, office_membership: office,
@ -95,7 +110,6 @@ export const user: User = {
}; };
export const user_: User = { export const user_: User = {
uid: "",
idNot: "654321_789101112", idNot: "654321_789101112",
contact: userContact_, contact: userContact_,
office_membership: office_, office_membership: office_,
@ -103,42 +117,7 @@ export const user_: User = {
updated_at: null, updated_at: null,
}; };
export const deedType: DeedType = {
uid: "",
name: "Wedding",
description: "we assume wedding involve two people",
archived_at: null,
office: office,
created_at: null,
updated_at: null,
};
export const deedType_: DeedType = {
uid: "",
name: "Inheritance",
description: "we assume inheritance involve two people",
archived_at: null,
office: office,
created_at: null,
updated_at: null,
};
export const deed: Deed = {
uid: "",
deed_type: deedType,
created_at: null,
updated_at: null,
};
export const deed_: Deed = {
uid: "",
deed_type: deedType_,
created_at: null,
updated_at: null,
};
export const documentType: DocumentType = { export const documentType: DocumentType = {
uid: "",
name: "Identity card", name: "Identity card",
public_description: "your ID card delivered by your country of residence", public_description: "your ID card delivered by your country of residence",
private_description: "verify if this ID card is legit", private_description: "verify if this ID card is legit",
@ -149,7 +128,6 @@ export const documentType: DocumentType = {
}; };
export const documentType_: DocumentType = { export const documentType_: DocumentType = {
uid: "",
name: "Electricity bill", name: "Electricity bill",
public_description: "an electricity bill payed within the last 3 months", public_description: "an electricity bill payed within the last 3 months",
private_description: "verify if this electricity company is legit", private_description: "verify if this electricity company is legit",
@ -159,24 +137,59 @@ export const documentType_: DocumentType = {
updated_at: null, updated_at: null,
}; };
export const deedType: DeedType = {
name: "Wedding",
description: "we assume wedding involve two people",
archived_at: null,
office: office,
created_at: null,
updated_at: null,
deed_type_has_document_types: [
{
document_type: documentType,
deed_type: new DeedType(),
created_at: null,
updated_at: null,
},
],
};
export const deedType_: DeedType = {
name: "Inheritance",
description: "we assume inheritance involve two people",
archived_at: null,
office: office_,
created_at: null,
updated_at: null,
};
export const deed: Deed = {
deed_type: deedType,
created_at: null,
updated_at: null,
};
export const deed_: Deed = {
deed_type: deedType_,
created_at: null,
updated_at: null,
};
export const customer: Customer = { export const customer: Customer = {
uid: "", contact: customerContact,
contact: userContact,
status: "PENDING", status: "PENDING",
created_at: null, created_at: null,
updated_at: null, updated_at: null,
}; };
export const customer_: Customer = { export const customer_: Customer = {
uid: "", contact: customerContact_,
contact: userContact_,
status: "ERRONED", status: "ERRONED",
created_at: null, created_at: null,
updated_at: null, updated_at: null,
}; };
export const officeFolder: OfficeFolder = { export const officeFolder: OfficeFolder = {
uid: "",
name: "Dossier 1234567", name: "Dossier 1234567",
folder_number: "1234567", folder_number: "1234567",
description: "Dossier de mr Dupont", description: "Dossier de mr Dupont",
@ -184,12 +197,39 @@ export const officeFolder: OfficeFolder = {
status: "ARCHIVED", status: "ARCHIVED",
deed: deed, deed: deed,
office: office, office: office,
office_folder_has_customers: [
{
customer: customer,
office_folder: new OfficeFolder(),
created_at: null,
updated_at: null,
},
{
customer: customer_,
office_folder: new OfficeFolder(),
created_at: null,
updated_at: null,
},
],
office_folder_has_stakeholder: [
{
user_stakeholder: user,
office_folder: new OfficeFolder(),
created_at: null,
updated_at: null,
},
{
user_stakeholder: user_,
office_folder: new OfficeFolder(),
created_at: null,
updated_at: null,
},
],
created_at: null, created_at: null,
updated_at: null, updated_at: null,
}; };
export const officeFolder_: OfficeFolder = { export const officeFolder_: OfficeFolder = {
uid: "",
name: "Dossier 89101112", name: "Dossier 89101112",
folder_number: "89101112", folder_number: "89101112",
description: "Dossier de mme Dutunnel", description: "Dossier de mme Dutunnel",

View File

@ -3,7 +3,7 @@ import "reflect-metadata";
import { Customer } from "le-coffre-resources/dist/SuperAdmin"; import { Customer } from "le-coffre-resources/dist/SuperAdmin";
import CustomersService from "@Services/super-admin/CustomersService/CustomersService"; import CustomersService from "@Services/super-admin/CustomersService/CustomersService";
import { PrismaClient } from "@prisma/client"; import { PrismaClient } from "@prisma/client";
import { customer, customer_, userContact, userContact_ } from "./MockedData"; import { customer, customerContact, customerContact_, customer_ } from "@Test/config/MockedData";
import Container from "typedi"; import Container from "typedi";
import CustomersRepository from "@Repositories/CustomersRepository"; import CustomersRepository from "@Repositories/CustomersRepository";
@ -38,9 +38,9 @@ describe("test create function", () => {
// verify if customer address is created in db // verify if customer address is created in db
const addressForContactCreated = await prisma.addresses.findUnique({ where: { uuid: contactCreated?.address_uuid } }); const addressForContactCreated = await prisma.addresses.findUnique({ where: { uuid: contactCreated?.address_uuid } });
expect(addressForContactCreated?.address).toEqual(customer.contact.address.address); expect(addressForContactCreated?.address).toEqual(customer.contact.address?.address);
expect(addressForContactCreated?.zip_code).toEqual(customer.contact.address.zip_code); expect(addressForContactCreated?.zip_code).toEqual(customer.contact.address?.zip_code);
expect(addressForContactCreated?.city).toEqual(customer.contact.address.city); expect(addressForContactCreated?.city).toEqual(customer.contact.address?.city);
}); });
it("should not create an customer already created", async () => { it("should not create an customer already created", async () => {
@ -53,7 +53,7 @@ describe("test create function", () => {
it("should not create an new customer with an email already created", async () => { it("should not create an new customer with an email already created", async () => {
let newCustomer: Customer = JSON.parse(JSON.stringify(customer_)); let newCustomer: Customer = JSON.parse(JSON.stringify(customer_));
newCustomer.contact.email = userContact.email; newCustomer.contact.email = customerContact.email;
// try to create a new customer with already used email // try to create a new customer with already used email
async function createCustomerWithDuplicateEmail() { async function createCustomerWithDuplicateEmail() {
@ -64,7 +64,7 @@ describe("test create function", () => {
it("should not create an customer with an phone number already created", async () => { it("should not create an customer with an phone number already created", async () => {
let newCustomer: Customer = JSON.parse(JSON.stringify(customer_)); let newCustomer: Customer = JSON.parse(JSON.stringify(customer_));
newCustomer.contact.cell_phone_number = userContact.cell_phone_number; newCustomer.contact.cell_phone_number = customerContact.cell_phone_number;
// try to create a new customer with already used cellphone number // try to create a new customer with already used cellphone number
async function duplicateCustomer() { async function duplicateCustomer() {
@ -75,8 +75,8 @@ describe("test create function", () => {
it("should create an new customer if unique attributes differ from existing customers", async () => { it("should create an new customer if unique attributes differ from existing customers", async () => {
let newCustomer: Customer = JSON.parse(JSON.stringify(customer)); let newCustomer: Customer = JSON.parse(JSON.stringify(customer));
newCustomer.contact.email = userContact_.email; newCustomer.contact.email = customerContact_.email;
newCustomer.contact.cell_phone_number = userContact_.cell_phone_number; newCustomer.contact.cell_phone_number = customerContact_.cell_phone_number;
const customerCreated = await CustomersServiceTest.create(newCustomer); const customerCreated = await CustomersServiceTest.create(newCustomer);
@ -93,9 +93,9 @@ describe("test create function", () => {
// verify if customer_ address is created in db // verify if customer_ address is created in db
const addressForContactCreated = await prisma.addresses.findUnique({ where: { uuid: contactCreated?.address_uuid } }); const addressForContactCreated = await prisma.addresses.findUnique({ where: { uuid: contactCreated?.address_uuid } });
expect(addressForContactCreated?.address).toEqual(customer.contact.address.address); expect(addressForContactCreated?.address).toEqual(customer.contact.address?.address);
expect(addressForContactCreated?.zip_code).toEqual(customer.contact.address.zip_code); expect(addressForContactCreated?.zip_code).toEqual(customer.contact.address?.zip_code);
expect(addressForContactCreated?.city).toEqual(customer.contact.address.city); expect(addressForContactCreated?.city).toEqual(customer.contact.address?.city);
}); });
}); });
@ -119,15 +119,15 @@ describe("test update function", () => {
// verify if customer_ address is created in db // verify if customer_ address is created in db
const addressForExistingContact = await prisma.addresses.findUnique({ where: { uuid: existingContact?.address_uuid } }); const addressForExistingContact = await prisma.addresses.findUnique({ where: { uuid: existingContact?.address_uuid } });
expect(addressForExistingContact?.address).toEqual(customer_.contact.address.address); expect(addressForExistingContact?.address).toEqual(customer_.contact.address?.address);
expect(addressForExistingContact?.zip_code).toEqual(customer_.contact.address.zip_code); expect(addressForExistingContact?.zip_code).toEqual(customer_.contact.address?.zip_code);
expect(addressForExistingContact?.city).toEqual(customer_.contact.address.city); expect(addressForExistingContact?.city).toEqual(customer_.contact.address?.city);
}); });
it("should not update an customer with an email already used", async () => { it("should not update an customer with an email already used", async () => {
const customerUid = (await prisma.customers.findFirstOrThrow({ where: { contact: { email: customer_.contact.email } } })).uuid; const customerUid = (await prisma.customers.findFirstOrThrow({ where: { contact: { email: customer_.contact.email } } })).uuid;
let updatedCustomer: Customer = JSON.parse(JSON.stringify(customer_)); let updatedCustomer: Customer = JSON.parse(JSON.stringify(customer_));
updatedCustomer.contact.email = userContact.email; updatedCustomer.contact.email = customerContact.email;
// try to create a new customer with already used email // try to create a new customer with already used email
async function updateCustomerWithDuplicateEmail() { async function updateCustomerWithDuplicateEmail() {
@ -139,7 +139,7 @@ describe("test update function", () => {
it("should not update an customer with an phone number already used", async () => { it("should not update an customer with an phone number already used", async () => {
const customerUid = (await prisma.customers.findFirstOrThrow({ where: { contact: { email: customer_.contact.email } } })).uuid; const customerUid = (await prisma.customers.findFirstOrThrow({ where: { contact: { email: customer_.contact.email } } })).uuid;
let updatedCustomer: Customer = JSON.parse(JSON.stringify(customer_)); let updatedCustomer: Customer = JSON.parse(JSON.stringify(customer_));
updatedCustomer.contact.cell_phone_number = userContact.cell_phone_number; updatedCustomer.contact.cell_phone_number = customerContact.cell_phone_number;
// try to create a new customer with already used email // try to create a new customer with already used email
async function updateCustomerWithDuplicateEmail() { async function updateCustomerWithDuplicateEmail() {

View File

@ -2,86 +2,21 @@ import "module-alias/register";
import "reflect-metadata"; import "reflect-metadata";
import { Deed } from "le-coffre-resources/dist/SuperAdmin"; import { Deed } from "le-coffre-resources/dist/SuperAdmin";
import DeedService from "@Services/super-admin/DeedsService/DeedsService"; import DeedService from "@Services/super-admin/DeedsService/DeedsService";
import { PrismaClient, Offices, DocumentTypes, DeedTypes, DeedTypeHasDocumentTypes } from "prisma/prisma-client"; import { PrismaClient } from "prisma/prisma-client";
import { deed, deedType, documentType, office } from "./MockedData"; import { deed, deedType, documentType, documentType_, office } from "@Test/config/MockedData";
import DeedsRepository from "@Repositories/DeedsRepository"; import DeedsRepository from "@Repositories/DeedsRepository";
import Container from "typedi"; import Container from "typedi";
import { initDeedType, initDocumentType, initOffice } from "@Test/config/Init";
const prisma = new PrismaClient(); const prisma = new PrismaClient();
const DeedServiceTest = new DeedService(Container.get(DeedsRepository)); const DeedServiceTest = new DeedService(Container.get(DeedsRepository));
let office1: Offices;
let documentType1: DocumentTypes;
//let documentType2: DocumentTypes;
let deedType1: DeedTypes;
let deedType1HasDocumentType1: DeedTypeHasDocumentTypes;
beforeAll(async () => { beforeAll(async () => {
office1 = await prisma.offices.create({ office.uid = (await initOffice(office)).uuid;
data: { documentType.uid = (await initDocumentType(documentType, office)).uuid;
idNot: office.idNot, documentType_.uid = (await initDocumentType(documentType_, office)).uuid;
name: office.name, deedType.uid = (await initDeedType(deedType, office, [documentType.uid])).uuid;
crpcen: office.crpcen,
address: {
create: {
address: office.address.address,
zip_code: office.address.zip_code,
city: office.address.city,
},
},
},
});
documentType1 = await prisma.documentTypes.create({
data: {
name: documentType.name,
public_description: documentType.public_description,
private_description: documentType.private_description,
archived_at: null,
office_uuid: office1.uuid,
},
});
// documentType2 = await prisma.documentTypes.create({
// data: {
// name: documentType_.name,
// public_description: documentType_.public_description,
// private_description: documentType_.private_description,
// archived_at: null,
// office_uuid: office1.uuid,
// },
// });
deedType1 = await prisma.deedTypes.create({
data: {
name: deedType.name,
description: deedType.description,
archived_at: null,
office_uuid: office1.uuid,
},
});
deedType1HasDocumentType1 = await prisma.deedTypeHasDocumentTypes.create({
data: {
deed_type_uuid: deedType1.uuid,
document_type_uuid: documentType1.uuid,
},
});
await prisma.deedTypes.update({
where: { uuid: deedType1.uuid },
data: {
deed_type_has_document_types: {
connect: {
uuid: deedType1HasDocumentType1.uuid,
},
},
},
});
}); });
afterAll(async () => { afterAll(async () => {
@ -93,41 +28,43 @@ afterAll(async () => {
describe("test create function", () => { describe("test create function", () => {
it("should not create a new deed if deed type is unknown", async () => { it("should not create a new deed if deed type is unknown", async () => {
let deedWithoutDeedTypeUid: Deed = JSON.parse(JSON.stringify(deed));
deedWithoutDeedTypeUid.deed_type.uid = "random uuid";
// try to create a new deed with unknown deed type // try to create a new deed with unknown deed type
async function createDeedWithUnknownDeedType() { async function createDeedWithUnknownDeedType() {
await DeedServiceTest.create(deed); await DeedServiceTest.create(deedWithoutDeedTypeUid);
} }
await expect(createDeedWithUnknownDeedType).rejects.toThrow(); await expect(createDeedWithUnknownDeedType).rejects.toThrow();
}); });
it("should create a new deed based on existing deed type", async () => { it("should create a new deed based on existing deed type", async () => {
let deedWithUid: Deed = JSON.parse(JSON.stringify(deed)); let deedWithDeedTypeUid: Deed = JSON.parse(JSON.stringify(deed));
deedWithUid.uid = deedType1.uuid; deedWithDeedTypeUid.deed_type.uid = deedType.uid;
const deedCreated = await DeedServiceTest.create(deedWithUid); const deedCreated = await DeedServiceTest.create(deedWithDeedTypeUid);
expect(deedCreated.deed_type_uuid).toEqual(deedType1.uuid); expect(deedCreated.deed_type_uuid).toEqual(deedType.uid);
}); });
it("should have by default the same document types as its deed type ", async () => { it("should have by default the same document types as its deed type ", async () => {
const deedWithDocumentTypes = await prisma.deeds.findFirstOrThrow({ include: { deed_has_document_types: true } }); const deedWithDocumentTypes = await prisma.deeds.findFirstOrThrow({ include: { deed_has_document_types: true } });
expect(deedWithDocumentTypes.deed_has_document_types.length).toEqual(1); expect(deedWithDocumentTypes.deed_has_document_types.length).toEqual(1);
expect(deedWithDocumentTypes.deed_has_document_types[0]?.document_type_uuid).toEqual(documentType1.uuid); expect(deedWithDocumentTypes.deed_has_document_types[0]?.document_type_uuid).toEqual(documentType.uid);
}); });
it("should create a the same deed based on existing deed type", async () => { it("should create a the same deed based on existing deed type", async () => {
let deedWithUid: Deed = JSON.parse(JSON.stringify(deed)); let deedWithDeedTypeUid: Deed = JSON.parse(JSON.stringify(deed));
deedWithUid.uid = deedType1.uuid; deedWithDeedTypeUid.deed_type.uid = deedType.uid;
const deedCreated = await DeedServiceTest.create(deedWithUid); const deedCreated = await DeedServiceTest.create(deedWithDeedTypeUid);
expect(deedCreated.deed_type_uuid).toEqual(deedType1.uuid); expect(deedCreated.deed_type_uuid).toEqual(deedType.uid);
}); });
it("should not create a new deed based on archivated deed type", async () => { it("should not create a new deed based on archivated deed type", async () => {
let deedArchivated: Deed = JSON.parse(JSON.stringify(deed)); let deedArchivated: Deed = JSON.parse(JSON.stringify(deed));
deedArchivated.uid = deedType1.uuid; deedArchivated.deed_type.uid = deedType.uid;
await prisma.deedTypes.update({ await prisma.deedTypes.update({
where: { uuid: deedType1.uuid }, where: { uuid: deedType.uid },
data: { data: {
archived_at: new Date(Date.now()), archived_at: new Date(Date.now()),
}, },
@ -141,112 +78,91 @@ describe("test create function", () => {
}); });
}); });
// describe("test addDocumentTypes function", () => { describe("test update function", () => {
// it("should add document types to a deed", async () => { it("should add document types to a deed", async () => {
// const deedUid = (await prisma.deeds.findFirstOrThrow({ where: { deed_type_uuid: deedType1.uuid } })).uuid; const deedUid = (await prisma.deeds.findFirstOrThrow({ where: { deed_type_uuid: deedType.uid } })).uuid;
// const documentsToAdd = [documentType1.uuid, documentType2.uuid]; let deedToUpdate: Deed = JSON.parse(JSON.stringify(deed));
// await DeedServiceTest.addDocumentTypes(deedUid, documentsToAdd);
// const deed = await prisma.deeds.findFirstOrThrow({ deedToUpdate.deed_has_document_types = [
// where: { {
// uuid: deedUid, document_type: documentType,
// }, deed: new Deed(),
// include: { created_at: null,
// deed_has_document_types: true, updated_at: null,
// }, },
// }); {
// expect(deed.deed_has_document_types.length).toEqual(2); document_type: documentType_,
// }); deed: new Deed(),
created_at: null,
updated_at: null,
},
];
// it("should not add document types to a deed type that already has those document types ", async () => { await DeedServiceTest.update(deedUid, deedToUpdate);
// const deedUid = (await prisma.deeds.findFirstOrThrow({ where: { deed_type_uuid: deedType1.uuid } })).uuid;
// let deedHasDocumentTypes = await prisma.deedHasDocumentTypes.findMany({ where: { deed_uuid: deedUid } });
// expect(deedHasDocumentTypes.length).toEqual(2);
// const documentsToAdd = [documentType1.uuid, documentType2.uuid]; const deedUpdated = await prisma.deeds.findFirstOrThrow({
// //we expect deedTypeHasDocumentTypes service to skip duplicates without throwing error where: {
// await DeedServiceTest.addDocumentTypes(deedUid, documentsToAdd); uuid: deedUid,
},
include: {
deed_has_document_types: true,
},
});
expect(deedUpdated.deed_has_document_types.length).toEqual(2);
});
// deedHasDocumentTypes = await prisma.deedHasDocumentTypes.findMany({ where: { deed_uuid: deedUid } }); it("should not add document types to a deed type that already has those document types ", async () => {
// expect(deedHasDocumentTypes.length).toEqual(2); const deedUid = (await prisma.deeds.findFirstOrThrow({ where: { deed_type_uuid: deedType.uid } })).uuid;
// }); let deedToUpdate: Deed = JSON.parse(JSON.stringify(deed));
// });
// describe("test removeDocumentTypes function", () => {
// it("should remove document types from a deed type", async () => {
// const deedUid = (await prisma.deeds.findFirstOrThrow({ where: { deed_type_uuid: deedType1.uuid } })).uuid;
// const documentsToRemove = [documentType1.uuid];
// await DeedServiceTest.removeDocumentTypes(deedUid, documentsToRemove);
// const deedWithDocumentTypeRelations = await prisma.deeds.findFirstOrThrow({ deedToUpdate.deed_has_document_types = [
// where: { {
// uuid: deedUid, document_type: documentType,
// }, deed: new Deed(),
// include: { created_at: null,
// deed_has_document_types: true, updated_at: null,
// }, },
// }); {
// expect(deedWithDocumentTypeRelations.deed_has_document_types.length).toEqual(1); document_type: documentType_,
// expect(deedWithDocumentTypeRelations.deed_has_document_types[0]?.document_type_uuid).toEqual(documentType2.uuid); deed: new Deed(),
// }); created_at: null,
// it("should not remove document types from a deed type is they were not linked", async () => { updated_at: null,
// let deedWithOneDocumentType = await prisma.deeds.findFirstOrThrow({ },
// where: { deed_type_uuid: deedType1.uuid }, ];
// include: {
// deed_has_document_types: true,
// },
// });
// expect(deedWithOneDocumentType.deed_has_document_types.length).toEqual(1);
// const documentsToRemove = [documentType1.uuid]; await DeedServiceTest.update(deedUid, deedToUpdate);
// async function removeDocumentTypeNotLinkedToDeedType() { const deedUpdated = await prisma.deeds.findFirstOrThrow({
// await DeedServiceTest.removeDocumentTypes(deedWithOneDocumentType.uuid, documentsToRemove); where: {
// } uuid: deedUid,
// await expect(removeDocumentTypeNotLinkedToDeedType).rejects.toThrow(); },
// }); include: {
// }); deed_has_document_types: true,
// describe("test removeDocumentType function", () => { },
// it("should remove only one document type from a deed type", async () => { });
// let deedWithOneDocumentType = await prisma.deeds.findFirstOrThrow({ expect(deedUpdated.deed_has_document_types.length).toEqual(2);
// where: { deed_type_uuid: deedType1.uuid }, });
// include: {
// deed_has_document_types: true,
// },
// });
// expect(deedWithOneDocumentType.deed_has_document_types.length).toEqual(1);
// const documentToRemove = documentType2.uuid; it("should delete document types from a deed", async () => {
// await DeedServiceTest.removeDocumentType(deedWithOneDocumentType.uuid, documentToRemove); const deedUid = (await prisma.deeds.findFirstOrThrow({ where: { deed_type_uuid: deedType.uid } })).uuid;
let deedToUpdate: Deed = JSON.parse(JSON.stringify(deed));
// deedWithOneDocumentType = await prisma.deeds.findFirstOrThrow({ // set relation between deed and document types empty
// where: { deedToUpdate.deed_has_document_types = [];
// uuid: deedWithOneDocumentType.uuid,
// },
// include: {
// deed_has_document_types: true,
// },
// });
// expect(deedWithOneDocumentType.deed_has_document_types.length).toEqual(0);
// });
// });
// describe("test addDocumentType function", () => {
// it("should add only one document type to a deed type", async () => {
// const deedUid = (await prisma.deeds.findFirstOrThrow({ where: { deed_type_uuid: deedType1.uuid } })).uuid;
// const documentToAdd = documentType1.uuid;
// await DeedServiceTest.addDocumentType(deedUid, documentToAdd);
// const deed = await prisma.deeds.findFirstOrThrow({ await DeedServiceTest.update(deedUid, deedToUpdate);
// where: {
// uuid: deedUid, const deedUpdated = await prisma.deeds.findFirstOrThrow({
// }, where: {
// include: { uuid: deedUid,
// deed_has_document_types: true, },
// }, include: {
// }); deed_has_document_types: true,
// expect(deed.deed_has_document_types.length).toEqual(1); },
// expect(deed.deed_has_document_types[0]?.document_type_uuid).toEqual(documentType1.uuid); });
// }); expect(deedUpdated.deed_has_document_types.length).toEqual(0);
// }); });
});
describe("test get function", () => { describe("test get function", () => {
it("should return an array of Deeds", async () => { it("should return an array of Deeds", async () => {
@ -257,7 +173,7 @@ describe("test get function", () => {
expect(deeds.length).toEqual(2); expect(deeds.length).toEqual(2);
// verify result content // verify result content
expect(deeds[0]?.deed_type_uuid).toEqual(deedType1.uuid); expect(deeds[0]?.deed_type_uuid).toEqual(deedType.uid);
expect(deeds[1]?.deed_type_uuid).toEqual(deedType1.uuid); expect(deeds[1]?.deed_type_uuid).toEqual(deedType.uid);
}); });
}); });

View File

@ -2,71 +2,21 @@ import "module-alias/register";
import "reflect-metadata"; import "reflect-metadata";
import { DeedType } from "le-coffre-resources/dist/SuperAdmin"; import { DeedType } from "le-coffre-resources/dist/SuperAdmin";
import DeedTypeService from "@Services/super-admin/DeedTypesService/DeedTypesService"; import DeedTypeService from "@Services/super-admin/DeedTypesService/DeedTypesService";
import { PrismaClient, Offices } from "prisma/prisma-client"; import { PrismaClient } from "prisma/prisma-client";
import { deedType, deedType_, office, office_ } from "./MockedData"; import { deedType, deedType_, documentType, documentType_, office, office_ } from "@Test/config/MockedData";
import DeedTypesRepository from "@Repositories/DeedTypesRepository"; import DeedTypesRepository from "@Repositories/DeedTypesRepository";
import Container from "typedi"; import Container from "typedi";
import { initDocumentType, initOffice } from "@Test/config/Init";
const prisma = new PrismaClient(); const prisma = new PrismaClient();
const DeedTypeServiceTest = new DeedTypeService(Container.get(DeedTypesRepository)); const DeedTypeServiceTest = new DeedTypeService(Container.get(DeedTypesRepository));
let office1: Offices;
let office2: Offices;
// let documentType1: DocumentTypes;
// let documentType2: DocumentTypes;
beforeAll(async () => { beforeAll(async () => {
office1 = await prisma.offices.create({ office.uid = (await initOffice(office)).uuid;
data: { office_.uid = (await initOffice(office_)).uuid;
idNot: office.idNot, documentType.uid = (await initDocumentType(documentType, office)).uuid;
name: office.name, documentType_.uid = (await initDocumentType(documentType_, office)).uuid;
crpcen: office.crpcen,
address: {
create: {
address: office.address.address,
zip_code: office.address.zip_code,
city: office.address.city,
},
},
},
});
office2 = await prisma.offices.create({
data: {
idNot: office_.idNot,
name: office_.name,
crpcen: office_.crpcen,
address: {
create: {
address: office_.address.address,
zip_code: office_.address.zip_code,
city: office_.address.city,
},
},
},
});
// documentType1 = await prisma.documentTypes.create({
// data: {
// name: documentType.name,
// public_description: documentType.public_description,
// private_description: documentType.private_description,
// archived_at: null,
// office_uuid: office1.uuid,
// },
// });
// documentType2 = await prisma.documentTypes.create({
// data: {
// name: documentType_.name,
// public_description: documentType_.public_description,
// private_description: documentType_.private_description,
// archived_at: null,
// office_uuid: office1.uuid,
// },
// });
}); });
afterAll(async () => { afterAll(async () => {
@ -78,27 +28,27 @@ afterAll(async () => {
describe("test create function", () => { describe("test create function", () => {
it("should not create a new deed type if office is unknown", async () => { it("should not create a new deed type if office is unknown", async () => {
let deedTypeWithoutOfficeUid: DeedType = JSON.parse(JSON.stringify(deedType));
deedTypeWithoutOfficeUid.office.uid = "random uuid";
// try to create a new deed type with unknown office // try to create a new deed type with unknown office
async function createDeedTypeWithUnknownOffice() { async function createDeedTypeWithUnknownOffice() {
await DeedTypeServiceTest.create(deedType); await DeedTypeServiceTest.create(deedTypeWithoutOfficeUid);
} }
await expect(createDeedTypeWithUnknownOffice).rejects.toThrow(); await expect(createDeedTypeWithUnknownOffice).rejects.toThrow();
}); });
it("should create a new deed type", async () => { it("should create a new deed type", async () => {
let deedTypeWithOfficeUid: DeedType = JSON.parse(JSON.stringify(deedType)); const deedTypeCreated = await DeedTypeServiceTest.create(deedType);
deedTypeWithOfficeUid.office.uid = office1.uuid;
const deedTypeCreated = await DeedTypeServiceTest.create(deedTypeWithOfficeUid);
expect(deedTypeCreated.name).toEqual(deedType.name); expect(deedTypeCreated.name).toEqual(deedType.name);
expect(deedTypeCreated.description).toEqual(deedType.description); expect(deedTypeCreated.description).toEqual(deedType.description);
expect(deedTypeCreated.archived_at).toBeNull(); expect(deedTypeCreated.archived_at).toBeNull();
expect(deedTypeCreated.office_uuid).toEqual(office1.uuid); expect(deedTypeCreated.office_uuid).toEqual(office.uid);
}); });
it("should not create a new deed type with a name already used for a given office", async () => { it("should not create a new deed type with a name already used for a given office", async () => {
let deedTypeWithSameNameAndOffice: DeedType = JSON.parse(JSON.stringify(deedType_)); let deedTypeWithSameNameAndOffice: DeedType = JSON.parse(JSON.stringify(deedType_));
deedTypeWithSameNameAndOffice.office.uid = office1.uuid; deedTypeWithSameNameAndOffice.office = office;
deedTypeWithSameNameAndOffice.name = deedType.name; deedTypeWithSameNameAndOffice.name = deedType.name;
async function createDeedTypeWithSameNameAndOffice() { async function createDeedTypeWithSameNameAndOffice() {
@ -109,19 +59,18 @@ describe("test create function", () => {
it("should create the same deed type for a different office", async () => { it("should create the same deed type for a different office", async () => {
let deedTypeDuplicatedForNewOffice: DeedType = JSON.parse(JSON.stringify(deedType)); let deedTypeDuplicatedForNewOffice: DeedType = JSON.parse(JSON.stringify(deedType));
deedTypeDuplicatedForNewOffice.office.uid = office2.uuid; deedTypeDuplicatedForNewOffice.office = office_;
const deedTypeCreated = await DeedTypeServiceTest.create(deedTypeDuplicatedForNewOffice); const deedTypeCreated = await DeedTypeServiceTest.create(deedTypeDuplicatedForNewOffice);
expect(deedTypeCreated.name).toEqual(deedType.name); expect(deedTypeCreated.name).toEqual(deedType.name);
expect(deedTypeCreated.description).toEqual(deedType.description); expect(deedTypeCreated.description).toEqual(deedType.description);
expect(deedTypeCreated.archived_at).toBeNull(); expect(deedTypeCreated.archived_at).toBeNull();
expect(deedTypeCreated.office_uuid).toEqual(office2.uuid); expect(deedTypeCreated.office_uuid).toEqual(office_.uid);
}); });
it("should create the a new deed type version with a different name for a given office", async () => { it("should create the a new deed type version with a different name for a given office", async () => {
let deedTypeWithSameDescription: DeedType = JSON.parse(JSON.stringify(deedType)); let deedTypeWithSameDescription: DeedType = JSON.parse(JSON.stringify(deedType));
deedTypeWithSameDescription.office.uid = office1.uuid;
deedTypeWithSameDescription.name = deedType_.name; deedTypeWithSameDescription.name = deedType_.name;
const deedTypeCreated = await DeedTypeServiceTest.create(deedTypeWithSameDescription); const deedTypeCreated = await DeedTypeServiceTest.create(deedTypeWithSameDescription);
@ -129,21 +78,21 @@ describe("test create function", () => {
expect(deedTypeCreated.name).toEqual(deedType_.name); expect(deedTypeCreated.name).toEqual(deedType_.name);
expect(deedTypeCreated.description).toEqual(deedType.description); expect(deedTypeCreated.description).toEqual(deedType.description);
expect(deedTypeCreated.archived_at).toBeNull(); expect(deedTypeCreated.archived_at).toBeNull();
expect(deedTypeCreated.office_uuid).toEqual(office1.uuid); expect(deedTypeCreated.office_uuid).toEqual(office.uid);
}); });
}); });
describe("test update function", () => { describe("test update function", () => {
it("should update a deed type data", async () => { it("should update a deed type data", async () => {
const deedTypeCreated = await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType_.name, office_uuid: office1.uuid } }); const deedTypeCreated = await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType_.name, office_uuid: office.uid } });
expect(deedTypeCreated.name).toEqual(deedType_.name); expect(deedTypeCreated.name).toEqual(deedType_.name);
expect(deedTypeCreated.description).toEqual(deedType.description); expect(deedTypeCreated.description).toEqual(deedType.description);
expect(deedTypeCreated.archived_at).toBeNull(); expect(deedTypeCreated.archived_at).toBeNull();
expect(deedTypeCreated.office_uuid).toEqual(office1.uuid); expect(deedTypeCreated.office_uuid).toEqual(deedType.office.uid);
let deedTypeWithNewDescription: DeedType = JSON.parse(JSON.stringify(deedType_)); let deedTypeWithNewDescription: DeedType = JSON.parse(JSON.stringify(deedType_));
deedTypeWithNewDescription.office.uid = office1.uuid; deedTypeWithNewDescription.office = office;
// update the last deed type created with his the right description // update the last deed type created with his the right description
const deedTypeUpdated = await DeedTypeServiceTest.update(deedTypeCreated.uuid, deedTypeWithNewDescription); const deedTypeUpdated = await DeedTypeServiceTest.update(deedTypeCreated.uuid, deedTypeWithNewDescription);
@ -151,13 +100,13 @@ describe("test update function", () => {
expect(deedTypeUpdated.name).toEqual(deedType_.name); expect(deedTypeUpdated.name).toEqual(deedType_.name);
expect(deedTypeUpdated.description).toEqual(deedType_.description); expect(deedTypeUpdated.description).toEqual(deedType_.description);
expect(deedTypeUpdated.archived_at).toBeNull(); expect(deedTypeUpdated.archived_at).toBeNull();
expect(deedTypeUpdated.office_uuid).toEqual(office1.uuid); expect(deedTypeUpdated.office_uuid).toEqual(deedType.office.uid);
}); });
it("should not update a deed type name with an already used name for given office", async () => { it("should not update a deed type name with an already used name for given office", async () => {
const deedTypeUid = (await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType_.name, office_uuid: office1.uuid } })).uuid; const deedTypeUid = (await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType_.name, office_uuid: office.uid } })).uuid;
let deedTypeWithSameNameAndOffice: DeedType = JSON.parse(JSON.stringify(deedType_)); let deedTypeWithSameNameAndOffice: DeedType = JSON.parse(JSON.stringify(deedType_));
deedTypeWithSameNameAndOffice.office.uid = office1.uuid; deedTypeWithSameNameAndOffice.office.uid = office.uid;
deedTypeWithSameNameAndOffice.name = deedType.name; deedTypeWithSameNameAndOffice.name = deedType.name;
// update the last deed type created with his the right description // update the last deed type created with his the right description
@ -168,9 +117,9 @@ describe("test update function", () => {
}); });
it("should not update a deed type office membership if the office already have this document type", async () => { it("should not update a deed type office membership if the office already have this document type", async () => {
const deedTypeUid = (await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType_.name, office_uuid: office1.uuid } })).uuid; const deedTypeUid = (await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType_.name, office_uuid: office.uid } })).uuid;
let deedTypeWithSameNameAndOffice: DeedType = JSON.parse(JSON.stringify(deedType_)); let deedTypeWithSameNameAndOffice: DeedType = JSON.parse(JSON.stringify(deedType_));
deedTypeWithSameNameAndOffice.office.uid = office1.uuid; deedTypeWithSameNameAndOffice.office.uid = office.uid;
deedTypeWithSameNameAndOffice.name = deedType.name; deedTypeWithSameNameAndOffice.name = deedType.name;
// try to duplicate deed type in a given office // try to duplicate deed type in a given office
@ -181,37 +130,33 @@ describe("test update function", () => {
}); });
it("should update a deed type office membership", async () => { it("should update a deed type office membership", async () => {
const deedTypeCreated = await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType_.name, office_uuid: office1.uuid } }); const deedTypeCreated = await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType_.name, office_uuid: office.uid } });
expect(deedTypeCreated.name).toEqual(deedType_.name); expect(deedTypeCreated.name).toEqual(deedType_.name);
expect(deedTypeCreated.description).toEqual(deedType_.description); expect(deedTypeCreated.description).toEqual(deedType_.description);
expect(deedTypeCreated.archived_at).toBeNull(); expect(deedTypeCreated.archived_at).toBeNull();
expect(deedTypeCreated.office_uuid).toEqual(office1.uuid); expect(deedTypeCreated.office_uuid).toEqual(office.uid);
let deedTypeTransferedToNewOffice: DeedType = JSON.parse(JSON.stringify(deedType_));
deedTypeTransferedToNewOffice.office.uid = office2.uuid;
// update the last deed type updated with a new office membership // update the last deed type updated with a new office membership
const deedTypeUpdated = await DeedTypeServiceTest.update(deedTypeCreated.uuid, deedTypeTransferedToNewOffice); const deedTypeUpdated = await DeedTypeServiceTest.update(deedTypeCreated.uuid, deedType_);
expect(deedTypeUpdated.name).toEqual(deedType_.name); expect(deedTypeUpdated.name).toEqual(deedType_.name);
expect(deedTypeUpdated.description).toEqual(deedType_.description); expect(deedTypeUpdated.description).toEqual(deedType_.description);
expect(deedTypeUpdated.archived_at).toBeNull(); expect(deedTypeUpdated.archived_at).toBeNull();
expect(deedTypeUpdated.office_uuid).toEqual(office2.uuid); expect(deedTypeUpdated.office_uuid).toEqual(deedType_.office.uid);
}); });
it("should archivate a deed type", async () => { it("should archivate a deed type", async () => {
const deedTypeCreated = await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType_.name, office_uuid: office2.uuid } }); const deedTypeCreated = await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType_.name, office_uuid: office_.uid } });
expect(deedTypeCreated.name).toEqual(deedType_.name); expect(deedTypeCreated.name).toEqual(deedType_.name);
expect(deedTypeCreated.description).toEqual(deedType_.description); expect(deedTypeCreated.description).toEqual(deedType_.description);
expect(deedTypeCreated.archived_at).toBeNull(); expect(deedTypeCreated.archived_at).toBeNull();
expect(deedTypeCreated.office_uuid).toEqual(office2.uuid); expect(deedTypeCreated.office_uuid).toEqual(deedType_.office.uid);
let deedTypeArchivated: DeedType = JSON.parse(JSON.stringify(deedType_)); let deedTypeArchivated: DeedType = JSON.parse(JSON.stringify(deedType_));
deedTypeArchivated.office.uid = office2.uuid;
const currentDate = new Date(Date.now()); const currentDate = new Date(Date.now());
deedTypeArchivated.archived_at = currentDate; deedTypeArchivated.archived_at = new Date(Date.now());
// archivate a deed type by giving a non null date for archivated_at attribute // archivate a deed type by giving a non null date for archivated_at attribute
const deedTypeUpdated = await DeedTypeServiceTest.update(deedTypeCreated.uuid, deedTypeArchivated); const deedTypeUpdated = await DeedTypeServiceTest.update(deedTypeCreated.uuid, deedTypeArchivated);
@ -219,138 +164,111 @@ describe("test update function", () => {
expect(deedTypeUpdated.name).toEqual(deedType_.name); expect(deedTypeUpdated.name).toEqual(deedType_.name);
expect(deedTypeUpdated.description).toEqual(deedType_.description); expect(deedTypeUpdated.description).toEqual(deedType_.description);
expect(deedTypeUpdated.archived_at).toEqual(currentDate); expect(deedTypeUpdated.archived_at).toEqual(currentDate);
expect(deedTypeUpdated.office_uuid).toEqual(office2.uuid); expect(deedTypeUpdated.office_uuid).toEqual(office_.uid);
}); });
it("should unarchivate a deed type", async () => { it("should unarchivate a deed type", async () => {
const deedTypeCreated = await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType_.name, office_uuid: office2.uuid } }); const deedTypeCreated = await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType_.name, office_uuid: office_.uid } });
expect(deedTypeCreated.name).toEqual(deedType_.name); expect(deedTypeCreated.name).toEqual(deedType_.name);
expect(deedTypeCreated.description).toEqual(deedType_.description); expect(deedTypeCreated.description).toEqual(deedType_.description);
expect(deedTypeCreated.archived_at).not.toBeNull(); expect(deedTypeCreated.archived_at).not.toBeNull();
expect(deedTypeCreated.office_uuid).toEqual(office2.uuid); expect(deedTypeCreated.office_uuid).toEqual(deedType_.office.uid);
let deedTypeUnarchivated: DeedType = JSON.parse(JSON.stringify(deedType_));
deedTypeUnarchivated.office.uid = office2.uuid;
// unarchivate a deed type by giving a null date for archivated_at attribute // unarchivate a deed type by giving a null date for archivated_at attribute
const deedTypeUpdated = await DeedTypeServiceTest.update(deedTypeCreated.uuid, deedTypeUnarchivated); const deedTypeUpdated = await DeedTypeServiceTest.update(deedTypeCreated.uuid, deedType_);
expect(deedTypeUpdated.name).toEqual(deedType_.name); expect(deedTypeUpdated.name).toEqual(deedType_.name);
expect(deedTypeUpdated.description).toEqual(deedType_.description); expect(deedTypeUpdated.description).toEqual(deedType_.description);
expect(deedTypeUpdated.archived_at).toBeNull(); expect(deedTypeUpdated.archived_at).toBeNull();
expect(deedTypeUpdated.office_uuid).toEqual(office2.uuid); expect(deedTypeUpdated.office_uuid).toEqual(office_.uid);
});
it("should add document types to a deed type", async () => {
const deedTypeUid = (await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType.name, office_uuid: office.uid } })).uuid;
let deedTypeToUpdate: DeedType = JSON.parse(JSON.stringify(deedType));
deedTypeToUpdate.deed_type_has_document_types = [
{
document_type: documentType,
deed_type: new DeedType(),
created_at: null,
updated_at: null,
},
{
document_type: documentType_,
deed_type: new DeedType(),
created_at: null,
updated_at: null,
},
];
await DeedTypeServiceTest.update(deedTypeUid, deedTypeToUpdate);
const deedTypeUpdated = await prisma.deedTypes.findFirstOrThrow({
where: {
uuid: deedTypeUid,
},
include: {
deed_type_has_document_types: true,
},
});
expect(deedTypeUpdated.deed_type_has_document_types.length).toEqual(2);
});
it("should not add document types to a deed type that already has those document types ", async () => {
const deedTypeUid = (await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType.name, office_uuid: office.uid } })).uuid;
let deedTypeToUpdate: DeedType = JSON.parse(JSON.stringify(deedType));
deedTypeToUpdate.deed_type_has_document_types = [
{
document_type: documentType,
deed_type: new DeedType(),
created_at: null,
updated_at: null,
},
{
document_type: documentType_,
deed_type: new DeedType(),
created_at: null,
updated_at: null,
},
];
await DeedTypeServiceTest.update(deedTypeUid, deedTypeToUpdate);
const deedTypeUpdated = await prisma.deedTypes.findFirstOrThrow({
where: {
uuid: deedTypeUid,
},
include: {
deed_type_has_document_types: true,
},
});
expect(deedTypeUpdated.deed_type_has_document_types.length).toEqual(2);
});
it("should delete document types from a deed", async () => {
const deedTypeUid = (await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType.name, office_uuid: office.uid } })).uuid;
let deedTypeToUpdate: DeedType = JSON.parse(JSON.stringify(deedType));
// set relation between deed and document types empty
deedTypeToUpdate.deed_type_has_document_types = [];
await DeedTypeServiceTest.update(deedTypeUid, deedTypeToUpdate);
const deedTypeUpdated = await prisma.deedTypes.findFirstOrThrow({
where: {
uuid: deedTypeUid,
},
include: {
deed_type_has_document_types: true,
},
});
expect(deedTypeUpdated.deed_type_has_document_types.length).toEqual(0);
}); });
}); });
// describe("test addDocumentTypes function", () => {
// it("should add document types to a deed type", async () => {
// const deedTypeUid = (await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType.name, office_uuid: office1.uuid } })).uuid;
// const documentsToAdd = [documentType1.uuid, documentType2.uuid];
// await DeedTypeServiceTest.addDocumentTypes(deedTypeUid, documentsToAdd);
// const deedTypes = await prisma.deedTypes.findFirstOrThrow({
// where: {
// uuid: deedTypeUid,
// },
// include: {
// deed_type_has_document_types: true,
// },
// });
// expect(deedTypes.deed_type_has_document_types.length).toEqual(2);
// });
// it("should not add document types to a deed type that already has those document types ", async () => {
// const deedTypeUid = (await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType.name, office_uuid: office1.uuid } })).uuid;
// let deedTypeHasDocumentTypes = await prisma.deedTypeHasDocumentTypes.findMany({ where: { deed_type_uuid: deedTypeUid } });
// expect(deedTypeHasDocumentTypes.length).toEqual(2);
// const documentsToAdd = [documentType1.uuid, documentType2.uuid];
// //we expect deedTypeHasDocumentTypes service to skip duplicates without throwing error
// await DeedTypeServiceTest.addDocumentTypes(deedTypeUid, documentsToAdd);
// deedTypeHasDocumentTypes = await prisma.deedTypeHasDocumentTypes.findMany({ where: { deed_type_uuid: deedTypeUid } });
// expect(deedTypeHasDocumentTypes.length).toEqual(2);
// });
// });
// describe("test removeDocumentTypes function", () => {
// it("should remove document types from a deed type", async () => {
// const deedTypeUid = (await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType.name, office_uuid: office1.uuid } })).uuid;
// const documentsToRemove = [documentType1.uuid];
// await DeedTypeServiceTest.removeDocumentTypes(deedTypeUid, documentsToRemove);
// const deedTypeWithDocumentTypeRelations = await prisma.deedTypes.findFirstOrThrow({
// where: {
// uuid: deedTypeUid,
// },
// include: {
// deed_type_has_document_types: true,
// },
// });
// expect(deedTypeWithDocumentTypeRelations.deed_type_has_document_types.length).toEqual(1);
// expect(deedTypeWithDocumentTypeRelations.deed_type_has_document_types[0]?.document_type_uuid).toEqual(documentType2.uuid);
// });
// it("should not remove document types from a deed type is they were not linked", async () => {
// let deedTypeWithOneDocumentType = await prisma.deedTypes.findFirstOrThrow({
// where: { name: deedType.name, office_uuid: office1.uuid },
// include: {
// deed_type_has_document_types: true,
// },
// });
// expect(deedTypeWithOneDocumentType.deed_type_has_document_types.length).toEqual(1);
// const documentsToRemove = [documentType1.uuid];
// // try to duplicate deed type in a given office
// async function removeDocumentTypeNotLinkedToDeedType() {
// await DeedTypeServiceTest.removeDocumentTypes(deedTypeWithOneDocumentType.uuid, documentsToRemove);
// }
// await expect(removeDocumentTypeNotLinkedToDeedType).rejects.toThrow();
// });
// });
// describe("test removeDocumentType function", () => {
// it("should remove only one document type from a deed type", async () => {
// let deedTypeWithOneDocumentType = await prisma.deedTypes.findFirstOrThrow({
// where: { name: deedType.name, office_uuid: office1.uuid },
// include: {
// deed_type_has_document_types: true,
// },
// });
// expect(deedTypeWithOneDocumentType.deed_type_has_document_types.length).toEqual(1);
// const documentToRemove = documentType2.uuid;
// await DeedTypeServiceTest.removeDocumentType(deedTypeWithOneDocumentType.uuid, documentToRemove);
// deedTypeWithOneDocumentType = await prisma.deedTypes.findFirstOrThrow({
// where: {
// uuid: deedTypeWithOneDocumentType.uuid,
// },
// include: {
// deed_type_has_document_types: true,
// },
// });
// expect(deedTypeWithOneDocumentType.deed_type_has_document_types.length).toEqual(0);
// });
// });
// describe("test addDocumentType function", () => {
// it("should add only one document type to a deed type", async () => {
// const deedTypeUid = (await prisma.deedTypes.findFirstOrThrow({ where: { name: deedType.name, office_uuid: office1.uuid } })).uuid;
// const documentToAdd = documentType1.uuid;
// await DeedTypeServiceTest.addDocumentType(deedTypeUid, documentToAdd);
// const deedTypes = await prisma.deedTypes.findFirstOrThrow({
// where: {
// uuid: deedTypeUid,
// },
// include: {
// deed_type_has_document_types: true,
// },
// });
// expect(deedTypes.deed_type_has_document_types.length).toEqual(1);
// expect(deedTypes.deed_type_has_document_types[0]?.document_type_uuid).toEqual(documentType1.uuid);
// });
// });
describe("test get function", () => { describe("test get function", () => {
it("should return an array of DeedTypes", async () => { it("should return an array of DeedTypes", async () => {
const deedTypes = await DeedTypeServiceTest.get({ orderBy: [{ name: "asc" }, { created_at: "asc" }] }); const deedTypes = await DeedTypeServiceTest.get({ orderBy: [{ name: "asc" }, { created_at: "asc" }] });
@ -363,21 +281,21 @@ describe("test get function", () => {
expect(deedTypes[0]?.name).toEqual(deedType_.name); expect(deedTypes[0]?.name).toEqual(deedType_.name);
expect(deedTypes[0]?.description).toEqual(deedType_.description); expect(deedTypes[0]?.description).toEqual(deedType_.description);
expect(deedTypes[0]?.archived_at).toBeNull(); expect(deedTypes[0]?.archived_at).toBeNull();
expect(deedTypes[0]?.office_uuid).toEqual(office2.uuid); expect(deedTypes[0]?.office_uuid).toEqual(office_.uid);
expect(deedTypes[1]?.name).toEqual(deedType.name); expect(deedTypes[1]?.name).toEqual(deedType.name);
expect(deedTypes[1]?.description).toEqual(deedType.description); expect(deedTypes[1]?.description).toEqual(deedType.description);
expect(deedTypes[1]?.archived_at).toBeNull(); expect(deedTypes[1]?.archived_at).toBeNull();
expect(deedTypes[1]?.office_uuid).toEqual(office1.uuid); expect(deedTypes[1]?.office_uuid).toEqual(office.uid);
expect(deedTypes[2]?.name).toEqual(deedType.name); expect(deedTypes[2]?.name).toEqual(deedType.name);
expect(deedTypes[2]?.description).toEqual(deedType.description); expect(deedTypes[2]?.description).toEqual(deedType.description);
expect(deedTypes[2]?.archived_at).toBeNull(); expect(deedTypes[2]?.archived_at).toBeNull();
expect(deedTypes[2]?.office_uuid).toEqual(office2.uuid); expect(deedTypes[2]?.office_uuid).toEqual(office_.uid);
}); });
it("should return an array of DeedTypes per offices", async () => { it("should return an array of DeedTypes per offices", async () => {
const deedTypesForFirstOffice = await DeedTypeServiceTest.get({ where: { office: office1 }, orderBy: { name: "asc" } }); const deedTypesForFirstOffice = await DeedTypeServiceTest.get({ where: { office: {uuid: office.uid} }, orderBy: { name: "asc" } });
expect(deedTypesForFirstOffice.length).toEqual(1); expect(deedTypesForFirstOffice.length).toEqual(1);
@ -385,9 +303,9 @@ describe("test get function", () => {
expect(deedTypesForFirstOffice[0]?.name).toEqual(deedType.name); expect(deedTypesForFirstOffice[0]?.name).toEqual(deedType.name);
expect(deedTypesForFirstOffice[0]?.description).toEqual(deedType.description); expect(deedTypesForFirstOffice[0]?.description).toEqual(deedType.description);
expect(deedTypesForFirstOffice[0]?.archived_at).toBeNull(); expect(deedTypesForFirstOffice[0]?.archived_at).toBeNull();
expect(deedTypesForFirstOffice[0]?.office_uuid).toEqual(office1.uuid); expect(deedTypesForFirstOffice[0]?.office_uuid).toEqual(office.uid);
const deedTypesForSecondOffice = await DeedTypeServiceTest.get({ where: { office: office2 }, orderBy: { name: "asc" } }); const deedTypesForSecondOffice = await DeedTypeServiceTest.get({ where: { office: {uuid: office_.uid} }, orderBy: { name: "asc" } });
expect(deedTypesForSecondOffice.length).toEqual(2); expect(deedTypesForSecondOffice.length).toEqual(2);
@ -395,11 +313,11 @@ describe("test get function", () => {
expect(deedTypesForSecondOffice[0]?.name).toEqual(deedType_.name); expect(deedTypesForSecondOffice[0]?.name).toEqual(deedType_.name);
expect(deedTypesForSecondOffice[0]?.description).toEqual(deedType_.description); expect(deedTypesForSecondOffice[0]?.description).toEqual(deedType_.description);
expect(deedTypesForSecondOffice[0]?.archived_at).toBeNull(); expect(deedTypesForSecondOffice[0]?.archived_at).toBeNull();
expect(deedTypesForSecondOffice[0]?.office_uuid).toEqual(office2.uuid); expect(deedTypesForSecondOffice[0]?.office_uuid).toEqual(office_.uid);
expect(deedTypesForSecondOffice[1]?.name).toEqual(deedType.name); expect(deedTypesForSecondOffice[1]?.name).toEqual(deedType.name);
expect(deedTypesForSecondOffice[1]?.description).toEqual(deedType.description); expect(deedTypesForSecondOffice[1]?.description).toEqual(deedType.description);
expect(deedTypesForSecondOffice[1]?.archived_at).toBeNull(); expect(deedTypesForSecondOffice[1]?.archived_at).toBeNull();
expect(deedTypesForSecondOffice[1]?.office_uuid).toEqual(office2.uuid); expect(deedTypesForSecondOffice[1]?.office_uuid).toEqual(office_.uid);
}); });
}); });

View File

@ -2,48 +2,19 @@ import "module-alias/register";
import "reflect-metadata"; import "reflect-metadata";
import { DocumentType } from "le-coffre-resources/dist/SuperAdmin"; import { DocumentType } from "le-coffre-resources/dist/SuperAdmin";
import DocumentTypesService from "@Services/super-admin/DocumentTypesService/DocumentTypesService"; import DocumentTypesService from "@Services/super-admin/DocumentTypesService/DocumentTypesService";
import { PrismaClient, Offices } from "prisma/prisma-client"; import { PrismaClient } from "prisma/prisma-client";
import { documentType, documentType_, office, office_ } from "./MockedData"; import { documentType, documentType_, office, office_ } from "@Test/config/MockedData";
import DocumentTypesRepository from "@Repositories/DocumentTypesRepository"; import DocumentTypesRepository from "@Repositories/DocumentTypesRepository";
import Container from "typedi"; import Container from "typedi";
import { initOffice } from "@Test/config/Init";
const prisma = new PrismaClient(); const prisma = new PrismaClient();
const DocumentTypesServiceTest = new DocumentTypesService(Container.get(DocumentTypesRepository)); const DocumentTypesServiceTest = new DocumentTypesService(Container.get(DocumentTypesRepository));
let office1: Offices;
let office2: Offices;
beforeAll(async () => { beforeAll(async () => {
office1 = await prisma.offices.create({ office.uid = (await initOffice(office)).uuid;
data: { office_.uid = (await initOffice(office_)).uuid;
idNot: office.idNot,
name: office.name,
crpcen: office.crpcen,
address: {
create: {
address: office.address.address,
zip_code: office.address.zip_code,
city: office.address.city,
},
},
},
});
office2 = await prisma.offices.create({
data: {
idNot: office_.idNot,
name: office_.name,
crpcen: office_.crpcen,
address: {
create: {
address: office_.address.address,
zip_code: office_.address.zip_code,
city: office_.address.city,
},
},
},
});
}); });
afterAll(async () => { afterAll(async () => {
@ -55,28 +26,30 @@ afterAll(async () => {
describe("test create function", () => { describe("test create function", () => {
it("should not create a new document type if office is unknown", async () => { it("should not create a new document type if office is unknown", async () => {
let documentTypeWithoutOfficeUid: DocumentType = JSON.parse(JSON.stringify(documentType));
documentTypeWithoutOfficeUid.office.uid = "random uuid";
// try to create a new document type with unknown office // try to create a new document type with unknown office
async function createDocumentTypeWithUnknownOffice() { async function createDocumentTypeWithUnknownOffice() {
await DocumentTypesServiceTest.create(documentType); await DocumentTypesServiceTest.create(documentTypeWithoutOfficeUid);
} }
await expect(createDocumentTypeWithUnknownOffice).rejects.toThrow(); await expect(createDocumentTypeWithUnknownOffice).rejects.toThrow();
}); });
it("should create a new document type", async () => { it("should create a new document type", async () => {
let documentTypeWithOfficeUid: DocumentType = JSON.parse(JSON.stringify(documentType)); let documentTypeWithOfficeUid: DocumentType = JSON.parse(JSON.stringify(documentType));
documentTypeWithOfficeUid.office.uid = office1.uuid; documentTypeWithOfficeUid.office.uid = office.uid;
const documentTypeCreated = await DocumentTypesServiceTest.create(documentTypeWithOfficeUid); const documentTypeCreated = await DocumentTypesServiceTest.create(documentTypeWithOfficeUid);
expect(documentTypeCreated.name).toEqual(documentType.name); expect(documentTypeCreated.name).toEqual(documentType.name);
expect(documentTypeCreated.public_description).toEqual(documentType.public_description); expect(documentTypeCreated.public_description).toEqual(documentType.public_description);
expect(documentTypeCreated.private_description).toEqual(documentType.private_description); expect(documentTypeCreated.private_description).toEqual(documentType.private_description);
expect(documentTypeCreated.archived_at).toBeNull(); expect(documentTypeCreated.archived_at).toBeNull();
expect(documentTypeCreated.office_uuid).toEqual(office1.uuid); expect(documentTypeCreated.office_uuid).toEqual(office.uid);
}); });
it("should not create a new document type with a name already used for a given office", async () => { it("should not create a new document type with a name already used for a given office", async () => {
let documentTypeWithSameNameAndOffice: DocumentType = JSON.parse(JSON.stringify(documentType_)); let documentTypeWithSameNameAndOffice: DocumentType = JSON.parse(JSON.stringify(documentType_));
documentTypeWithSameNameAndOffice.office.uid = office1.uuid; documentTypeWithSameNameAndOffice.office.uid = office.uid;
documentTypeWithSameNameAndOffice.name = documentType.name; documentTypeWithSameNameAndOffice.name = documentType.name;
async function createDocumentTypeWithSameNameAndOffice() { async function createDocumentTypeWithSameNameAndOffice() {
@ -87,7 +60,7 @@ describe("test create function", () => {
it("should create the same document type for a different office", async () => { it("should create the same document type for a different office", async () => {
let documentTypeDuplicatedForNewOffice: DocumentType = JSON.parse(JSON.stringify(documentType)); let documentTypeDuplicatedForNewOffice: DocumentType = JSON.parse(JSON.stringify(documentType));
documentTypeDuplicatedForNewOffice.office.uid = office2.uuid; documentTypeDuplicatedForNewOffice.office.uid = office_.uid;
const documentTypeCreated = await DocumentTypesServiceTest.create(documentTypeDuplicatedForNewOffice); const documentTypeCreated = await DocumentTypesServiceTest.create(documentTypeDuplicatedForNewOffice);
@ -95,12 +68,12 @@ describe("test create function", () => {
expect(documentTypeCreated.public_description).toEqual(documentType.public_description); expect(documentTypeCreated.public_description).toEqual(documentType.public_description);
expect(documentTypeCreated.private_description).toEqual(documentType.private_description); expect(documentTypeCreated.private_description).toEqual(documentType.private_description);
expect(documentTypeCreated.archived_at).toBeNull(); expect(documentTypeCreated.archived_at).toBeNull();
expect(documentTypeCreated.office_uuid).toEqual(office2.uuid); expect(documentTypeCreated.office_uuid).toEqual(office_.uid);
}); });
it("should create a new document type version with a different name for a given office", async () => { it("should create a new document type version with a different name for a given office", async () => {
let documentTypeWithSameDescription: DocumentType = JSON.parse(JSON.stringify(documentType)); let documentTypeWithSameDescription: DocumentType = JSON.parse(JSON.stringify(documentType));
documentTypeWithSameDescription.office.uid = office1.uuid; documentTypeWithSameDescription.office.uid = office.uid;
documentTypeWithSameDescription.name = documentType_.name; documentTypeWithSameDescription.name = documentType_.name;
const documentTypeCreated = await DocumentTypesServiceTest.create(documentTypeWithSameDescription); const documentTypeCreated = await DocumentTypesServiceTest.create(documentTypeWithSameDescription);
@ -109,23 +82,23 @@ describe("test create function", () => {
expect(documentTypeCreated.public_description).toEqual(documentType.public_description); expect(documentTypeCreated.public_description).toEqual(documentType.public_description);
expect(documentTypeCreated.private_description).toEqual(documentType.private_description); expect(documentTypeCreated.private_description).toEqual(documentType.private_description);
expect(documentTypeCreated.archived_at).toBeNull(); expect(documentTypeCreated.archived_at).toBeNull();
expect(documentTypeCreated.office_uuid).toEqual(office1.uuid); expect(documentTypeCreated.office_uuid).toEqual(office.uid);
}); });
}); });
describe("test update function", () => { describe("test update function", () => {
it("should update a document type", async () => { it("should update a document type", async () => {
const documentTypeCreated = await prisma.documentTypes.findFirstOrThrow({ const documentTypeCreated = await prisma.documentTypes.findFirstOrThrow({
where: { name: documentType_.name, office_uuid: office1.uuid }, where: { name: documentType_.name, office_uuid: office.uid },
}); });
expect(documentTypeCreated.name).toEqual(documentType_.name); expect(documentTypeCreated.name).toEqual(documentType_.name);
expect(documentTypeCreated.public_description).toEqual(documentType.public_description); expect(documentTypeCreated.public_description).toEqual(documentType.public_description);
expect(documentTypeCreated.private_description).toEqual(documentType.private_description); expect(documentTypeCreated.private_description).toEqual(documentType.private_description);
expect(documentTypeCreated.archived_at).toBeNull(); expect(documentTypeCreated.archived_at).toBeNull();
expect(documentTypeCreated.office_uuid).toEqual(office1.uuid); expect(documentTypeCreated.office_uuid).toEqual(office.uid);
let documentTypeWithNewDescription: DocumentType = JSON.parse(JSON.stringify(documentType_)); let documentTypeWithNewDescription: DocumentType = JSON.parse(JSON.stringify(documentType_));
documentTypeWithNewDescription.office.uid = office1.uuid; documentTypeWithNewDescription.office.uid = office.uid;
// update the last document type created with his the right descriptions // update the last document type created with his the right descriptions
const documentTypeUpdated = await DocumentTypesServiceTest.update(documentTypeCreated.uuid, documentTypeWithNewDescription); const documentTypeUpdated = await DocumentTypesServiceTest.update(documentTypeCreated.uuid, documentTypeWithNewDescription);
@ -133,15 +106,15 @@ describe("test update function", () => {
expect(documentTypeUpdated.public_description).toEqual(documentType_.public_description); expect(documentTypeUpdated.public_description).toEqual(documentType_.public_description);
expect(documentTypeUpdated.private_description).toEqual(documentType_.private_description); expect(documentTypeUpdated.private_description).toEqual(documentType_.private_description);
expect(documentTypeUpdated.archived_at).toBeNull(); expect(documentTypeUpdated.archived_at).toBeNull();
expect(documentTypeUpdated.office_uuid).toEqual(office1.uuid); expect(documentTypeUpdated.office_uuid).toEqual(office.uid);
}); });
it("should not update a document type name with an already used name for given office", async () => { it("should not update a document type name with an already used name for given office", async () => {
const documentTypeUid = ( const documentTypeUid = (
await prisma.documentTypes.findFirstOrThrow({ where: { name: documentType_.name, office_uuid: office1.uuid } }) await prisma.documentTypes.findFirstOrThrow({ where: { name: documentType_.name, office_uuid: office.uid } })
).uuid; ).uuid;
let documentTypeWithSameNameAndOffice: DocumentType = JSON.parse(JSON.stringify(documentType_)); let documentTypeWithSameNameAndOffice: DocumentType = JSON.parse(JSON.stringify(documentType_));
documentTypeWithSameNameAndOffice.office.uid = office1.uuid; documentTypeWithSameNameAndOffice.office.uid = office.uid;
documentTypeWithSameNameAndOffice.name = documentType.name; documentTypeWithSameNameAndOffice.name = documentType.name;
// update the last document type created with his the right description // update the last document type created with his the right description
@ -153,10 +126,10 @@ describe("test update function", () => {
it("should not update a document type office membership if the office already has this document type", async () => { it("should not update a document type office membership if the office already has this document type", async () => {
const documentTypeUid = ( const documentTypeUid = (
await prisma.documentTypes.findFirstOrThrow({ where: { name: documentType_.name, office_uuid: office1.uuid } }) await prisma.documentTypes.findFirstOrThrow({ where: { name: documentType_.name, office_uuid: office.uid } })
).uuid; ).uuid;
let documentTypeWithSameNameAndOffice: DocumentType = JSON.parse(JSON.stringify(documentType_)); let documentTypeWithSameNameAndOffice: DocumentType = JSON.parse(JSON.stringify(documentType_));
documentTypeWithSameNameAndOffice.office.uid = office1.uuid; documentTypeWithSameNameAndOffice.office.uid = office.uid;
documentTypeWithSameNameAndOffice.name = documentType.name; documentTypeWithSameNameAndOffice.name = documentType.name;
// try to duplicate document type in a given office // try to duplicate document type in a given office
@ -168,17 +141,17 @@ describe("test update function", () => {
it("should update a document type office membership", async () => { it("should update a document type office membership", async () => {
const documentTypeCreated = await prisma.documentTypes.findFirstOrThrow({ const documentTypeCreated = await prisma.documentTypes.findFirstOrThrow({
where: { name: documentType_.name, office_uuid: office1.uuid }, where: { name: documentType_.name, office_uuid: office.uid },
}); });
expect(documentTypeCreated.name).toEqual(documentType_.name); expect(documentTypeCreated.name).toEqual(documentType_.name);
expect(documentTypeCreated.public_description).toEqual(documentType_.public_description); expect(documentTypeCreated.public_description).toEqual(documentType_.public_description);
expect(documentTypeCreated.private_description).toEqual(documentType_.private_description); expect(documentTypeCreated.private_description).toEqual(documentType_.private_description);
expect(documentTypeCreated.archived_at).toBeNull(); expect(documentTypeCreated.archived_at).toBeNull();
expect(documentTypeCreated.office_uuid).toEqual(office1.uuid); expect(documentTypeCreated.office_uuid).toEqual(office.uid);
let documentTypeTransferedToNewOffice: DocumentType = JSON.parse(JSON.stringify(documentType_)); let documentTypeTransferedToNewOffice: DocumentType = JSON.parse(JSON.stringify(documentType_));
documentTypeTransferedToNewOffice.office.uid = office2.uuid; documentTypeTransferedToNewOffice.office.uid = office_.uid;
// update the last document type updated with a new office membership // update the last document type updated with a new office membership
const documentTypeUpdated = await DocumentTypesServiceTest.update(documentTypeCreated.uuid, documentTypeTransferedToNewOffice); const documentTypeUpdated = await DocumentTypesServiceTest.update(documentTypeCreated.uuid, documentTypeTransferedToNewOffice);
@ -187,22 +160,22 @@ describe("test update function", () => {
expect(documentTypeUpdated.public_description).toEqual(documentType_.public_description); expect(documentTypeUpdated.public_description).toEqual(documentType_.public_description);
expect(documentTypeUpdated.private_description).toEqual(documentType_.private_description); expect(documentTypeUpdated.private_description).toEqual(documentType_.private_description);
expect(documentTypeUpdated.archived_at).toBeNull(); expect(documentTypeUpdated.archived_at).toBeNull();
expect(documentTypeUpdated.office_uuid).toEqual(office2.uuid); expect(documentTypeUpdated.office_uuid).toEqual(office_.uid);
}); });
it("should archivate a document type", async () => { it("should archivate a document type", async () => {
const documentTypeCreated = await prisma.documentTypes.findFirstOrThrow({ const documentTypeCreated = await prisma.documentTypes.findFirstOrThrow({
where: { name: documentType_.name, office_uuid: office2.uuid }, where: { name: documentType_.name, office_uuid: office_.uid },
}); });
expect(documentTypeCreated.name).toEqual(documentType_.name); expect(documentTypeCreated.name).toEqual(documentType_.name);
expect(documentTypeCreated.public_description).toEqual(documentType_.public_description); expect(documentTypeCreated.public_description).toEqual(documentType_.public_description);
expect(documentTypeCreated.private_description).toEqual(documentType_.private_description); expect(documentTypeCreated.private_description).toEqual(documentType_.private_description);
expect(documentTypeCreated.archived_at).toBeNull(); expect(documentTypeCreated.archived_at).toBeNull();
expect(documentTypeCreated.office_uuid).toEqual(office2.uuid); expect(documentTypeCreated.office_uuid).toEqual(office_.uid);
let documentTypeArchivated: DocumentType = JSON.parse(JSON.stringify(documentType_)); let documentTypeArchivated: DocumentType = JSON.parse(JSON.stringify(documentType_));
documentTypeArchivated.office.uid = office2.uuid; documentTypeArchivated.office.uid = office_.uid;
const currentDate = new Date(Date.now()); const currentDate = new Date(Date.now());
documentTypeArchivated.archived_at = currentDate; documentTypeArchivated.archived_at = currentDate;
// archivate a document type by giving a non null date for archivated_at attribute // archivate a document type by giving a non null date for archivated_at attribute
@ -211,22 +184,22 @@ describe("test update function", () => {
expect(documentTypeUpdated.public_description).toEqual(documentType_.public_description); expect(documentTypeUpdated.public_description).toEqual(documentType_.public_description);
expect(documentTypeUpdated.private_description).toEqual(documentType_.private_description); expect(documentTypeUpdated.private_description).toEqual(documentType_.private_description);
expect(documentTypeUpdated.archived_at).toEqual(currentDate); expect(documentTypeUpdated.archived_at).toEqual(currentDate);
expect(documentTypeUpdated.office_uuid).toEqual(office2.uuid); expect(documentTypeUpdated.office_uuid).toEqual(office_.uid);
}); });
it("should unarchivate a document type", async () => { it("should unarchivate a document type", async () => {
const documentTypeCreated = await prisma.documentTypes.findFirstOrThrow({ const documentTypeCreated = await prisma.documentTypes.findFirstOrThrow({
where: { name: documentType_.name, office_uuid: office2.uuid }, where: { name: documentType_.name, office_uuid: office_.uid },
}); });
expect(documentTypeCreated.name).toEqual(documentType_.name); expect(documentTypeCreated.name).toEqual(documentType_.name);
expect(documentTypeCreated.public_description).toEqual(documentType_.public_description); expect(documentTypeCreated.public_description).toEqual(documentType_.public_description);
expect(documentTypeCreated.private_description).toEqual(documentType_.private_description); expect(documentTypeCreated.private_description).toEqual(documentType_.private_description);
expect(documentTypeCreated.archived_at).not.toBeNull(); expect(documentTypeCreated.archived_at).not.toBeNull();
expect(documentTypeCreated.office_uuid).toEqual(office2.uuid); expect(documentTypeCreated.office_uuid).toEqual(office_.uid);
let documentTypeUnarchivated: DocumentType = JSON.parse(JSON.stringify(documentType_)); let documentTypeUnarchivated: DocumentType = JSON.parse(JSON.stringify(documentType_));
documentTypeUnarchivated.office.uid = office2.uuid; documentTypeUnarchivated.office.uid = office_.uid;
// unarchivate a document type by giving a null date for archivated_at attribute // unarchivate a document type by giving a null date for archivated_at attribute
const documentTypeUpdated = await DocumentTypesServiceTest.update(documentTypeCreated.uuid, documentTypeUnarchivated); const documentTypeUpdated = await DocumentTypesServiceTest.update(documentTypeCreated.uuid, documentTypeUnarchivated);
@ -235,7 +208,7 @@ describe("test update function", () => {
expect(documentTypeUpdated.public_description).toEqual(documentType_.public_description); expect(documentTypeUpdated.public_description).toEqual(documentType_.public_description);
expect(documentTypeUpdated.private_description).toEqual(documentType_.private_description); expect(documentTypeUpdated.private_description).toEqual(documentType_.private_description);
expect(documentTypeUpdated.archived_at).toBeNull(); expect(documentTypeUpdated.archived_at).toBeNull();
expect(documentTypeUpdated.office_uuid).toEqual(office2.uuid); expect(documentTypeUpdated.office_uuid).toEqual(office_.uid);
}); });
}); });
@ -252,23 +225,23 @@ describe("test get function", () => {
expect(documentTypes[0]?.public_description).toEqual(documentType_.public_description); expect(documentTypes[0]?.public_description).toEqual(documentType_.public_description);
expect(documentTypes[0]?.private_description).toEqual(documentType_.private_description); expect(documentTypes[0]?.private_description).toEqual(documentType_.private_description);
expect(documentTypes[0]?.archived_at).toBeNull(); expect(documentTypes[0]?.archived_at).toBeNull();
expect(documentTypes[0]?.office_uuid).toEqual(office2.uuid); expect(documentTypes[0]?.office_uuid).toEqual(office_.uid);
expect(documentTypes[1]?.name).toEqual(documentType.name); expect(documentTypes[1]?.name).toEqual(documentType.name);
expect(documentTypes[1]?.public_description).toEqual(documentType.public_description); expect(documentTypes[1]?.public_description).toEqual(documentType.public_description);
expect(documentTypes[1]?.private_description).toEqual(documentType.private_description); expect(documentTypes[1]?.private_description).toEqual(documentType.private_description);
expect(documentTypes[1]?.archived_at).toBeNull(); expect(documentTypes[1]?.archived_at).toBeNull();
expect(documentTypes[1]?.office_uuid).toEqual(office1.uuid); expect(documentTypes[1]?.office_uuid).toEqual(office.uid);
expect(documentTypes[2]?.name).toEqual(documentType.name); expect(documentTypes[2]?.name).toEqual(documentType.name);
expect(documentTypes[2]?.public_description).toEqual(documentType.public_description); expect(documentTypes[2]?.public_description).toEqual(documentType.public_description);
expect(documentTypes[2]?.private_description).toEqual(documentType.private_description); expect(documentTypes[2]?.private_description).toEqual(documentType.private_description);
expect(documentTypes[2]?.archived_at).toBeNull(); expect(documentTypes[2]?.archived_at).toBeNull();
expect(documentTypes[2]?.office_uuid).toEqual(office2.uuid); expect(documentTypes[2]?.office_uuid).toEqual(office_.uid);
}); });
it("should return an array of DocumentTypes per offices", async () => { it("should return an array of DocumentTypes per offices", async () => {
const documentTypesForFirstOffice = await DocumentTypesServiceTest.get({ where: { office: office1 }, orderBy: { name: "asc" } }); const documentTypesForFirstOffice = await DocumentTypesServiceTest.get({ where: { office: {uuid : office.uid }}, orderBy: { name: "asc" } });
expect(documentTypesForFirstOffice.length).toEqual(1); expect(documentTypesForFirstOffice.length).toEqual(1);
@ -277,9 +250,9 @@ describe("test get function", () => {
expect(documentTypesForFirstOffice[0]?.public_description).toEqual(documentType.public_description); expect(documentTypesForFirstOffice[0]?.public_description).toEqual(documentType.public_description);
expect(documentTypesForFirstOffice[0]?.private_description).toEqual(documentType.private_description); expect(documentTypesForFirstOffice[0]?.private_description).toEqual(documentType.private_description);
expect(documentTypesForFirstOffice[0]?.archived_at).toBeNull(); expect(documentTypesForFirstOffice[0]?.archived_at).toBeNull();
expect(documentTypesForFirstOffice[0]?.office_uuid).toEqual(office1.uuid); expect(documentTypesForFirstOffice[0]?.office_uuid).toEqual(office.uid);
const documentTypesForSecondOffice = await DocumentTypesServiceTest.get({ where: { office: office2 }, orderBy: { name: "asc" } }); const documentTypesForSecondOffice = await DocumentTypesServiceTest.get({ where: { office: {uuid : office_.uid }}, orderBy: { name: "asc" } });
expect(documentTypesForSecondOffice.length).toEqual(2); expect(documentTypesForSecondOffice.length).toEqual(2);
@ -288,12 +261,12 @@ describe("test get function", () => {
expect(documentTypesForSecondOffice[0]?.public_description).toEqual(documentType_.public_description); expect(documentTypesForSecondOffice[0]?.public_description).toEqual(documentType_.public_description);
expect(documentTypesForSecondOffice[0]?.private_description).toEqual(documentType_.private_description); expect(documentTypesForSecondOffice[0]?.private_description).toEqual(documentType_.private_description);
expect(documentTypesForSecondOffice[0]?.archived_at).toBeNull(); expect(documentTypesForSecondOffice[0]?.archived_at).toBeNull();
expect(documentTypesForSecondOffice[0]?.office_uuid).toEqual(office2.uuid); expect(documentTypesForSecondOffice[0]?.office_uuid).toEqual(office_.uid);
expect(documentTypesForSecondOffice[1]?.name).toEqual(documentType.name); expect(documentTypesForSecondOffice[1]?.name).toEqual(documentType.name);
expect(documentTypesForSecondOffice[1]?.public_description).toEqual(documentType.public_description); expect(documentTypesForSecondOffice[1]?.public_description).toEqual(documentType.public_description);
expect(documentTypesForSecondOffice[1]?.private_description).toEqual(documentType.private_description); expect(documentTypesForSecondOffice[1]?.private_description).toEqual(documentType.private_description);
expect(documentTypesForSecondOffice[1]?.archived_at).toBeNull(); expect(documentTypesForSecondOffice[1]?.archived_at).toBeNull();
expect(documentTypesForSecondOffice[1]?.office_uuid).toEqual(office2.uuid); expect(documentTypesForSecondOffice[1]?.office_uuid).toEqual(office_.uid);
}); });
}); });

View File

@ -1,273 +1,288 @@
import "module-alias/register"; import "module-alias/register";
import "reflect-metadata"; import "reflect-metadata";
import { PrismaClient, Offices, DocumentTypes, DeedTypes, DeedTypeHasDocumentTypes } from "prisma/prisma-client"; import { OfficeFolderHasCustomers, OfficeFolderHasStakeholders, PrismaClient } from "prisma/prisma-client";
import { deedType, documentType, office } from "./MockedData"; import {
// import Container from "typedi"; customer,
// import OfficeFoldersRepository from "@Repositories/OfficeFoldersRepository"; customer_,
// import OfficeFoldersHasStakeholderRepository from "@Repositories/OfficeFoldersHasStakeholderRepository"; deedType,
// import OfficeFoldersHasCustomerRepository from "@Repositories/OfficeFoldersHasCustomerRepository"; documentType,
// import CustomersService from "@Services/super-admin/CustomersService/CustomersService"; documentType_,
// import OfficeFolderService from "@Services/super-admin/OfficeFoldersService/OfficeFoldersService"; office,
// import DocumentsRepository from "@Repositories/DocumentsRepository"; officeFolder,
officeFolder_,
user,
user_,
} from "@Test/config/MockedData";
import Container from "typedi";
import OfficeFoldersRepository from "@Repositories/OfficeFoldersRepository";
import OfficeFolderService from "@Services/super-admin/OfficeFoldersService/OfficeFoldersService";
import { initCustomers, initDeedType, initDocumentType, initOffice, initUsers } from "@Test/config/Init";
import { OfficeFolder } from "le-coffre-resources/dist/SuperAdmin";
import DeedTypesService from "@Services/super-admin/DeedTypesService/DeedTypesService";
const prisma = new PrismaClient(); const prisma = new PrismaClient();
// const OfficeFolderServiceTest = new OfficeFolderService( const OfficeFolderServiceTest = new OfficeFolderService(Container.get(OfficeFoldersRepository), Container.get(DeedTypesService));
// Container.get(OfficeFoldersRepository),
// Container.get(OfficeFoldersHasStakeholderRepository),
// Container.get(OfficeFoldersHasCustomerRepository),
// Container.get(CustomersService),
// Container.get(DocumentsRepository),
// );
let office1: Offices;
let documentType1: DocumentTypes;
//let documentType2: DocumentTypes;
let deedType1: DeedTypes;
let deedType1HasDocumentType1: DeedTypeHasDocumentTypes;
beforeAll(async () => { beforeAll(async () => {
office1 = await prisma.offices.create({ office.uid = (await initOffice(office)).uuid;
data: { documentType.uid = (await initDocumentType(documentType, office)).uuid;
idNot: office.idNot, documentType_.uid = (await initDocumentType(documentType_, office)).uuid;
name: office.name, deedType.uid = (await initDeedType(deedType, office, [documentType.uid])).uuid;
crpcen: office.crpcen, user.uid = (await initUsers(user)).uuid;
address: { user_.uid = (await initUsers(user_)).uuid;
create: { customer.uid = (await initCustomers(customer)).uuid;
address: office.address.address, customer_.uid = (await initCustomers(customer_)).uuid;
zip_code: office.address.zip_code,
city: office.address.city,
},
},
},
});
documentType1 = await prisma.documentTypes.create({
data: {
name: documentType.name,
public_description: documentType.public_description,
private_description: documentType.private_description,
archived_at: null,
office_uuid: office1.uuid,
},
});
// documentType2 = await prisma.documentTypes.create({
// data: {
// name: documentType_.name,
// public_description: documentType_.public_description,
// private_description: documentType_.private_description,
// archived_at: null,
// office_uuid: office1.uuid,
// },
// });
deedType1 = await prisma.deedTypes.create({
data: {
name: deedType.name,
description: deedType.description,
archived_at: null,
office_uuid: office1.uuid,
},
});
deedType1HasDocumentType1 = await prisma.deedTypeHasDocumentTypes.create({
data: {
deed_type_uuid: deedType1.uuid,
document_type_uuid: documentType1.uuid,
},
});
await prisma.deedTypes.update({
where: { uuid: deedType1.uuid },
data: {
deed_type_has_document_types: {
connect: {
uuid: deedType1HasDocumentType1.uuid,
},
},
},
});
}); });
afterAll(async () => { afterAll(async () => {
/*
* Clean database after all tests execution.
* Due to cascade deletion, if addresses are deleted, all items following tables are dropped: contacts, customers, offices
*/
const deleteDeedTypes = prisma.deedTypes.deleteMany(); const deleteDeedTypes = prisma.deedTypes.deleteMany();
const deleteOffices = prisma.offices.deleteMany(); const deleteAddresses = prisma.addresses.deleteMany();
await prisma.$transaction([deleteDeedTypes, deleteOffices]); await prisma.$transaction([deleteDeedTypes, deleteAddresses]);
await prisma.$disconnect(); await prisma.$disconnect();
}); });
describe("test create function", () => { describe("test create function", () => {
it("should not create a new office folder if deed type is unknown", async () => { it("should not create a new office folder if deed type is unknown", async () => {
let officeFolderWithoutDeedTypeUid: OfficeFolder = JSON.parse(JSON.stringify(officeFolder));
officeFolderWithoutDeedTypeUid.deed.deed_type.uid = "random uuid";
// try to create a new deed with unknown deed type // try to create a new deed with unknown deed type
// async function createDeedWithUnknownDeedType() { async function createOfficeFolderWithUnknownDeedType() {
// await OfficeFolderServiceTest.create(officeFolder, deedType); await OfficeFolderServiceTest.create(officeFolderWithoutDeedTypeUid);
// } }
// await expect(createDeedWithUnknownDeedType).rejects.toThrow("deed type not found"); await expect(createOfficeFolderWithUnknownDeedType).rejects.toThrow();
}); });
// it("should create a new office folder based on existing deed type", async () => { it("should create a new office folder based on existing deed type", async () => {
// let deedTypeWithUid: DeedType = JSON.parse(JSON.stringify(deedType)); const officeFolderCreated = await OfficeFolderServiceTest.create(officeFolder);
// deedTypeWithUid.uid = deedType1.uuid;
// const officeFolderCreated = await OfficeFolderServiceTest.create(officeFolder, deedTypeWithUid);
// expect(officeFolderCreated.office_uuid).toEqual(office1.uuid); const deedCreated = await prisma.deeds.findUniqueOrThrow({ where: { uuid: officeFolderCreated.deed_uuid } });
// expect(officeFolderCreated.deed_uuid.).toEqual(office1.uuid);
// });
// it("should have by default the same document types as its deed type ", async () => { expect(officeFolderCreated.name).toEqual(officeFolder.name);
// const deedWithDocumentTypes = await prisma.deeds.findFirstOrThrow({ include: { deed_has_document_types: true } }); expect(officeFolderCreated.folder_number).toEqual(officeFolder.folder_number);
// expect(deedWithDocumentTypes.deed_has_document_types.length).toEqual(1); expect(officeFolderCreated.description).toEqual(officeFolder.description);
// expect(deedWithDocumentTypes.deed_has_document_types[0]?.document_type_uuid).toEqual(documentType1.uuid); expect(officeFolderCreated.archived_description).toEqual(null);
// }); expect(officeFolderCreated.status).toEqual("LIVE");
expect(officeFolderCreated.office_uuid).toEqual(officeFolder.office.uid);
expect(deedCreated.deed_type_uuid).toEqual(officeFolder.deed.deed_type.uid);
});
// it("should create a the same deed based on existing deed type", async () => { it("should contains stakeholders", async () => {
// let deedTypeWithUid: DeedType = JSON.parse(JSON.stringify(deedType)); const officeFolderCreated = await prisma.officeFolders.findFirstOrThrow({
// deedTypeWithUid.uid = deedType1.uuid; include: { office_folder_has_stakeholder: true },
// const deedCreated = await OfficeFolderServiceTest.create(deedTypeWithUid); });
const stakeholderRelation = await prisma.officeFolderHasStakeholders.findUniqueOrThrow({
where: {
office_folder_uuid_user_stakeholder_uuid: {
user_stakeholder_uuid: user.uid!,
office_folder_uuid: officeFolderCreated.uuid,
},
},
});
const stakeholderRelation_ = await prisma.officeFolderHasStakeholders.findUniqueOrThrow({
where: {
office_folder_uuid_user_stakeholder_uuid: {
user_stakeholder_uuid: user_.uid!,
office_folder_uuid: officeFolderCreated.uuid,
},
},
});
expect(officeFolderCreated.office_folder_has_stakeholder.length).toEqual(2);
const stakeholder: OfficeFolderHasStakeholders = {
uuid: stakeholderRelation.uuid,
office_folder_uuid: officeFolderCreated.uuid,
user_stakeholder_uuid: user.uid!,
created_at: officeFolderCreated.created_at,
updated_at: officeFolderCreated.updated_at,
};
const stakeholder_: OfficeFolderHasStakeholders = {
uuid: stakeholderRelation_.uuid,
office_folder_uuid: officeFolderCreated.uuid,
user_stakeholder_uuid: user_.uid!,
created_at: officeFolderCreated.created_at,
updated_at: officeFolderCreated.updated_at,
};
expect(officeFolderCreated.office_folder_has_stakeholder).toEqual(expect.arrayContaining([stakeholder, stakeholder_]));
});
// expect(deedCreated.deed_type_uuid).toEqual(deedType1.uuid); it("should not create a new office folder with folder number already created", async () => {
// }); let officeFolderWithSameFolderNumber: OfficeFolder = JSON.parse(JSON.stringify(officeFolder_));
officeFolderWithSameFolderNumber.folder_number = officeFolder.folder_number;
// try to create a new deed with unknown deed type
async function createOfficeFolderWithSameFolderNumber() {
await OfficeFolderServiceTest.create(officeFolderWithSameFolderNumber);
}
await expect(createOfficeFolderWithSameFolderNumber).rejects.toThrow();
});
// it("should not create a new deed based on archivated deed type", async () => { it("should not create a new office folder if deed type is archived", async () => {
// let deedTypeArchivated: DeedType = JSON.parse(JSON.stringify(deedType)); await prisma.deedTypes.update({ where: { uuid: deedType.uid }, data: { archived_at: new Date(Date.now()) } });
// deedTypeArchivated.uid = deedType1.uuid; // try to create a new deed with archivated deed type
async function createDeedWithArchivatedDeedType() {
// await prisma.deedTypes.update({ await OfficeFolderServiceTest.create(officeFolder);
// where: { uuid: deedType1.uuid }, }
// data: { await expect(createDeedWithArchivatedDeedType).rejects.toThrow("deed type is archived");
// archived_at: new Date(Date.now()), });
// },
// });
// // try to create a new deed with archivated deed type
// async function createDeedWithArchivatedDeedType() {
// await OfficeFolderServiceTest.create(deedTypeArchivated);
// }
// await expect(createDeedWithArchivatedDeedType).rejects.toThrow("deed type is archived");
// });
}); });
// describe("test addDocumentTypes function", () => { describe("test update function", () => {
// it("should add document types to a deed", async () => { it("should add customers", async () => {
// const deedUid = (await prisma.deeds.findFirstOrThrow({ where: { deed_type_uuid: deedType1.uuid } })).uuid; let officeFolderCreated = await prisma.officeFolders.findFirstOrThrow({
// const documentsToAdd = [documentType1.uuid, documentType2.uuid]; include: { office_folder_has_customers: true },
// await OfficeFolderServiceTest.addDocumentTypes(deedUid, documentsToAdd); });
// const deed = await prisma.deeds.findFirstOrThrow({ expect(officeFolderCreated.office_folder_has_customers).toEqual([]);
// where: { // mocked data contains the customers
// uuid: deedUid, await OfficeFolderServiceTest.update(officeFolderCreated.uuid, officeFolder);
// },
// include: {
// deed_has_document_types: true,
// },
// });
// expect(deed.deed_has_document_types.length).toEqual(2);
// });
// it("should not add document types to a deed type that already has those document types ", async () => { const customerRelation = await prisma.officeFolderHasCustomers.findUniqueOrThrow({
// const deedUid = (await prisma.deeds.findFirstOrThrow({ where: { deed_type_uuid: deedType1.uuid } })).uuid; where: {
// let deedHasDocumentTypes = await prisma.deedHasDocumentTypes.findMany({ where: { deed_uuid: deedUid } }); office_folder_uuid_customer_uuid: {
// expect(deedHasDocumentTypes.length).toEqual(2); customer_uuid: customer.uid!,
office_folder_uuid: officeFolderCreated.uuid,
},
},
});
const customerRelation_ = await prisma.officeFolderHasCustomers.findUniqueOrThrow({
where: {
office_folder_uuid_customer_uuid: {
customer_uuid: customer_.uid!,
office_folder_uuid: officeFolderCreated.uuid,
},
},
});
// const documentsToAdd = [documentType1.uuid, documentType2.uuid]; officeFolderCreated = await prisma.officeFolders.findFirstOrThrow({
// //we expect deedTypeHasDocumentTypes service to skip duplicates without throwing error include: { office_folder_has_customers: true },
// await OfficeFolderServiceTest.addDocumentTypes(deedUid, documentsToAdd); });
// deedHasDocumentTypes = await prisma.deedHasDocumentTypes.findMany({ where: { deed_uuid: deedUid } }); expect(officeFolderCreated.office_folder_has_customers.length).toEqual(2);
// expect(deedHasDocumentTypes.length).toEqual(2); const officeFolderHasCustomer: OfficeFolderHasCustomers = {
// }); uuid: customerRelation.uuid,
// }); office_folder_uuid: officeFolderCreated.uuid,
// describe("test removeDocumentTypes function", () => { customer_uuid: customer.uid!,
// it("should remove document types from a deed type", async () => { created_at: customerRelation.created_at,
// const deedUid = (await prisma.deeds.findFirstOrThrow({ where: { deed_type_uuid: deedType1.uuid } })).uuid; updated_at: customerRelation.updated_at,
// const documentsToRemove = [documentType1.uuid]; };
// await OfficeFolderServiceTest.removeDocumentTypes(deedUid, documentsToRemove); const officeFolderHasCustomer_: OfficeFolderHasCustomers = {
uuid: customerRelation_.uuid,
office_folder_uuid: officeFolderCreated.uuid,
customer_uuid: customer_.uid!,
created_at: customerRelation_.created_at,
updated_at: customerRelation_.updated_at,
};
expect(officeFolderCreated.office_folder_has_customers).toEqual(
expect.arrayContaining([officeFolderHasCustomer, officeFolderHasCustomer_]),
);
});
// const deedWithDocumentTypeRelations = await prisma.deeds.findFirstOrThrow({ it("should remove customers", async () => {
// where: { let officeFolderCreated = await prisma.officeFolders.findFirstOrThrow({
// uuid: deedUid, include: { office_folder_has_customers: true },
// }, });
// include: {
// deed_has_document_types: true,
// },
// });
// expect(deedWithDocumentTypeRelations.deed_has_document_types.length).toEqual(1);
// expect(deedWithDocumentTypeRelations.deed_has_document_types[0]?.document_type_uuid).toEqual(documentType2.uuid);
// });
// it("should not remove document types from a deed type is they were not linked", async () => {
// let deedWithOneDocumentType = await prisma.deeds.findFirstOrThrow({
// where: { deed_type_uuid: deedType1.uuid },
// include: {
// deed_has_document_types: true,
// },
// });
// expect(deedWithOneDocumentType.deed_has_document_types.length).toEqual(1);
// const documentsToRemove = [documentType1.uuid]; expect(officeFolderCreated.office_folder_has_customers.length).toEqual(2);
// async function removeDocumentTypeNotLinkedToDeedType() { let officeFolderWithLessCustomers: OfficeFolder = JSON.parse(JSON.stringify(officeFolder));
// await OfficeFolderServiceTest.removeDocumentTypes(deedWithOneDocumentType.uuid, documentsToRemove); officeFolderWithLessCustomers.office_folder_has_customers!.pop();
// } // mocked data contains the customers
// await expect(removeDocumentTypeNotLinkedToDeedType).rejects.toThrow(); await OfficeFolderServiceTest.update(officeFolderCreated.uuid, officeFolderWithLessCustomers);
// });
// });
// describe("test removeDocumentType function", () => {
// it("should remove only one document type from a deed type", async () => {
// let deedWithOneDocumentType = await prisma.deeds.findFirstOrThrow({
// where: { deed_type_uuid: deedType1.uuid },
// include: {
// deed_has_document_types: true,
// },
// });
// expect(deedWithOneDocumentType.deed_has_document_types.length).toEqual(1);
// const documentToRemove = documentType2.uuid; const customerRelation = await prisma.officeFolderHasCustomers.findUniqueOrThrow({
// await OfficeFolderServiceTest.removeDocumentType(deedWithOneDocumentType.uuid, documentToRemove); where: {
office_folder_uuid_customer_uuid: {
customer_uuid: customer.uid!,
office_folder_uuid: officeFolderCreated.uuid,
},
},
});
// deedWithOneDocumentType = await prisma.deeds.findFirstOrThrow({ officeFolderCreated = await prisma.officeFolders.findFirstOrThrow({
// where: { include: { office_folder_has_customers: true },
// uuid: deedWithOneDocumentType.uuid, });
// },
// include: {
// deed_has_document_types: true,
// },
// });
// expect(deedWithOneDocumentType.deed_has_document_types.length).toEqual(0);
// });
// });
// describe("test addDocumentType function", () => {
// it("should add only one document type to a deed type", async () => {
// const deedUid = (await prisma.deeds.findFirstOrThrow({ where: { deed_type_uuid: deedType1.uuid } })).uuid;
// const documentToAdd = documentType1.uuid;
// await OfficeFolderServiceTest.addDocumentType(deedUid, documentToAdd);
// const deed = await prisma.deeds.findFirstOrThrow({ expect(officeFolderCreated.office_folder_has_customers.length).toEqual(1);
// where: { const officeFolderHasCustomer: OfficeFolderHasCustomers = {
// uuid: deedUid, uuid: customerRelation.uuid,
// }, office_folder_uuid: officeFolderCreated.uuid,
// include: { customer_uuid: customer.uid!,
// deed_has_document_types: true, created_at: customerRelation.created_at,
// }, updated_at: customerRelation.updated_at,
// }); };
// expect(deed.deed_has_document_types.length).toEqual(1); expect(officeFolderCreated.office_folder_has_customers).toEqual([officeFolderHasCustomer]);
// expect(deed.deed_has_document_types[0]?.document_type_uuid).toEqual(documentType1.uuid); });
// }); it("should remove stakeholders", async () => {
// }); let officeFolderCreated = await prisma.officeFolders.findFirstOrThrow({
include: { office_folder_has_stakeholder: true },
});
// describe("test get function", () => { expect(officeFolderCreated.office_folder_has_stakeholder.length).toEqual(2);
// it("should return an array of Deeds", async () => {
// const deeds = await OfficeFolderServiceTest.get({});
// // verify result typing let officeFolderWithLessStakeholders: OfficeFolder = JSON.parse(JSON.stringify(officeFolder));
// expect(deeds).toBeInstanceOf(Array<Deed>); officeFolderWithLessStakeholders.office_folder_has_stakeholder!.pop();
// expect(deeds.length).toEqual(2); // mocked data contains the customers
await OfficeFolderServiceTest.update(officeFolderCreated.uuid, officeFolderWithLessStakeholders);
// // verify result content const stakeholderRelation = await prisma.officeFolderHasStakeholders.findUniqueOrThrow({
// expect(deeds[0]?.deed_type_uuid).toEqual(deedType1.uuid); where: {
// expect(deeds[1]?.deed_type_uuid).toEqual(deedType1.uuid); office_folder_uuid_user_stakeholder_uuid: {
// }); user_stakeholder_uuid: user.uid!,
// }); office_folder_uuid: officeFolderCreated.uuid,
},
},
});
officeFolderCreated = await prisma.officeFolders.findFirstOrThrow({
include: { office_folder_has_stakeholder: true },
});
expect(officeFolderCreated.office_folder_has_stakeholder.length).toEqual(1);
const officeFolderHasStakeholder: OfficeFolderHasStakeholders = {
uuid: stakeholderRelation.uuid,
office_folder_uuid: officeFolderCreated.uuid,
user_stakeholder_uuid: user.uid!,
created_at: stakeholderRelation.created_at,
updated_at: stakeholderRelation.updated_at,
};
expect(officeFolderCreated.office_folder_has_stakeholder).toEqual([officeFolderHasStakeholder]);
});
it("should archivate an office folder", async () => {
const officeFolderCreated = await prisma.officeFolders.findFirstOrThrow({});
let officeFolderArchived: OfficeFolder = JSON.parse(JSON.stringify(officeFolder));
// mocked data for this office folder is "ARCHIVATED" by default
officeFolderArchived.archived_description = "folder complete";
const officeFolderUpdated = await OfficeFolderServiceTest.update(officeFolderCreated.uuid, officeFolderArchived);
expect(officeFolderUpdated.archived_description).toEqual(officeFolderArchived.archived_description);
expect(officeFolderUpdated.status).toEqual("ARCHIVED");
});
it("should unarchivate an office folder", async () => {
const officeFolderCreated = await prisma.officeFolders.findFirstOrThrow({});
expect(officeFolderCreated.archived_description).toEqual("folder complete");
expect(officeFolderCreated.status).toEqual("ARCHIVED");
let officeFolderUnarchived: OfficeFolder = JSON.parse(JSON.stringify(officeFolder));
officeFolderUnarchived.status = "LIVE";
const officeFolderUpdated = await OfficeFolderServiceTest.update(officeFolderCreated.uuid, officeFolderUnarchived);
expect(officeFolderUpdated.archived_description).toBeNull();
expect(officeFolderUpdated.status).toEqual("LIVE");
});
});
describe("test get function", () => {
it("should return an array of officeFolders", async () => {
const officeFolders = await OfficeFolderServiceTest.get({});
// verify result typing
expect(officeFolders).toBeInstanceOf(Array<OfficeFolder>);
expect(officeFolders.length).toEqual(1);
});
});

View File

@ -3,7 +3,7 @@ import "reflect-metadata";
import User from "le-coffre-resources/dist/SuperAdmin"; import User from "le-coffre-resources/dist/SuperAdmin";
import UsersService from "@Services/super-admin/UsersService/UsersService"; import UsersService from "@Services/super-admin/UsersService/UsersService";
import { PrismaClient } from "@prisma/client"; import { PrismaClient } from "@prisma/client";
import { user, userContact, userContact_, user_ } from "./MockedData"; import { user, userContact, userContact_, user_ } from "@Test/config/MockedData";
import UsersRepository from "@Repositories/UsersRepository"; import UsersRepository from "@Repositories/UsersRepository";
import Container from "typedi"; import Container from "typedi";
@ -38,9 +38,9 @@ describe("test create function", () => {
// verify if user address is created in db // verify if user address is created in db
const addressForContactCreated = await prisma.addresses.findUnique({ where: { uuid: contactCreated?.address_uuid } }); const addressForContactCreated = await prisma.addresses.findUnique({ where: { uuid: contactCreated?.address_uuid } });
expect(addressForContactCreated?.address).toEqual(user.contact.address.address); expect(addressForContactCreated?.address).toEqual(user.contact.address?.address);
expect(addressForContactCreated?.zip_code).toEqual(user.contact.address.zip_code); expect(addressForContactCreated?.zip_code).toEqual(user.contact.address?.zip_code);
expect(addressForContactCreated?.city).toEqual(user.contact.address.city); expect(addressForContactCreated?.city).toEqual(user.contact.address?.city);
// verify if user office is created in db // verify if user office is created in db
const officeCreated = await prisma.offices.findUnique({ where: { uuid: userCreated.office_uuid } }); const officeCreated = await prisma.offices.findUnique({ where: { uuid: userCreated.office_uuid } });
@ -107,9 +107,9 @@ describe("test create function", () => {
// verify if user_ address is created in db // verify if user_ address is created in db
const addressForContactCreated = await prisma.addresses.findUnique({ where: { uuid: contactCreated?.address_uuid } }); const addressForContactCreated = await prisma.addresses.findUnique({ where: { uuid: contactCreated?.address_uuid } });
expect(addressForContactCreated?.address).toEqual(user.contact.address.address); expect(addressForContactCreated?.address).toEqual(user.contact.address?.address);
expect(addressForContactCreated?.zip_code).toEqual(user.contact.address.zip_code); expect(addressForContactCreated?.zip_code).toEqual(user.contact.address?.zip_code);
expect(addressForContactCreated?.city).toEqual(user.contact.address.city); expect(addressForContactCreated?.city).toEqual(user.contact.address?.city);
// verify if user joined the existing office // verify if user joined the existing office
const officeJoined = await prisma.offices.findUnique({ where: { uuid: userCreated.office_uuid } }); const officeJoined = await prisma.offices.findUnique({ where: { uuid: userCreated.office_uuid } });
@ -146,9 +146,9 @@ describe("test update function", () => {
// verify if user_ address is created in db // verify if user_ address is created in db
const addressForExistingContact = await prisma.addresses.findUnique({ where: { uuid: existingContact?.address_uuid } }); const addressForExistingContact = await prisma.addresses.findUnique({ where: { uuid: existingContact?.address_uuid } });
expect(addressForExistingContact?.address).toEqual(user_.contact.address.address); expect(addressForExistingContact?.address).toEqual(user_.contact.address?.address);
expect(addressForExistingContact?.zip_code).toEqual(user_.contact.address.zip_code); expect(addressForExistingContact?.zip_code).toEqual(user_.contact.address?.zip_code);
expect(addressForExistingContact?.city).toEqual(user_.contact.address.city); expect(addressForExistingContact?.city).toEqual(user_.contact.address?.city);
// verify if user_ joined the new office // verify if user_ joined the new office
const officeCreated = await prisma.offices.findUnique({ where: { uuid: updatedUser.office_uuid } }); const officeCreated = await prisma.offices.findUnique({ where: { uuid: updatedUser.office_uuid } });