lecoffre-back/src/common/repositories/OfficeFolderAnchorsRepository.ts
2023-10-03 11:21:25 +02:00

89 lines
2.5 KiB
TypeScript

import Database from "@Common/databases/database";
import BaseRepository from "@Repositories/BaseRepository";
import { Service } from "typedi";
import { OfficeFolderAnchors, Prisma } from "@prisma/client";
import { OfficeFolderAnchor } from "le-coffre-resources/dist/SuperAdmin";
@Service()
export default class OfficeFolderAnchorsRepository extends BaseRepository {
constructor(private database: Database) {
super();
}
protected get model() {
return this.database.getClient().officeFolderAnchors;
}
protected get instanceDb() {
return this.database.getClient();
}
/**
* @description : Create new office folder anchor
*/
public async create(officeFolderAnchor: OfficeFolderAnchor): Promise<OfficeFolderAnchors> {
const createArgs: Prisma.OfficeFolderAnchorsCreateArgs = {
data: {
hash_sources: officeFolderAnchor.hash_sources,
root_hash: officeFolderAnchor.root_hash!,
blockchain: officeFolderAnchor.blockchain as OfficeFolderAnchors["blockchain"],
status: officeFolderAnchor.status as OfficeFolderAnchors["status"],
anchor_nb_try: officeFolderAnchor.anchor_nb_try,
tx_id: officeFolderAnchor.tx_id,
tx_link: officeFolderAnchor.tx_link,
tx_hash: officeFolderAnchor.tx_hash,
anchored_at: officeFolderAnchor.anchored_at,
},
};
return this.model.create({ ...createArgs });
}
/**
* @description : Update data of an office folder anchor
*/
public async update(officeFolderAnchorUid: string, officeFolderAnchor: OfficeFolderAnchor): Promise<OfficeFolderAnchors> {
const updateArgs: Prisma.OfficeFolderAnchorsUpdateArgs = {
where: {
uid: officeFolderAnchorUid,
},
data: {
blockchain: officeFolderAnchor.blockchain as OfficeFolderAnchors["blockchain"],
status: officeFolderAnchor.status as OfficeFolderAnchors["status"],
anchor_nb_try: officeFolderAnchor.anchor_nb_try,
tx_id: officeFolderAnchor.tx_id,
tx_link: officeFolderAnchor.tx_link,
tx_hash: officeFolderAnchor.tx_hash,
anchored_at: officeFolderAnchor.anchored_at,
},
};
return this.model.update({
...updateArgs,
});
}
/**
* @description : Find one office folder
*/
public async findOneByUid(uid: string, query?: Prisma.OfficeFolderAnchorsInclude) {
return this.model.findUnique({
where: {
uid: uid,
},
include: query,
});
}
/**
* @description : Find many office folders
*/
public async findMany(query: Prisma.OfficeFolderAnchorsFindManyArgs) {
query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows);
return this.model.findMany(query);
}
}