get-default-channel-token.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { ConnectionOptions, getConnection } from 'typeorm';
  2. import { DEFAULT_CHANNEL_CODE } from '../src/common/constants';
  3. import { Channel } from '../src/entity/channel/channel.entity';
  4. // tslint:disable:no-console
  5. // tslint:disable:no-floating-promises
  6. /**
  7. * Queries the database for the default Channel and returns its token.
  8. */
  9. export async function getDefaultChannelToken(
  10. connectionOptions: ConnectionOptions,
  11. logging = true,
  12. ): Promise<string> {
  13. (connectionOptions as any).entities = [__dirname + '/../src/**/*.entity.ts'];
  14. const connection = await getConnection();
  15. let defaultChannel: Channel | undefined;
  16. try {
  17. defaultChannel = await connection.manager.getRepository(Channel).findOne({
  18. where: {
  19. code: DEFAULT_CHANNEL_CODE,
  20. },
  21. });
  22. } catch (err) {
  23. console.log(`Error occurred when attempting to get default Channel`);
  24. console.error(err);
  25. }
  26. if (!defaultChannel) {
  27. throw new Error(`No default channel could be found!`);
  28. }
  29. if (logging) {
  30. console.log(`Got default channel token: ${defaultChannel.token}`);
  31. }
  32. return defaultChannel.token;
  33. }