refacto customers entry points

This commit is contained in:
OxSaitama 2023-09-29 00:32:19 +02:00
parent 73cb62f200
commit 77578e8d31
12 changed files with 91 additions and 162 deletions

View File

@ -8,6 +8,7 @@ import { validateOrReject } from "class-validator";
import authHandler from "@App/middlewares/AuthHandler";
import ruleHandler from "@App/middlewares/RulesHandler";
import roleHandler from "@App/middlewares/RolesHandler";
import { Prisma } from "@prisma/client";
@Controller()
@Service()
@ -28,6 +29,12 @@ export default class CustomersController extends ApiController {
query = JSON.parse(req.query["q"] as string);
}
const officeId: string = req.body.user.office_Id;
if(query.where?.office_folders?.some?.office_uid) delete query.where.office_folders.some.office_uid;
if(query.where?.office_folders?.some?.office?.uid) delete query.where?.office_folders?.some?.office?.uid;
const customerWhereInput: Prisma.CustomersWhereInput = { ...query.where, office_folders: { some: { office_uid: officeId } }};
query.where = customerWhereInput;
//call service to get prisma entity
const customersEntities = await this.customersService.get(query);

View File

@ -1,79 +0,0 @@
import { Response, Request } from "express";
import { Controller, Get } from "@ControllerPattern/index";
import ApiController from "@Common/system/controller-pattern/ApiController";
import CustomersService from "@Services/customer/CustomersService/CustomersService";
import { Service } from "typedi";
import Customer from "le-coffre-resources/dist/Customer";
import authHandler from "@App/middlewares/AuthHandler";
import ruleHandler from "@App/middlewares/RulesHandler";
@Controller()
@Service()
export default class CustomersController extends ApiController {
constructor(private customersService: CustomersService) {
super();
}
/**
* @description Get all customers
*/
@Get("/api/v1/customer/customers")
protected async get(req: Request, response: Response) {
try {
//get query
let query;
if (req.query["q"]) {
query = JSON.parse(req.query["q"] as string);
}
//call service to get prisma entity
const customersEntities = await this.customersService.get(query);
//Hydrate ressource with prisma entity
const customers = Customer.hydrateArray<Customer>(customersEntities, { strategy: "excludeAll" });
//success
this.httpSuccess(response, customers);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Get a specific customer by uid
*/
@Get("/api/v1/customer/customers/:uid", [authHandler, ruleHandler])
protected async getOneByUid(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
let query;
if (req.query["q"]) {
query = JSON.parse(req.query["q"] as string);
}
const customerEntity = await this.customersService.getByUid(uid, query);
if (!customerEntity) {
this.httpNotFoundRequest(response, "customer not found");
return;
}
//Hydrate ressource with prisma entity
const customer = Customer.hydrate<Customer>(customerEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, customer);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
}

View File

@ -5,6 +5,8 @@ import OfficeFoldersService from "@Services/customer/OfficeFoldersService/Office
import { Service } from "typedi";
import { OfficeFolders, Prisma } from "@prisma/client";
import { OfficeFolder } from "le-coffre-resources/dist/Customer";
import officeFolderHandler from "@App/middlewares/CustomerHandler/FolderHandler";
import authHandler from "@App/middlewares/AuthHandler";
// import authHandler from "@App/middlewares/AuthHandler";
// import ruleHandler from "@App/middlewares/RulesHandler";
// import folderHandler from "@App/middlewares/OfficeMembershipHandlers/FolderHandler";
@ -19,7 +21,7 @@ export default class OfficeFoldersController extends ApiController {
/**
* @description Get all folders
*/
@Get("/api/v1/customer/folders")
@Get("/api/v1/customer/folders", [authHandler])
protected async get(req: Request, response: Response) {
try {
//get query
@ -28,37 +30,14 @@ export default class OfficeFoldersController extends ApiController {
query = JSON.parse(req.query["q"] as string);
}
if (req.query["search"] && typeof req.query["search"] === "string") {
const filter = req.query["search"];
query = {
where: {
OR: [
{
name: { contains: filter, mode: "insensitive" },
},
{
folder_number: { contains: filter, mode: "insensitive" },
},
{
customers: {
some: {
contact: {
OR: [
{ first_name: { contains: filter, mode: "insensitive" } },
{ last_name: { contains: filter, mode: "insensitive" } },
],
},
},
},
},
],
},
};
const customerId: string = req.body.user.customerId;
if(!customerId) {
this.httpBadRequest(response, "No customerId provided");
return;
}
const officeWhereInput: Prisma.OfficesWhereInput = {};
if (!query.where) query.where = { office: officeWhereInput };
query.where.office = officeWhereInput;
if(query.where?.customers) delete query.where.customers;
const officeFolderWhereInput: Prisma.OfficeFoldersWhereInput = { ...query.where, customers: { some: { uid: customerId } }};
query.where = officeFolderWhereInput;
//call service to get prisma entity
const officeFolderEntities: OfficeFolders[] = await this.officeFoldersService.get(query);
@ -79,7 +58,7 @@ export default class OfficeFoldersController extends ApiController {
* @description Get a specific folder by uid
* @returns IFolder
*/
@Get("/api/v1/customer/folders/:uid")
@Get("/api/v1/customer/folders/:uid", [authHandler, officeFolderHandler])
protected async getOneByUid(req: Request, response: Response) {
try {
const uid = req.params["uid"];

View File

@ -7,6 +7,7 @@ import { Customer } from "le-coffre-resources/dist/Notary";
import { validateOrReject } from "class-validator";
import authHandler from "@App/middlewares/AuthHandler";
import ruleHandler from "@App/middlewares/RulesHandler";
import { Prisma } from "@prisma/client";
@Controller()
@Service()
@ -22,11 +23,17 @@ export default class CustomersController extends ApiController {
protected async get(req: Request, response: Response) {
try {
//get query
let query;
let query: Prisma.CustomersFindManyArgs = {};
if (req.query["q"]) {
query = JSON.parse(req.query["q"] as string);
}
const officeId: string = req.body.user.office_Id;
if(query.where?.office_folders?.some?.office_uid) delete query.where.office_folders.some.office_uid;
if(query.where?.office_folders?.some?.office?.uid) delete query.where?.office_folders?.some?.office?.uid;
const customerWhereInput: Prisma.CustomersWhereInput = { ...query.where, office_folders: { some: { office_uid: officeId } }};
query.where = customerWhereInput;
//call service to get prisma entity
const customersEntities = await this.customersService.get(query);

View File

@ -8,6 +8,7 @@ import { validateOrReject } from "class-validator";
import authHandler from "@App/middlewares/AuthHandler";
import ruleHandler from "@App/middlewares/RulesHandler";
import roleHandler from "@App/middlewares/RolesHandler";
import { Prisma } from "@prisma/client";
@Controller()
@Service()
@ -28,6 +29,12 @@ export default class CustomersController extends ApiController {
query = JSON.parse(req.query["q"] as string);
}
const officeId: string = req.body.user.office_Id;
if(query.where?.office_folders?.some?.office_uid) delete query.where.office_folders.some.office_uid;
if(query.where?.office_folders?.some?.office?.uid) delete query.where?.office_folders?.some?.office?.uid;
const customerWhereInput: Prisma.CustomersWhereInput = { ...query.where, office_folders: { some: { office_uid: officeId } }};
query.where = customerWhereInput;
//call service to get prisma entity
const customersEntities = await this.customersService.get(query);

View File

@ -42,7 +42,6 @@ import FilesControllerCustomer from "./api/customer/FilesController";
import DocumentsControllerCustomer from "./api/customer/DocumentsController";
import OfficeFoldersController from "./api/customer/OfficeFoldersController";
import OfficeFolderAnchorsController from "./api/notary/OfficeFolderAnchorsController";
import CustomersController from "./api/customer/CustomersController";
import AppointmentsController from "./api/super-admin/AppointmentsController";
import VotesController from "./api/super-admin/VotesController";
import LiveVoteController from "./api/super-admin/LiveVoteController";
@ -105,7 +104,6 @@ export default {
Container.get(DocumentsControllerCustomer);
Container.get(OfficeFoldersController);
Container.get(OfficeFolderAnchorsController);
Container.get(CustomersController);
Container.get(UserNotificationController);
Container.get(DocumentControllerId360);
Container.get(CustomerControllerId360);

View File

@ -0,0 +1,24 @@
import HttpCodes from "@Common/system/controller-pattern/HttpCodes";
import OfficeFoldersService from "@Services/customer/OfficeFoldersService/OfficeFoldersService";
import { NextFunction, Request, Response } from "express";
import Container from "typedi";
export default async function officeFolderHandler(req: Request, response: Response, next: NextFunction) {
const customerId = req.body.user.customerId;
const uid = req.path && req.path.split("/")[5];
if (uid) {
const officeFolderService = Container.get(OfficeFoldersService);
const officeFolder = await officeFolderService.getByUidWithCustomers(uid);
if (!officeFolder) {
response.status(HttpCodes.NOT_FOUND).send("Folder not found");
return;
}
if (!officeFolder.customers.find((customer) => customer.uid == customerId)) {
response.status(HttpCodes.UNAUTHORIZED).send("Not authorized with this depositor");
return;
}
}
next();
}

View File

@ -109,6 +109,20 @@ export default class OfficeFoldersRepository extends BaseRepository {
});
}
/**
* @description : Find one office folder
*/
public async findOneByUidWithCustomers(uid: string) {
return this.model.findUnique({
where: {
uid: uid,
},
include: {
customers: true,
}
});
}
/**
* @description : Find one office folder
*/

View File

@ -1,19 +0,0 @@
import { Contacts, Customers } from "@prisma/client";
import BaseService from "@Services/BaseService";
import { Service } from "typedi";
import ContactRepository from "@Repositories/ContactRepository";
@Service()
export default class DocumentsService extends BaseService {
constructor(private contactRepository: ContactRepository) {
super();
}
/**
* @description : Get a contact by email
* @throws {Error} If contact cannot be get by email
*/
public async getByEmail(email: string): Promise<(Contacts & {customers: Customers | null}) | null> {
return this.contactRepository.findOneByEmail(email);
}
}

View File

@ -1,12 +1,11 @@
import { Customers, Prisma } from "@prisma/client";
import CustomersRepository from "@Repositories/CustomersRepository";
import ContactRepository from "@Repositories/ContactRepository";
import BaseService from "@Services/BaseService";
import { Service } from "typedi";
@Service()
export default class CustomersService extends BaseService {
constructor(private customerRepository: CustomersRepository, private contactRepository: ContactRepository) {
constructor(private customerRepository: CustomersRepository) {
super();
}
@ -17,30 +16,4 @@ export default class CustomersService extends BaseService {
public async get(query: Prisma.CustomersFindManyArgs): Promise<Customers[]> {
return this.customerRepository.findMany(query);
}
/**
* @description : Get a customer by uid
* @throws {Error} If customer cannot be get by uid
*/
public async getByUid(uid: string, query?: Prisma.CustomersInclude): Promise<Customers | null> {
return this.customerRepository.findOneByUid(uid, query);
}
/**
* @description : Get a customer by contact uid
* @throws {Error} If customer cannot be get by contact uid
*/
public async getByContact(contactUid: string): Promise<Customers | null> {
return this.customerRepository.findOneByContact(contactUid);
}
/**
* @description : Get a customer by contact uid
* @throws {Error} If customer cannot be get by contact uid
*/
public async getByEmail(contactUid: string) {
return this.contactRepository.findOneByEmail(contactUid);
}
}
}

View File

@ -26,4 +26,14 @@ export default class OfficeFoldersService extends BaseService {
public async getByUid(uid: string, query?: Prisma.OfficeFoldersInclude) {
return this.officeFoldersRepository.findOneByUid(uid, query);
}
/**
* @description : Get a folder by uid
* @throws {Error} If folder cannot be get by uid
*/
public async getByUidWithCustomers(uid: string) {
return this.officeFoldersRepository.findOneByUidWithCustomers(uid);
}
}

View File

@ -23,6 +23,14 @@ export default class CustomersService extends BaseService {
* @throws {Error} If customer cannot be created
*/
public async create(customerEntity: Customer): Promise<Customers> {
const customers = await this.get({
where: {
contact: {
OR: [{ email: customerEntity.contact?.email }, { cell_phone_number: customerEntity.contact?.cell_phone_number }],
},
},
});
if(customers[0]) return customers[0];
return this.customerRepository.create(customerEntity);
}