68 lines
2.4 KiB
TypeScript
68 lines
2.4 KiB
TypeScript
import EmailBuilder from "@Common/emails/EmailBuilder";
|
|
import { Customers, Documents, Prisma } from "@prisma/client";
|
|
import CustomersRepository from "@Repositories/CustomersRepository";
|
|
import BaseService from "@Services/BaseService";
|
|
import { Customer, DocumentReminder } from "le-coffre-resources/dist/Notary";
|
|
import { Service } from "typedi";
|
|
import DocumentsReminderService from "../DocumentsReminder/DocumentsReminder";
|
|
|
|
@Service()
|
|
export default class CustomersService extends BaseService {
|
|
constructor(private customerRepository: CustomersRepository, private emailBuilder: EmailBuilder, private documentsReminderService : DocumentsReminderService) {
|
|
super();
|
|
}
|
|
|
|
/**
|
|
* @description : Get all Customers
|
|
* @throws {Error} If Customers cannot be get
|
|
*/
|
|
public async get(query: Prisma.CustomersFindManyArgs): Promise<Customers[]> {
|
|
return this.customerRepository.findMany(query);
|
|
}
|
|
|
|
/**
|
|
* @description : Create a new customer
|
|
* @throws {Error} If customer cannot be created
|
|
*/
|
|
public async create(customerEntity: Customer): Promise<Customers> {
|
|
return this.customerRepository.create(customerEntity);
|
|
}
|
|
|
|
/**
|
|
* @description : Modify a customer
|
|
* @throws {Error} If customer cannot be modified
|
|
*/
|
|
public async update(uid: string, customerEntity: Customer): Promise<Customers> {
|
|
return this.customerRepository.update(uid, customerEntity);
|
|
}
|
|
|
|
/**
|
|
* @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);
|
|
}
|
|
|
|
public async sendDocumentsReminder(customer: Customer, documents: Documents[]): Promise<void> {
|
|
//Call email builder to send mail
|
|
const email = this.emailBuilder.sendReminder(customer, documents);
|
|
//Call DocumentsReminder service to create add the reminder in database
|
|
if (!email) return;
|
|
for (const document of documents) {
|
|
//Create document reminder
|
|
const documentReminder = new DocumentReminder();
|
|
documentReminder.document = document;
|
|
await this.documentsReminderService.create(documentReminder);
|
|
}
|
|
}
|
|
}
|