wishlist.service.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { Injectable } from '@nestjs/common';
  2. import {
  3. Customer,
  4. ForbiddenError,
  5. ID,
  6. InternalServerError,
  7. ProductVariantService,
  8. RequestContext,
  9. TransactionalConnection,
  10. UserInputError,
  11. } from '@vendure/core';
  12. import { WishlistItem } from '../entities/wishlist-item.entity';
  13. @Injectable()
  14. export class WishlistService {
  15. constructor(
  16. private connection: TransactionalConnection,
  17. private productVariantService: ProductVariantService,
  18. ) {}
  19. async getWishlistItems(ctx: RequestContext): Promise<WishlistItem[]> {
  20. try {
  21. const customer = await this.getCustomerWithWishlistItems(ctx);
  22. return customer.customFields.wishlistItems;
  23. } catch (err: any) {
  24. return [];
  25. }
  26. }
  27. /**
  28. * Adds a new item to the active Customer's wishlist.
  29. */
  30. async addItem(ctx: RequestContext, variantId: ID): Promise<WishlistItem[]> {
  31. const customer = await this.getCustomerWithWishlistItems(ctx);
  32. const variant = await this.productVariantService.findOne(ctx, variantId);
  33. if (!variant) {
  34. throw new UserInputError(`No ProductVariant with the id ${variantId} could be found`);
  35. }
  36. const existingItem = customer.customFields.wishlistItems.find(i => i.productVariantId === variantId);
  37. if (existingItem) {
  38. // Item already exists in wishlist, do not
  39. // add it again
  40. return customer.customFields.wishlistItems;
  41. }
  42. const wishlistItem = await this.connection
  43. .getRepository(ctx, WishlistItem)
  44. .save(new WishlistItem({ productVariantId: variantId }));
  45. customer.customFields.wishlistItems.push(wishlistItem);
  46. await this.connection.getRepository(ctx, Customer).save(customer, { reload: false });
  47. return this.getWishlistItems(ctx);
  48. }
  49. /**
  50. * Removes an item from the active Customer's wishlist.
  51. */
  52. async removeItem(ctx: RequestContext, itemId: ID): Promise<WishlistItem[]> {
  53. const customer = await this.getCustomerWithWishlistItems(ctx);
  54. const itemToRemove = customer.customFields.wishlistItems.find(i => i.id === itemId);
  55. if (itemToRemove) {
  56. await this.connection.getRepository(ctx, WishlistItem).remove(itemToRemove);
  57. customer.customFields.wishlistItems = customer.customFields.wishlistItems.filter(
  58. i => i.id !== itemId,
  59. );
  60. }
  61. await this.connection.getRepository(ctx, Customer).save(customer);
  62. return this.getWishlistItems(ctx);
  63. }
  64. /**
  65. * Gets the active Customer from the context and loads the wishlist items.
  66. */
  67. private async getCustomerWithWishlistItems(ctx: RequestContext): Promise<Customer> {
  68. if (!ctx.activeUserId) {
  69. throw new ForbiddenError();
  70. }
  71. const customer = await this.connection.getRepository(ctx, Customer).findOne({
  72. where: { user: { id: ctx.activeUserId } },
  73. relations: {
  74. customFields: {
  75. wishlistItems: {
  76. productVariant: true,
  77. },
  78. },
  79. },
  80. });
  81. if (!customer) {
  82. throw new InternalServerError(`Customer was not found`);
  83. }
  84. return customer;
  85. }
  86. }