import Database from "@Common/databases/database"; import BaseRepository from "@Repositories/BaseRepository"; import { Service } from "typedi"; import { Contacts, Customers, ECivility, ECustomerStatus, Prisma } from "@prisma/client"; import { Customer } from "le-coffre-resources/dist/SuperAdmin"; @Service() export default class CustomersRepository extends BaseRepository { constructor(private database: Database) { super(); } protected get model() { return this.database.getClient().customers; } protected get instanceDb() { return this.database.getClient(); } /** * @description : Find many customers */ public async findMany(query: Prisma.CustomersFindManyArgs): Promise< (Customers & { contact: Contacts; })[] > { query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows); if (!query.include) return this.model.findMany({ ...query, include: { contact: true } }); return this.model.findMany({ ...query, include: { contact: { include: { address: true } } } }); } /** * @description : Create a customer */ public async create(customer: Customer): Promise { const createArgs: Prisma.CustomersCreateArgs = { 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: {}, }, }, }, }; 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, include: { contact: true } }); } /** * @description : Update data from a customer */ public async update(uid: string, customer: Customer): Promise { const updateArgs: Prisma.CustomersUpdateArgs = { where: { uid: uid, }, data: { status: ECustomerStatus[customer.status as keyof typeof ECustomerStatus], contact: { update: { 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: {}, }, }, }, }; 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, include: { contact: true } }); } /** * @description : Find unique customer */ public async findOneByUid(uid: string, query?: any): Promise { const findOneArgs: Prisma.CustomersFindUniqueArgs = { where: { uid: uid, }, }; if (query) { findOneArgs.include = query; } const customerEntity = await this.model.findUnique(findOneArgs); return customerEntity; } }