Files
oneuptime/CommonServer/Services/FileService.ts
Simon Larsen 7a14d93d4c fix lint.
2022-09-07 19:40:14 +01:00

52 lines
1.5 KiB
TypeScript

import PostgresDatabase from '../Infrastructure/PostgresDatabase';
import File from 'Model/Models/File';
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';
export class Service extends DatabaseService<File> {
public constructor(postgresDatabase?: PostgresDatabase) {
super(File, postgresDatabase);
}
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 };
}
}
export default new Service();