customer.resolver.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { Args, Mutation, Parent, Query, ResolveProperty, Resolver } from '@nestjs/graphql';
  2. import {
  3. CreateCustomerAddressMutationArgs,
  4. CreateCustomerMutationArgs,
  5. CustomerQueryArgs,
  6. CustomersQueryArgs,
  7. Permission,
  8. UpdateCustomerAddressMutationArgs,
  9. UpdateCustomerMutationArgs,
  10. } from '../../../../shared/generated-types';
  11. import { PaginatedList } from '../../../../shared/shared-types';
  12. import { idsAreEqual } from '../../common/utils';
  13. import { Address } from '../../entity/address/address.entity';
  14. import { Customer } from '../../entity/customer/customer.entity';
  15. import { CustomerService } from '../../service/services/customer.service';
  16. import { IdCodecService } from '../common/id-codec.service';
  17. import { RequestContext } from '../common/request-context';
  18. import { Allow } from '../decorators/allow.decorator';
  19. import { Decode } from '../decorators/decode.decorator';
  20. import { Ctx } from '../decorators/request-context.decorator';
  21. @Resolver('Customer')
  22. export class CustomerResolver {
  23. constructor(private customerService: CustomerService, private idCodecService: IdCodecService) {}
  24. @Query()
  25. @Allow(Permission.ReadCustomer)
  26. async customers(@Args() args: CustomersQueryArgs): Promise<PaginatedList<Customer>> {
  27. return this.customerService.findAll(args.options || undefined);
  28. }
  29. @Query()
  30. @Allow(Permission.ReadCustomer)
  31. async customer(@Args() args: CustomerQueryArgs): Promise<Customer | undefined> {
  32. return this.customerService.findOne(args.id);
  33. }
  34. @Query()
  35. @Allow(Permission.Owner)
  36. async activeCustomer(@Ctx() ctx: RequestContext): Promise<Customer | undefined> {
  37. const userId = ctx.activeUserId;
  38. if (userId) {
  39. return this.customerService.findOneByUserId(userId);
  40. }
  41. }
  42. @ResolveProperty()
  43. @Allow(Permission.ReadCustomer, Permission.Owner)
  44. async addresses(
  45. @Ctx() ctx: RequestContext,
  46. @Parent() customer: Customer,
  47. ): Promise<Address[] | undefined> {
  48. if (ctx.authorizedAsOwnerOnly) {
  49. const userId = customer.user && this.idCodecService.decode(customer.user.id);
  50. if (userId && !idsAreEqual(userId, ctx.activeUserId)) {
  51. return;
  52. }
  53. }
  54. const customerId = this.idCodecService.decode(customer.id);
  55. return this.customerService.findAddressesByCustomerId(customerId);
  56. }
  57. @Mutation()
  58. @Allow(Permission.CreateCustomer)
  59. async createCustomer(@Args() args: CreateCustomerMutationArgs): Promise<Customer> {
  60. const { input, password } = args;
  61. return this.customerService.create(input, password || undefined);
  62. }
  63. @Mutation()
  64. @Allow(Permission.UpdateCustomer)
  65. async updateCustomer(@Args() args: UpdateCustomerMutationArgs): Promise<Customer> {
  66. const { input } = args;
  67. return this.customerService.update(input);
  68. }
  69. @Mutation()
  70. @Allow(Permission.CreateCustomer)
  71. @Decode('customerId')
  72. async createCustomerAddress(@Args() args: CreateCustomerAddressMutationArgs): Promise<Address> {
  73. const { customerId, input } = args;
  74. return this.customerService.createAddress(customerId, input);
  75. }
  76. @Mutation()
  77. @Allow(Permission.UpdateCustomer)
  78. async updateCustomerAddress(@Args() args: UpdateCustomerAddressMutationArgs): Promise<Address> {
  79. const { input } = args;
  80. return this.customerService.updateAddress(input);
  81. }
  82. }