SystemInformationService.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { Inject, Service } from '@tsed/di';
  2. import readPkgUp from 'read-pkg-up';
  3. import { SystemInformation } from '~/entities/system-information';
  4. import { SystemInformationRepository } from '~/repositories/system-information';
  5. import loggerFactory from '~/utils/logger';
  6. import { RelationsService } from './RelationsService';
  7. const logger = loggerFactory(
  8. 'slackbot-proxy:services:SystemInformationService',
  9. );
  10. @Service()
  11. export class SystemInformationService {
  12. @Inject()
  13. private readonly repository: SystemInformationRepository;
  14. @Inject()
  15. relationsService: RelationsService;
  16. async $onInit(): Promise<void> {
  17. await this.onInitCheckVersion();
  18. }
  19. /*
  20. * updates version or create new system information record
  21. * make all relations expired if the previous version was <= 4.4.8
  22. */
  23. async onInitCheckVersion(): Promise<void> {
  24. const readPkgUpResult = await readPkgUp();
  25. const proxyVersion = readPkgUpResult?.packageJson.version;
  26. if (proxyVersion == null) return logger.error('version is null');
  27. const systemInfo: SystemInformation | undefined =
  28. await this.repository.findOne();
  29. // return if the version didn't change
  30. if (systemInfo != null && systemInfo.version === proxyVersion) {
  31. return;
  32. }
  33. await this.repository.createOrUpdateUniqueRecordWithVersion(
  34. systemInfo,
  35. proxyVersion,
  36. );
  37. // make relations expired
  38. await this.relationsService.resetAllExpiredAtCommands();
  39. }
  40. }