customer.resolver.ts 4.4 KB

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