customer-group.resolver.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
  2. import {
  3. DeletionResponse,
  4. MutationAddCustomersToGroupArgs,
  5. MutationCreateCustomerGroupArgs,
  6. MutationDeleteCustomerGroupArgs,
  7. MutationRemoveCustomersFromGroupArgs,
  8. MutationUpdateCustomerGroupArgs,
  9. Permission,
  10. QueryCustomerGroupArgs,
  11. QueryCustomerGroupsArgs,
  12. } from '@vendure/common/lib/generated-types';
  13. import { PaginatedList } from '@vendure/common/lib/shared-types';
  14. import { CustomerGroup } from '../../../entity/customer-group/customer-group.entity';
  15. import { CustomerGroupService } from '../../../service/services/customer-group.service';
  16. import { RequestContext } from '../../common/request-context';
  17. import { Allow } from '../../decorators/allow.decorator';
  18. import { Ctx } from '../../decorators/request-context.decorator';
  19. import { Transaction } from '../../decorators/transaction.decorator';
  20. @Resolver('CustomerGroup')
  21. export class CustomerGroupResolver {
  22. constructor(private customerGroupService: CustomerGroupService) {}
  23. @Query()
  24. @Allow(Permission.ReadCustomer)
  25. customerGroups(
  26. @Ctx() ctx: RequestContext,
  27. @Args() args: QueryCustomerGroupsArgs,
  28. ): Promise<PaginatedList<CustomerGroup>> {
  29. return this.customerGroupService.findAll(ctx, args.options || undefined);
  30. }
  31. @Query()
  32. @Allow(Permission.ReadCustomer)
  33. async customerGroup(
  34. @Ctx() ctx: RequestContext,
  35. @Args() args: QueryCustomerGroupArgs,
  36. ): Promise<CustomerGroup | undefined> {
  37. return this.customerGroupService.findOne(ctx, args.id);
  38. }
  39. @Transaction()
  40. @Mutation()
  41. @Allow(Permission.CreateCustomer)
  42. async createCustomerGroup(
  43. @Ctx() ctx: RequestContext,
  44. @Args() args: MutationCreateCustomerGroupArgs,
  45. ): Promise<CustomerGroup> {
  46. return this.customerGroupService.create(ctx, args.input);
  47. }
  48. @Transaction()
  49. @Mutation()
  50. @Allow(Permission.UpdateCustomer)
  51. async updateCustomerGroup(
  52. @Ctx() ctx: RequestContext,
  53. @Args() args: MutationUpdateCustomerGroupArgs,
  54. ): Promise<CustomerGroup> {
  55. return this.customerGroupService.update(ctx, args.input);
  56. }
  57. @Transaction()
  58. @Mutation()
  59. @Allow(Permission.DeleteCustomer)
  60. async deleteCustomerGroup(
  61. @Ctx() ctx: RequestContext,
  62. @Args() args: MutationDeleteCustomerGroupArgs,
  63. ): Promise<DeletionResponse> {
  64. return this.customerGroupService.delete(ctx, args.id);
  65. }
  66. @Transaction()
  67. @Mutation()
  68. @Allow(Permission.UpdateCustomer)
  69. async addCustomersToGroup(
  70. @Ctx() ctx: RequestContext,
  71. @Args() args: MutationAddCustomersToGroupArgs,
  72. ): Promise<CustomerGroup> {
  73. return this.customerGroupService.addCustomersToGroup(ctx, args);
  74. }
  75. @Transaction()
  76. @Mutation()
  77. @Allow(Permission.UpdateCustomer)
  78. async removeCustomersFromGroup(
  79. @Ctx() ctx: RequestContext,
  80. @Args() args: MutationRemoveCustomersFromGroupArgs,
  81. ): Promise<CustomerGroup> {
  82. return this.customerGroupService.removeCustomersFromGroup(ctx, args);
  83. }
  84. }