oneuptime/CommonServer/Services/LabelService.ts

47 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-07-19 09:07:09 +00:00
import PostgresDatabase from '../Infrastructure/PostgresDatabase';
2022-08-03 18:57:41 +00:00
import Model from 'Model/Models/Label';
2022-08-16 21:43:06 +00:00
import DatabaseService, { OnCreate } from './DatabaseService';
2022-07-23 20:10:21 +00:00
import CreateBy from '../Types/Database/CreateBy';
import QueryHelper from '../Types/Database/QueryHelper';
import BadDataException from 'Common/Types/Exception/BadDataException';
2022-07-24 08:31:50 +00:00
import ObjectID from 'Common/Types/ObjectID';
2022-07-19 09:07:09 +00:00
export class Service extends DatabaseService<Model> {
public constructor(postgresDatabase?: PostgresDatabase) {
super(Model, postgresDatabase);
}
2022-07-23 20:10:21 +00:00
2022-07-23 21:05:57 +00:00
protected override async onBeforeCreate(
createBy: CreateBy<Model>
2022-08-16 21:43:06 +00:00
): Promise<OnCreate<Model>> {
2022-07-23 20:10:21 +00:00
let existingProjectWithSameNameCount: number = 0;
2022-07-23 21:05:57 +00:00
existingProjectWithSameNameCount = (
await this.countBy({
query: {
_id:
createBy.props.userGlobalAccessPermission?.projectIds.map(
2022-07-24 08:31:50 +00:00
(item: ObjectID) => {
2022-07-23 21:05:57 +00:00
return item.toString();
}
) || [],
name: QueryHelper.findWithSameName(createBy.data.name!),
2022-08-10 18:14:01 +00:00
projectId: createBy.props.tenantId!,
2022-07-23 21:05:57 +00:00
},
props: {
isRoot: true,
},
})
).toNumber();
2022-07-23 20:10:21 +00:00
if (existingProjectWithSameNameCount > 0) {
throw new BadDataException(
'Label with the same name already exists in this project.'
);
}
2022-08-16 21:43:06 +00:00
return Promise.resolve({ createBy, carryForward: null });
2022-07-23 20:10:21 +00:00
}
2022-07-19 09:07:09 +00:00
}
export default new Service();