SystemInformationService.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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('slackbot-proxy:services:SystemInformationService');
  8. @Service()
  9. export class SystemInformationService {
  10. @Inject()
  11. private readonly repository: SystemInformationRepository;
  12. @Inject()
  13. relationsService: RelationsService;
  14. async $onInit(): Promise<void> {
  15. await this.onInitCheckVersion();
  16. }
  17. /*
  18. * updates version or create new system information record
  19. * make all relations expired if the previous version was <= 4.4.8
  20. */
  21. async onInitCheckVersion(): Promise<void> {
  22. const readPkgUpResult = await readPkgUp();
  23. const proxyVersion = readPkgUpResult?.packageJson.version;
  24. if (proxyVersion == null) return logger.error('version is null');
  25. const systemInfo: SystemInformation | undefined = await this.repository.findOne();
  26. // return if the version didn't change
  27. if (systemInfo != null && systemInfo.version === proxyVersion) {
  28. return;
  29. }
  30. await this.repository.createOrUpdateUniqueRecordWithVersion(systemInfo, proxyVersion);
  31. // make relations expired
  32. await this.relationsService.resetAllExpiredAtCommands();
  33. }
  34. }