mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-24 16:57:54 +00:00
added base service
This commit is contained in:
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from './base-product-item.service';
|
||||
Reference in New Issue
Block a user