99 lines
2.6 KiB
TypeScript
99 lines
2.6 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
Param,
|
|
ParseUUIDPipe,
|
|
Patch,
|
|
Post,
|
|
Put,
|
|
Query,
|
|
} from '@nestjs/common';
|
|
import { SchemaPipe } from '../common/validation.pipe';
|
|
import {
|
|
CurrentPrincipal,
|
|
RequirePermission,
|
|
} from '../identity/access.decorator';
|
|
import type { Principal } from '../identity/identity.types';
|
|
import { ProductStore } from './product.store';
|
|
import { VariantStore } from './variant.store';
|
|
import {
|
|
catalogQuery,
|
|
productSchema,
|
|
productStatusSchema,
|
|
variantSchema,
|
|
type CatalogQuery,
|
|
type ProductInput,
|
|
type VariantInput,
|
|
} from './catalog.schemas';
|
|
|
|
@Controller('products')
|
|
export class ProductsController {
|
|
constructor(
|
|
private readonly products: ProductStore,
|
|
private readonly variants: VariantStore,
|
|
) {}
|
|
@Get()
|
|
@RequirePermission('catalog.read')
|
|
list(
|
|
@CurrentPrincipal() actor: Principal,
|
|
@Query(new SchemaPipe(catalogQuery)) query: CatalogQuery,
|
|
) {
|
|
return this.products.list(actor, query);
|
|
}
|
|
@Get(':id')
|
|
@RequirePermission('catalog.read')
|
|
get(
|
|
@CurrentPrincipal() actor: Principal,
|
|
@Param('id', ParseUUIDPipe) id: string,
|
|
) {
|
|
return this.products.get(actor, id);
|
|
}
|
|
@Post()
|
|
@RequirePermission('catalog.manage')
|
|
create(
|
|
@CurrentPrincipal() actor: Principal,
|
|
@Body(new SchemaPipe(productSchema)) input: ProductInput,
|
|
) {
|
|
return this.products.save(actor, input);
|
|
}
|
|
@Put(':id')
|
|
@RequirePermission('catalog.manage')
|
|
update(
|
|
@CurrentPrincipal() actor: Principal,
|
|
@Param('id', ParseUUIDPipe) id: string,
|
|
@Body(new SchemaPipe(productSchema)) input: ProductInput,
|
|
) {
|
|
return this.products.save(actor, input, id);
|
|
}
|
|
@Patch(':id/status')
|
|
@RequirePermission('catalog.publish')
|
|
status(
|
|
@CurrentPrincipal() actor: Principal,
|
|
@Param('id', ParseUUIDPipe) id: string,
|
|
@Body(new SchemaPipe(productStatusSchema))
|
|
input: { status: 'DRAFT' | 'PUBLISHED' | 'ARCHIVED' },
|
|
) {
|
|
return this.products.setStatus(actor, id, input.status);
|
|
}
|
|
@Post(':id/variants')
|
|
@RequirePermission('catalog.manage')
|
|
createVariant(
|
|
@CurrentPrincipal() actor: Principal,
|
|
@Param('id', ParseUUIDPipe) id: string,
|
|
@Body(new SchemaPipe(variantSchema)) input: VariantInput,
|
|
) {
|
|
return this.variants.save(actor, id, input);
|
|
}
|
|
@Put(':id/variants/:variantId')
|
|
@RequirePermission('catalog.manage')
|
|
updateVariant(
|
|
@CurrentPrincipal() actor: Principal,
|
|
@Param('id', ParseUUIDPipe) id: string,
|
|
@Param('variantId', ParseUUIDPipe) variantId: string,
|
|
@Body(new SchemaPipe(variantSchema)) input: VariantInput,
|
|
) {
|
|
return this.variants.save(actor, id, input, variantId);
|
|
}
|
|
}
|