customer.resolver.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { Args, Mutation, Query, ResolveProperty, Resolver } from '@nestjs/graphql';
  2. import {
  3. CreateCustomerAddressMutationArgs,
  4. CreateCustomerMutationArgs,
  5. CustomerQueryArgs,
  6. CustomersQueryArgs,
  7. Permission,
  8. } from 'shared/generated-types';
  9. import { PaginatedList } from 'shared/shared-types';
  10. import { Address } from '../../entity/address/address.entity';
  11. import { Customer } from '../../entity/customer/customer.entity';
  12. import { CustomerService } from '../../service/services/customer.service';
  13. import { Allow } from '../common/auth-guard';
  14. import { Decode } from '../common/id-interceptor';
  15. @Resolver('Customer')
  16. export class CustomerResolver {
  17. constructor(private customerService: CustomerService) {}
  18. @Query()
  19. @Allow(Permission.ReadCustomer)
  20. async customers(@Args() args: CustomersQueryArgs): Promise<PaginatedList<Customer>> {
  21. return this.customerService.findAll(args.options || undefined);
  22. }
  23. @Query()
  24. @Allow(Permission.ReadCustomer)
  25. async customer(@Args() args: CustomerQueryArgs): Promise<Customer | undefined> {
  26. return this.customerService.findOne(args.id);
  27. }
  28. @ResolveProperty()
  29. @Allow(Permission.ReadCustomer)
  30. async addresses(customer: Customer): Promise<Address[]> {
  31. return this.customerService.findAddressesByCustomerId(customer.id);
  32. }
  33. @Mutation()
  34. @Allow(Permission.CreateCustomer)
  35. async createCustomer(@Args() args: CreateCustomerMutationArgs): Promise<Customer> {
  36. const { input, password } = args;
  37. return this.customerService.create(input, password || undefined);
  38. }
  39. @Mutation()
  40. @Allow(Permission.CreateCustomer)
  41. @Decode('customerId')
  42. async createCustomerAddress(@Args() args: CreateCustomerAddressMutationArgs): Promise<Address> {
  43. const { customerId, input } = args;
  44. return this.customerService.createAddress(customerId, input);
  45. }
  46. }