get-superadmin-context.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { INestApplicationContext } from '@nestjs/common';
  2. import { ChannelService, ConfigService, RequestContext, TransactionalConnection, User } from '@vendure/core';
  3. /**
  4. * @description
  5. * Creates a {@link RequestContext} configured for the default Channel with the activeUser set
  6. * as the superadmin user. Useful for populating data.
  7. *
  8. * @docsCategory testing
  9. */
  10. export async function getSuperadminContext(app: INestApplicationContext): Promise<RequestContext> {
  11. const defaultChannel = await app.get(ChannelService).getDefaultChannel();
  12. const connection = app.get(TransactionalConnection);
  13. const configService = app.get(ConfigService);
  14. const { superadminCredentials } = configService.authOptions;
  15. const superAdminUser = await connection
  16. .getRepository(User)
  17. .findOneOrFail({ where: { identifier: superadminCredentials.identifier } });
  18. return new RequestContext({
  19. channel: defaultChannel,
  20. apiType: 'admin',
  21. isAuthorized: true,
  22. authorizedAsOwnerOnly: false,
  23. session: {
  24. id: '',
  25. token: '',
  26. expires: new Date(),
  27. cacheExpiry: 999999,
  28. user: {
  29. id: superAdminUser.id,
  30. identifier: superAdminUser.identifier,
  31. verified: true,
  32. channelPermissions: [],
  33. },
  34. },
  35. });
  36. }