custom-history-entry-plugin.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { Args, Mutation, Resolver } from '@nestjs/graphql';
  2. import {
  3. Ctx,
  4. Customer,
  5. HistoryService,
  6. ID,
  7. Order,
  8. PluginCommonModule,
  9. RequestContext,
  10. TransactionalConnection,
  11. VendurePlugin,
  12. } from '@vendure/core';
  13. import gql from 'graphql-tag';
  14. import { CUSTOM_TYPE } from './types';
  15. @Resolver()
  16. class AddHistoryEntryResolver {
  17. constructor(private connection: TransactionalConnection, private historyService: HistoryService) {}
  18. @Mutation()
  19. async addCustomOrderHistoryEntry(
  20. @Ctx() ctx: RequestContext,
  21. @Args() args: { orderId: ID; message: string },
  22. ) {
  23. const order = await this.connection.getEntityOrThrow(ctx, Order, args.orderId);
  24. await this.historyService.createHistoryEntryForOrder({
  25. orderId: order.id,
  26. ctx,
  27. type: CUSTOM_TYPE,
  28. data: { message: args.message },
  29. });
  30. return order;
  31. }
  32. @Mutation()
  33. async addCustomCustomerHistoryEntry(
  34. @Ctx() ctx: RequestContext,
  35. @Args() args: { customerId: ID; name: string },
  36. ) {
  37. const customer = await this.connection.getEntityOrThrow(ctx, Customer, args.customerId);
  38. await this.historyService.createHistoryEntryForCustomer({
  39. customerId: customer.id,
  40. ctx,
  41. type: CUSTOM_TYPE,
  42. data: { name: args.name },
  43. });
  44. return customer;
  45. }
  46. }
  47. @VendurePlugin({
  48. imports: [PluginCommonModule],
  49. adminApiExtensions: {
  50. schema: gql`
  51. extend enum HistoryEntryType {
  52. CUSTOM_TYPE
  53. }
  54. extend type Mutation {
  55. addCustomOrderHistoryEntry(orderId: ID!, message: String!): Order!
  56. addCustomCustomerHistoryEntry(customerId: ID!, name: String!): Customer!
  57. }
  58. `,
  59. resolvers: [AddHistoryEntryResolver],
  60. },
  61. configuration: config => {
  62. return config;
  63. },
  64. })
  65. export class CustomHistoryEntryPlugin {}