51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import { catalogWhere } from './catalog-query';
|
|
import { Injectable } from '@nestjs/common';
|
|
import { DatabaseService } from '../database/database.service';
|
|
import { AppError } from '../common/errors/app-error';
|
|
import type { CatalogQuery } from './catalog.schemas';
|
|
|
|
const storefrontView = {
|
|
id: true,
|
|
name: true,
|
|
slug: true,
|
|
description: true,
|
|
variants: {
|
|
where: { active: true },
|
|
orderBy: { id: 'asc' as const },
|
|
select: {
|
|
id: true,
|
|
sku: true,
|
|
name: true,
|
|
price: true,
|
|
currency: true,
|
|
attributes: true,
|
|
},
|
|
},
|
|
groups: {
|
|
select: {
|
|
group: { select: { id: true, name: true, slug: true, kind: true } },
|
|
},
|
|
},
|
|
} as const;
|
|
@Injectable()
|
|
export class StorefrontStore {
|
|
constructor(private readonly db: DatabaseService) {}
|
|
list(organizationId: string, query: CatalogQuery) {
|
|
return this.db.product.findMany({
|
|
where: catalogWhere(organizationId, query, true),
|
|
select: { id: true, name: true, slug: true },
|
|
orderBy: [{ createdAt: 'desc' }, { id: 'desc' }],
|
|
take: query.limit,
|
|
skip: query.offset,
|
|
});
|
|
}
|
|
async get(organizationId: string, id: string) {
|
|
const product = await this.db.product.findFirst({
|
|
where: { id, organizationId, status: 'PUBLISHED' },
|
|
select: storefrontView,
|
|
});
|
|
if (!product) throw new AppError('PRODUCT_NOT_FOUND');
|
|
return product;
|
|
}
|
|
}
|