88 lines
2.0 KiB
TypeScript
88 lines
2.0 KiB
TypeScript
import Database from "@Common/databases/database";
|
|
import BaseRepository from "@Repositories/BaseRepository";
|
|
import { Service } from "typedi";
|
|
import { EOfficeStatus, Offices, Prisma } from "@prisma/client";
|
|
import { Office as OfficeRessource } from "le-coffre-resources/dist/SuperAdmin";
|
|
|
|
@Service()
|
|
export default class OfficesRepository extends BaseRepository {
|
|
constructor(private database: Database) {
|
|
super();
|
|
}
|
|
protected get model() {
|
|
return this.database.getClient().offices;
|
|
}
|
|
protected get instanceDb() {
|
|
return this.database.getClient();
|
|
}
|
|
|
|
/**
|
|
* @description : Find many users
|
|
*/
|
|
public async findMany(query: any): Promise<Offices[]> {
|
|
query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows);
|
|
|
|
return this.model.findMany(query);
|
|
}
|
|
|
|
/**
|
|
* @description : Create an office
|
|
*/
|
|
public async create(office: OfficeRessource): Promise<Offices> {
|
|
return this.model.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,
|
|
},
|
|
},
|
|
office_status: EOfficeStatus.DESACTIVATED,
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description : Update data from an office
|
|
*/
|
|
public async update(uid: string, office: OfficeRessource): Promise<Offices> {
|
|
return this.model.update({
|
|
where: {
|
|
uid: uid,
|
|
},
|
|
data: {
|
|
name: office.name,
|
|
address: {
|
|
create: {
|
|
address: office.address!.address,
|
|
zip_code: office.address!.zip_code,
|
|
city: office.address!.city,
|
|
},
|
|
},
|
|
office_status: EOfficeStatus[office.office_status as keyof typeof EOfficeStatus],
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description : Find one office
|
|
*/
|
|
public async findOneByUid(uid: string, query?: any): Promise<Offices | null> {
|
|
const findOneArgs: Prisma.OfficesFindUniqueArgs = {
|
|
where: {
|
|
uid: uid,
|
|
},
|
|
};
|
|
if (query) {
|
|
findOneArgs.include = query;
|
|
}
|
|
const officeEntity = await this.model.findUnique(findOneArgs);
|
|
|
|
return officeEntity;
|
|
}
|
|
}
|