added base service

This commit is contained in:
hannathkadher
2024-12-17 19:21:00 +04:00
parent d1050babd1
commit e35cefb03b
4 changed files with 94 additions and 116 deletions
@@ -0,0 +1,69 @@
import { HttpException, HttpStatus } from '@nestjs/common';
import { QueryRunner } from 'typeorm';
export abstract class BaseProductItemService {
protected async validateTags(
incomingTags: string[],
queryRunner: QueryRunner,
spaceUuid: string,
): Promise<void> {
const duplicateTags = incomingTags.filter(
(tag, index) => incomingTags.indexOf(tag) !== index,
);
if (duplicateTags.length > 0) {
throw new HttpException(
`Duplicate tags found in the request: ${[...new Set(duplicateTags)].join(', ')}`,
HttpStatus.BAD_REQUEST,
);
}
const existingTagsQuery = `
SELECT tag FROM (
SELECT spi.tag
FROM "space-product-item" spi
INNER JOIN "space-product" spm ON spi.space_product_uuid = spm.uuid
WHERE spm.space_uuid = $1
UNION
SELECT spi.tag
FROM "subspace-product-item" spi
INNER JOIN "subspace-product" spm ON spi.subspace_product_uuid = spm.uuid
INNER JOIN "subspace" sm ON spm.subspace_uuid = sm.uuid
WHERE sm.space_uuid = $1
) AS combined_tags;
`;
const existingTags = await queryRunner.manager.query(existingTagsQuery, [
spaceUuid,
]);
const existingTagSet = new Set(
existingTags.map((row: { tag: string }) => row.tag),
);
const conflictingTags = incomingTags.filter((tag) =>
existingTagSet.has(tag),
);
if (conflictingTags.length > 0) {
throw new HttpException(
`Tags already exist in the model: ${conflictingTags.join(', ')}`,
HttpStatus.CONFLICT,
);
}
}
protected async saveProductItems<T>(
productItems: T[],
targetRepository: any,
queryRunner: QueryRunner,
): Promise<void> {
try {
await queryRunner.manager.save(targetRepository, productItems);
} catch (error) {
throw new HttpException(
error.message || 'An error occurred while creating product items.',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
+1
View File
@@ -0,0 +1 @@
export * from './base-product-item.service';