
controllers, repositories, services and associated tests for: Deed types, Deed, Document types, Users and Customers --------- Co-authored-by: Vincent Alamelle <vincent.alamelle@smart-chain.fr> Co-authored-by: OxSaitama <arnaud.daubernatali@smart-chain.fr>
265 lines
8.9 KiB
TypeScript
265 lines
8.9 KiB
TypeScript
import "module-alias/register";
|
|
import "reflect-metadata";
|
|
import { Deed, DeedType } from "le-coffre-resources/dist/SuperAdmin";
|
|
import DeedService from "@Services/super-admin/DeedsService/DeedsService";
|
|
import { PrismaClient, Offices, DocumentTypes, DeedTypes, DeedTypeHasDocumentTypes } from "prisma/prisma-client";
|
|
import { deedType, documentType, documentType_, office } from "./MockedData";
|
|
import DeedsRepository from "@Repositories/DeedsRepository";
|
|
import Container from "typedi";
|
|
import DeedHasDocumentTypesRepository from "@Repositories/DeedsHasDocumentTypesRepository";
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
const DeedServiceTest = new DeedService(Container.get(DeedsRepository), Container.get(DeedHasDocumentTypesRepository));
|
|
|
|
let office1: Offices;
|
|
|
|
let documentType1: DocumentTypes;
|
|
let documentType2: DocumentTypes;
|
|
|
|
let deedType1: DeedTypes;
|
|
|
|
let deedType1HasDocumentType1: DeedTypeHasDocumentTypes;
|
|
|
|
beforeAll(async () => {
|
|
office1 = 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,
|
|
},
|
|
});
|
|
|
|
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 () => {
|
|
const deleteDeedTypes = prisma.deedTypes.deleteMany();
|
|
const deleteOffices = prisma.offices.deleteMany();
|
|
await prisma.$transaction([deleteDeedTypes, deleteOffices]);
|
|
await prisma.$disconnect();
|
|
});
|
|
|
|
describe("test create function", () => {
|
|
it("should not create a new deed if deed type is unknown", async () => {
|
|
// try to create a new deed with unknown deed type
|
|
async function createDeedWithUnknownDeedType() {
|
|
await DeedServiceTest.create(deedType);
|
|
}
|
|
await expect(createDeedWithUnknownDeedType).rejects.toThrow();
|
|
});
|
|
|
|
it("should create a new deed based on existing deed type", async () => {
|
|
let deedTypeWithUid: DeedType = JSON.parse(JSON.stringify(deedType));
|
|
deedTypeWithUid.uid = deedType1.uuid;
|
|
const deedCreated = await DeedServiceTest.create(deedTypeWithUid);
|
|
|
|
expect(deedCreated.deed_type_uuid).toEqual(deedType1.uuid);
|
|
});
|
|
|
|
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 } });
|
|
expect(deedWithDocumentTypes.deed_has_document_types.length).toEqual(1);
|
|
expect(deedWithDocumentTypes.deed_has_document_types[0]?.document_type_uuid).toEqual(documentType1.uuid);
|
|
});
|
|
|
|
it("should create a the same deed based on existing deed type", async () => {
|
|
let deedTypeWithUid: DeedType = JSON.parse(JSON.stringify(deedType));
|
|
deedTypeWithUid.uid = deedType1.uuid;
|
|
const deedCreated = await DeedServiceTest.create(deedTypeWithUid);
|
|
|
|
expect(deedCreated.deed_type_uuid).toEqual(deedType1.uuid);
|
|
});
|
|
|
|
it("should not create a new deed based on archivated deed type", async () => {
|
|
let deedTypeArchivated: DeedType = JSON.parse(JSON.stringify(deedType));
|
|
deedTypeArchivated.uid = deedType1.uuid;
|
|
|
|
await prisma.deedTypes.update({
|
|
where: { uuid: deedType1.uuid },
|
|
data: {
|
|
archived_at: new Date(Date.now()),
|
|
},
|
|
});
|
|
|
|
// try to create a new deed with archivated deed type
|
|
async function createDeedWithArchivatedDeedType() {
|
|
await DeedServiceTest.create(deedTypeArchivated);
|
|
}
|
|
await expect(createDeedWithArchivatedDeedType).rejects.toThrow("deed type is archived");
|
|
});
|
|
});
|
|
|
|
describe("test addDocumentTypes function", () => {
|
|
it("should add document types to a deed", async () => {
|
|
const deedUid = (await prisma.deeds.findFirstOrThrow({ where: { deed_type_uuid: deedType1.uuid } })).uuid;
|
|
const documentsToAdd = [documentType1.uuid, documentType2.uuid];
|
|
await DeedServiceTest.addDocumentTypes(deedUid, documentsToAdd);
|
|
|
|
const deed = await prisma.deeds.findFirstOrThrow({
|
|
where: {
|
|
uuid: deedUid,
|
|
},
|
|
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 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];
|
|
//we expect deedTypeHasDocumentTypes service to skip duplicates without throwing error
|
|
await DeedServiceTest.addDocumentTypes(deedUid, documentsToAdd);
|
|
|
|
deedHasDocumentTypes = await prisma.deedHasDocumentTypes.findMany({ where: { deed_uuid: deedUid } });
|
|
expect(deedHasDocumentTypes.length).toEqual(2);
|
|
});
|
|
});
|
|
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({
|
|
where: {
|
|
uuid: deedUid,
|
|
},
|
|
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];
|
|
|
|
async function removeDocumentTypeNotLinkedToDeedType() {
|
|
await DeedServiceTest.removeDocumentTypes(deedWithOneDocumentType.uuid, documentsToRemove);
|
|
}
|
|
await expect(removeDocumentTypeNotLinkedToDeedType).rejects.toThrow();
|
|
});
|
|
});
|
|
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;
|
|
await DeedServiceTest.removeDocumentType(deedWithOneDocumentType.uuid, documentToRemove);
|
|
|
|
deedWithOneDocumentType = await prisma.deeds.findFirstOrThrow({
|
|
where: {
|
|
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({
|
|
where: {
|
|
uuid: deedUid,
|
|
},
|
|
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);
|
|
});
|
|
});
|
|
|
|
describe("test get function", () => {
|
|
it("should return an array of Deeds", async () => {
|
|
const deeds = await DeedServiceTest.get({});
|
|
|
|
// verify result typing
|
|
expect(deeds).toBeInstanceOf(Array<Deed>);
|
|
expect(deeds.length).toEqual(2);
|
|
|
|
// verify result content
|
|
expect(deeds[0]?.deed_type_uuid).toEqual(deedType1.uuid);
|
|
expect(deeds[1]?.deed_type_uuid).toEqual(deedType1.uuid);
|
|
});
|
|
});
|