100 lines
3.1 KiB
TypeScript
100 lines
3.1 KiB
TypeScript
import { catalogWhere } from './catalog-query';
|
|
import { Injectable } from '@nestjs/common';
|
|
import { DatabaseService } from '../database/database.service';
|
|
import { AccessStore } from '../identity/access.store';
|
|
import { recordAudit } from '../identity/audit';
|
|
import type { Principal } from '../identity/identity.types';
|
|
import { AppError } from '../common/errors/app-error';
|
|
import {
|
|
editableProduct,
|
|
scopedProduct,
|
|
validateGroups,
|
|
} from './catalog.helpers';
|
|
import type { CatalogQuery, ProductInput } from './catalog.schemas';
|
|
import { imageOrder, imageSummary } from './product-image.store';
|
|
|
|
@Injectable()
|
|
export class ProductStore {
|
|
constructor(
|
|
private readonly db: DatabaseService,
|
|
private readonly access: AccessStore,
|
|
) {}
|
|
list(actor: Principal, query: CatalogQuery) {
|
|
return this.db.product.findMany({
|
|
where: catalogWhere(actor.organizationId, query),
|
|
select: { id: true, name: true, slug: true, status: true },
|
|
orderBy: [{ createdAt: 'desc' }, { id: 'desc' }],
|
|
take: query.limit,
|
|
skip: query.offset,
|
|
});
|
|
}
|
|
async get(actor: Principal, id: string) {
|
|
await scopedProduct(this.db, actor.organizationId, id);
|
|
return this.db.product.findUnique({
|
|
where: { id },
|
|
include: {
|
|
variants: { orderBy: { id: 'asc' } },
|
|
groups: { include: { group: true } },
|
|
images: { orderBy: imageOrder, select: imageSummary },
|
|
},
|
|
});
|
|
}
|
|
save(actor: Principal, input: ProductInput, id?: string) {
|
|
return this.access.mutate(actor, 'catalog.manage', async (tx) => {
|
|
if (id) await editableProduct(tx, actor.organizationId, id);
|
|
await validateGroups(tx, actor.organizationId, input.groupIds);
|
|
const { groupIds, ...data } = input;
|
|
const product = id
|
|
? await tx.product.update({ where: { id }, data })
|
|
: await tx.product.create({
|
|
data: { ...data, organizationId: actor.organizationId },
|
|
});
|
|
await tx.productGroup.deleteMany({ where: { productId: product.id } });
|
|
await tx.productGroup.createMany({
|
|
data: groupIds.map((groupId) => ({
|
|
productId: product.id,
|
|
groupId,
|
|
organizationId: actor.organizationId,
|
|
})),
|
|
});
|
|
await recordAudit(
|
|
tx,
|
|
actor.organizationId,
|
|
actor.userId,
|
|
id ? 'product.updated' : 'product.created',
|
|
product.id,
|
|
);
|
|
return product;
|
|
});
|
|
}
|
|
setStatus(
|
|
actor: Principal,
|
|
id: string,
|
|
status: 'DRAFT' | 'PUBLISHED' | 'ARCHIVED',
|
|
) {
|
|
return this.access.mutate(actor, 'catalog.publish', async (tx) => {
|
|
await editableProduct(tx, actor.organizationId, id);
|
|
if (
|
|
status === 'PUBLISHED' &&
|
|
!(await tx.productVariant.count({
|
|
where: { productId: id, active: true },
|
|
}))
|
|
) {
|
|
throw new AppError('PRODUCT_NOT_PUBLISHABLE');
|
|
}
|
|
const product = await tx.product.update({
|
|
where: { id },
|
|
data: { status },
|
|
});
|
|
await recordAudit(
|
|
tx,
|
|
actor.organizationId,
|
|
actor.userId,
|
|
`product.${status.toLowerCase()}`,
|
|
id,
|
|
);
|
|
return product;
|
|
});
|
|
}
|
|
}
|