product-bundle-item.service.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { Injectable } from '@nestjs/common';
  2. import { DeletionResponse, DeletionResult } from '@vendure/common/lib/generated-types';
  3. import { ID } from '@vendure/common/lib/shared-types';
  4. import { ListQueryBuilder, patchEntity, RequestContext, TransactionalConnection } from '@vendure/core';
  5. import { ProductBundleItem } from '../entities/product-bundle-item.entity';
  6. import { ProductBundle } from '../entities/product-bundle.entity';
  7. import { CreateProductBundleItemInput, UpdateProductBundleItemInput } from '../types';
  8. @Injectable()
  9. export class ProductBundleItemService {
  10. constructor(
  11. private connection: TransactionalConnection,
  12. private listQueryBuilder: ListQueryBuilder,
  13. ) {}
  14. async createProductBundleItem(
  15. ctx: RequestContext,
  16. input: CreateProductBundleItemInput,
  17. ): Promise<ProductBundleItem> {
  18. const bundle = await this.connection.getEntityOrThrow(ctx, ProductBundle, input.bundleId);
  19. const newEntity = await this.connection.getRepository(ctx, ProductBundleItem).save(
  20. new ProductBundleItem({
  21. ...input,
  22. bundle,
  23. }),
  24. );
  25. return newEntity;
  26. }
  27. async updateProductBundleItem(
  28. ctx: RequestContext,
  29. input: UpdateProductBundleItemInput,
  30. ): Promise<ProductBundleItem> {
  31. const entity = await this.connection.getEntityOrThrow(ctx, ProductBundleItem, input.id);
  32. const updatedEntity = patchEntity(entity, input);
  33. await this.connection.getRepository(ctx, ProductBundleItem).save(updatedEntity, { reload: false });
  34. return updatedEntity;
  35. }
  36. async delete(ctx: RequestContext, id: ID): Promise<DeletionResponse> {
  37. const entity = await this.connection.getEntityOrThrow(ctx, ProductBundleItem, id);
  38. try {
  39. await this.connection.getRepository(ctx, ProductBundleItem).remove(entity);
  40. return {
  41. result: DeletionResult.DELETED,
  42. };
  43. } catch (e: any) {
  44. return {
  45. result: DeletionResult.NOT_DELETED,
  46. message: e.toString(),
  47. };
  48. }
  49. }
  50. }