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-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: {
|
2022-08-19 19:47:56 +00:00
|
|
|
name: QueryHelper.findWithSameText(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();
|