121 lines
2.7 KiB
TypeScript
121 lines
2.7 KiB
TypeScript
import Database from "@Common/databases/database";
|
|
import BaseRepository from "@Repositories/BaseRepository";
|
|
import { Service } from "typedi";
|
|
import { Files, Prisma } from "@prisma/client";
|
|
import { File } from "le-coffre-resources/dist/SuperAdmin";
|
|
|
|
@Service()
|
|
export default class FilesRepository extends BaseRepository {
|
|
constructor(private database: Database) {
|
|
super();
|
|
}
|
|
protected get model() {
|
|
return this.database.getClient().files;
|
|
}
|
|
protected get instanceDb() {
|
|
return this.database.getClient();
|
|
}
|
|
|
|
/**
|
|
* @description : Find many files
|
|
*/
|
|
public async findMany(query: Prisma.FilesFindManyArgs) {
|
|
query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows);
|
|
return this.model.findMany(query);
|
|
}
|
|
|
|
/**
|
|
* @description : Create a file linked to a document
|
|
*/
|
|
public async create(file: File, key: string): Promise<Files> {
|
|
const createArgs: Prisma.FilesCreateArgs = {
|
|
data: {
|
|
document: {
|
|
connect: {
|
|
uid: file.document!.uid,
|
|
},
|
|
},
|
|
file_name: file.file_name,
|
|
file_path: file.file_path,
|
|
mimetype: file.mimetype,
|
|
hash: file.hash,
|
|
size: file.size,
|
|
key: key,
|
|
},
|
|
};
|
|
return this.model.create({ ...createArgs, include: { document: true } });
|
|
}
|
|
|
|
/**
|
|
* @description : Update data of a file
|
|
*/
|
|
public async update(uid: string, file: File, key: string): Promise<Files> {
|
|
const updateArgs: Prisma.FilesUpdateArgs = {
|
|
where: {
|
|
uid: uid,
|
|
},
|
|
data: {
|
|
file_name: file.file_name,
|
|
file_path: file.file_path,
|
|
mimetype: file.mimetype,
|
|
hash: file.hash,
|
|
size: file.size,
|
|
key: key,
|
|
},
|
|
};
|
|
return this.model.update({ ...updateArgs, include: { document: true } });
|
|
}
|
|
|
|
/**
|
|
* @description : Delete a file key and archive
|
|
*/
|
|
public async deleteKeyAndArchive(uid: string): Promise<Files> {
|
|
const updateArgs: Prisma.FilesUpdateArgs = {
|
|
where: {
|
|
uid: uid,
|
|
},
|
|
data: {
|
|
key: null,
|
|
archived_at: new Date(Date.now()),
|
|
},
|
|
};
|
|
return this.model.update({ ...updateArgs, include: { document: true } });
|
|
}
|
|
|
|
/**
|
|
* @description : Find unique file
|
|
*/
|
|
public async findOneByUid(uid: string, query?: Prisma.FilesInclude) {
|
|
return this.model.findUnique({
|
|
where: {
|
|
uid: uid,
|
|
},
|
|
include: query,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description : Find unique file with office
|
|
*/
|
|
public async findOneByUidWithOffice(uid: string) {
|
|
return this.model.findUnique({
|
|
where: {
|
|
uid: uid,
|
|
},
|
|
include: { document: { include: { folder: { include: { office: true } } } } },
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description : Find unique file with document
|
|
*/
|
|
public async findOneByUidWithDocument(uid: string) {
|
|
return this.model.findUnique({
|
|
where: {
|
|
uid: uid,
|
|
},
|
|
include: { document: true },
|
|
});
|
|
}
|
|
}
|