customer.resolver.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
  2. import {
  3. CreateCustomerResult,
  4. DeletionResponse,
  5. MutationAddNoteToCustomerArgs,
  6. MutationCreateCustomerAddressArgs,
  7. MutationCreateCustomerArgs,
  8. MutationDeleteCustomerAddressArgs,
  9. MutationDeleteCustomerArgs,
  10. MutationDeleteCustomerNoteArgs,
  11. MutationUpdateCustomerAddressArgs,
  12. MutationUpdateCustomerArgs,
  13. MutationUpdateCustomerNoteArgs,
  14. Permission,
  15. QueryCustomerArgs,
  16. QueryCustomersArgs,
  17. Success,
  18. UpdateCustomerResult,
  19. } from '@vendure/common/lib/generated-types';
  20. import { PaginatedList } from '@vendure/common/lib/shared-types';
  21. import { ErrorResultUnion } from '../../../common/error/error-result';
  22. import { Address } from '../../../entity/address/address.entity';
  23. import { Customer } from '../../../entity/customer/customer.entity';
  24. import { CustomerService } from '../../../service/services/customer.service';
  25. import { OrderService } from '../../../service/services/order.service';
  26. import { RequestContext } from '../../common/request-context';
  27. import { Allow } from '../../decorators/allow.decorator';
  28. import { Ctx } from '../../decorators/request-context.decorator';
  29. import { Transaction } from '../../decorators/transaction.decorator';
  30. @Resolver()
  31. export class CustomerResolver {
  32. constructor(private customerService: CustomerService, private orderService: OrderService) {}
  33. @Query()
  34. @Allow(Permission.ReadCustomer)
  35. async customers(
  36. @Ctx() ctx: RequestContext,
  37. @Args() args: QueryCustomersArgs,
  38. ): Promise<PaginatedList<Customer>> {
  39. return this.customerService.findAll(ctx, args.options || undefined);
  40. }
  41. @Query()
  42. @Allow(Permission.ReadCustomer)
  43. async customer(
  44. @Ctx() ctx: RequestContext,
  45. @Args() args: QueryCustomerArgs,
  46. ): Promise<Customer | undefined> {
  47. return this.customerService.findOne(ctx, args.id);
  48. }
  49. @Transaction()
  50. @Mutation()
  51. @Allow(Permission.CreateCustomer)
  52. async createCustomer(
  53. @Ctx() ctx: RequestContext,
  54. @Args() args: MutationCreateCustomerArgs,
  55. ): Promise<ErrorResultUnion<CreateCustomerResult, Customer>> {
  56. const { input, password } = args;
  57. return this.customerService.create(ctx, input, password || undefined);
  58. }
  59. @Transaction()
  60. @Mutation()
  61. @Allow(Permission.UpdateCustomer)
  62. async updateCustomer(
  63. @Ctx() ctx: RequestContext,
  64. @Args() args: MutationUpdateCustomerArgs,
  65. ): Promise<ErrorResultUnion<UpdateCustomerResult, Customer>> {
  66. const { input } = args;
  67. return this.customerService.update(ctx, input);
  68. }
  69. @Transaction()
  70. @Mutation()
  71. @Allow(Permission.CreateCustomer)
  72. async createCustomerAddress(
  73. @Ctx() ctx: RequestContext,
  74. @Args() args: MutationCreateCustomerAddressArgs,
  75. ): Promise<Address> {
  76. const { customerId, input } = args;
  77. return this.customerService.createAddress(ctx, customerId, input);
  78. }
  79. @Transaction()
  80. @Mutation()
  81. @Allow(Permission.UpdateCustomer)
  82. async updateCustomerAddress(
  83. @Ctx() ctx: RequestContext,
  84. @Args() args: MutationUpdateCustomerAddressArgs,
  85. ): Promise<Address> {
  86. const { input } = args;
  87. return this.customerService.updateAddress(ctx, input);
  88. }
  89. @Transaction()
  90. @Mutation()
  91. @Allow(Permission.DeleteCustomer)
  92. async deleteCustomerAddress(
  93. @Ctx() ctx: RequestContext,
  94. @Args() args: MutationDeleteCustomerAddressArgs,
  95. ): Promise<Success> {
  96. const { id } = args;
  97. const success = await this.customerService.deleteAddress(ctx, id);
  98. return { success };
  99. }
  100. @Transaction()
  101. @Mutation()
  102. @Allow(Permission.DeleteCustomer)
  103. async deleteCustomer(
  104. @Ctx() ctx: RequestContext,
  105. @Args() args: MutationDeleteCustomerArgs,
  106. ): Promise<DeletionResponse> {
  107. return this.customerService.softDelete(ctx, args.id);
  108. }
  109. @Transaction()
  110. @Mutation()
  111. @Allow(Permission.UpdateCustomer)
  112. async addNoteToCustomer(@Ctx() ctx: RequestContext, @Args() args: MutationAddNoteToCustomerArgs) {
  113. return this.customerService.addNoteToCustomer(ctx, args.input);
  114. }
  115. @Transaction()
  116. @Mutation()
  117. @Allow(Permission.UpdateCustomer)
  118. async updateCustomerNote(@Ctx() ctx: RequestContext, @Args() args: MutationUpdateCustomerNoteArgs) {
  119. return this.customerService.updateCustomerNote(ctx, args.input);
  120. }
  121. @Transaction()
  122. @Mutation()
  123. @Allow(Permission.UpdateCustomer)
  124. async deleteCustomerNote(@Ctx() ctx: RequestContext, @Args() args: MutationDeleteCustomerNoteArgs) {
  125. return this.customerService.deleteCustomerNote(ctx, args.id);
  126. }
  127. }