oneuptime/CommonServer/Services/FileService.ts

44 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-09-06 20:07:02 +00:00
import PostgresDatabase from '../Infrastructure/PostgresDatabase';
import File from 'Model/Models/File';
2022-09-06 20:12:36 +00:00
import DatabaseService, { OnDelete, OnFind, OnUpdate } from './DatabaseService';
import FindBy from '../Types/Database/FindBy';
import DeleteBy from '../Types/Database/DeleteBy';
import NotAuthorizedException from 'Common/Types/Exception/NotAuthorizedException';
import UpdateBy from '../Types/Database/UpdateBy';
2022-09-06 20:07:02 +00:00
export class Service extends DatabaseService<File> {
public constructor(postgresDatabase?: PostgresDatabase) {
super(File, postgresDatabase);
}
2022-09-06 20:12:36 +00:00
protected override async onBeforeUpdate(updateBy: UpdateBy<File>): Promise<OnUpdate<File>> {
if (!updateBy.props.isRoot) {
throw new NotAuthorizedException("Not authorized to update a file.");
}
return { updateBy, carryForward: null };
}
protected override async onBeforeDelete(deleteBy: DeleteBy<File>): Promise<OnDelete<File>> {
if (!deleteBy.props.isRoot) {
throw new NotAuthorizedException("Not authorized to delete a file.");
}
return { deleteBy, carryForward: null };
}
protected override async onBeforeFind(findBy: FindBy<File>): Promise<OnFind<File>> {
if (!findBy.props.isRoot) {
findBy.query = {
...findBy.query,
isPublic: true,
}
}
return {findBy, carryForward: null}
}
2022-09-06 20:07:02 +00:00
}
export default new Service();