lecoffre-back/src/common/repositories/CustomersRepository.ts
2023-11-27 17:36:46 +01:00

133 lines
3.8 KiB
TypeScript

import Database from "@Common/databases/database";
import BaseRepository from "@Repositories/BaseRepository";
import { Service } from "typedi";
import { Customers, ECivility, ECustomerStatus, Prisma } from "@prisma/client";
import { Customer } from "le-coffre-resources/dist/SuperAdmin";
type IExcludedCustomerVars = {
totpCodeExpire?: Date | null;
password?: string;
};
@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) {
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 : Find one customers
*/
public async findOne(query: Prisma.CustomersFindFirstArgs) {
query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows);
if (!query.include) return this.model.findFirst({ ...query, include: { contact: true } });
return this.model.findFirst({ ...query, include: { contact: { include: { address: true } }, ...query.include } });
}
/**
* @description : Create a customer
*/
public async create(customer: Customer): Promise<Customers> {
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, excludedVars?: IExcludedCustomerVars): Promise<Customers> {
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: {},
},
},
password: excludedVars && excludedVars.password,
},
};
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?: Prisma.CustomersInclude) {
return this.model.findUnique({
where: {
uid: uid,
},
include: query,
});
}
/**
* @description : Find unique customer
*/
public async findOneByContact(contactUid: string) {
return this.model.findUnique({
where: {
contact_uid: contactUid,
},
});
}
}