get-default-channel-token.ts 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. import { ConnectionOptions, getConnection } from 'typeorm';
  2. import { DEFAULT_CHANNEL_CODE } from '../../shared/shared-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(logging = true): Promise<string> {
  10. const connection = await getConnection();
  11. let defaultChannel: Channel | undefined;
  12. try {
  13. defaultChannel = await connection.manager.getRepository(Channel).findOne({
  14. where: {
  15. code: DEFAULT_CHANNEL_CODE,
  16. },
  17. });
  18. } catch (err) {
  19. console.log(`Error occurred when attempting to get default Channel`);
  20. console.error(err);
  21. }
  22. if (!defaultChannel) {
  23. throw new Error(`No default channel could be found!`);
  24. }
  25. if (logging) {
  26. console.log(`Got default channel token: ${defaultChannel.token}`);
  27. }
  28. return defaultChannel.token;
  29. }