2022-09-06 20:07:02 +00:00
|
|
|
import PostgresDatabase from '../Infrastructure/PostgresDatabase';
|
|
|
|
import File from 'Model/Models/File';
|
2023-10-02 09:32:31 +00:00
|
|
|
import DatabaseService from './DatabaseService';
|
|
|
|
import { OnDelete, OnFind, OnUpdate } from '../Types/Database/Hooks';
|
2022-09-06 20:12:36 +00:00
|
|
|
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
|
|
|
|
2022-09-07 18:40:14 +00:00
|
|
|
protected override async onBeforeUpdate(
|
|
|
|
updateBy: UpdateBy<File>
|
|
|
|
): Promise<OnUpdate<File>> {
|
2022-09-06 20:12:36 +00:00
|
|
|
if (!updateBy.props.isRoot) {
|
2022-09-07 18:40:14 +00:00
|
|
|
throw new NotAuthorizedException(
|
|
|
|
'Not authorized to update a file.'
|
|
|
|
);
|
2022-09-06 20:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return { updateBy, carryForward: null };
|
|
|
|
}
|
|
|
|
|
2022-09-07 18:40:14 +00:00
|
|
|
protected override async onBeforeDelete(
|
|
|
|
deleteBy: DeleteBy<File>
|
|
|
|
): Promise<OnDelete<File>> {
|
2022-09-06 20:12:36 +00:00
|
|
|
if (!deleteBy.props.isRoot) {
|
2022-09-07 18:40:14 +00:00
|
|
|
throw new NotAuthorizedException(
|
|
|
|
'Not authorized to delete a file.'
|
|
|
|
);
|
2022-09-06 20:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return { deleteBy, carryForward: null };
|
|
|
|
}
|
|
|
|
|
2022-09-07 18:40:14 +00:00
|
|
|
protected override async onBeforeFind(
|
|
|
|
findBy: FindBy<File>
|
|
|
|
): Promise<OnFind<File>> {
|
2022-09-06 20:12:36 +00:00
|
|
|
if (!findBy.props.isRoot) {
|
|
|
|
findBy.query = {
|
|
|
|
...findBy.query,
|
2022-09-07 18:40:14 +00:00
|
|
|
isPublic: true,
|
|
|
|
};
|
2022-09-06 20:12:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 18:40:14 +00:00
|
|
|
return { findBy, carryForward: null };
|
2022-09-06 20:12:36 +00:00
|
|
|
}
|
2022-09-06 20:07:02 +00:00
|
|
|
}
|
|
|
|
export default new Service();
|