145 lines
3.1 KiB
TypeScript
145 lines
3.1 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: Prisma.OfficesFindManyArgs) {
|
|
if(query){
|
|
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> {
|
|
const createArgs: Prisma.OfficesCreateArgs = {
|
|
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,
|
|
},
|
|
};
|
|
return this.model.create(createArgs);
|
|
}
|
|
|
|
/**
|
|
* @description : Update data from an office
|
|
*/
|
|
public async update(uid: string, office: OfficeRessource): Promise<Offices> {
|
|
const updateArgs: Prisma.OfficesUpdateArgs = {
|
|
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],
|
|
rib_url: office.rib_url,
|
|
rib_name: office.rib_name,
|
|
},
|
|
};
|
|
return this.model.update(updateArgs);
|
|
}
|
|
|
|
/**
|
|
* @description : Update check date of an office
|
|
*/
|
|
public async updateCheckedAt(uid: string) {
|
|
return this.model.update({
|
|
where: {
|
|
uid: uid,
|
|
},
|
|
data: {
|
|
checked_at: new Date(),
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description : Delete an office
|
|
*/
|
|
public async delete(uid: string): Promise<Offices> {
|
|
return this.model.delete({
|
|
where: {
|
|
uid: uid,
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description : Find one office
|
|
*/
|
|
public async findOneByUid(uid: string, query?: Prisma.OfficesInclude) {
|
|
return this.model.findUnique({
|
|
where: {
|
|
uid: uid,
|
|
},
|
|
include: query,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description : Find one office
|
|
*/
|
|
public async findOneByProvider(providerName: string, id: string, query?: Prisma.OfficesInclude) {
|
|
return this.model.findUnique({
|
|
where: { [providerName]: id },
|
|
include: query,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description : Find offices which need to be checked with idNot API
|
|
*/
|
|
public async findManyToCheck() {
|
|
return this.model.findMany({
|
|
where: {
|
|
OR: [
|
|
{
|
|
checked_at: null,
|
|
},
|
|
{
|
|
checked_at: { lt: new Date(Date.now() - 3 * 60 * 1000) }, // less than 24h ago
|
|
},
|
|
],
|
|
},
|
|
include: {
|
|
address: true,
|
|
},
|
|
});
|
|
}
|
|
}
|