incomingMessage.service.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { map, Observable, of, tap } from "rxjs";
  2. import { BaseMessage, Command, ResponseMessage, Uuid } from "../dependencies/fisappmessagejsutilty/interface/export"
  3. import { LoggingService } from "../dependencies/fisloggingservice/services/logging-service";
  4. import { LogSetting, MessageLog } from "../dependencies/fisloggingservice/type/datatype";
  5. import { IncomingMessageServiceInterface } from "../type/datatype";
  6. export class IncomingMessageService implements IncomingMessageServiceInterface {
  7. constructor(private logService?: LoggingService) {
  8. this.logService = new LoggingService()
  9. }
  10. private settings: LogSetting & { incomingObservable: Observable<BaseMessage>; } = {
  11. storage: '',
  12. setting: {
  13. appId: '',
  14. appName: '',
  15. logLocName: '',
  16. logLocId: '',
  17. appLogLocId: '',
  18. appLocName: '',
  19. appLogId: ''
  20. },
  21. incomingObservable: null
  22. }
  23. /* This function is mainful used for setting the log setting as well as transforming the messages
  24. into log messages and then storing them in the desiganted location of storage as specified. */
  25. public init(settings: LogSetting & { incomingObservable: Observable<BaseMessage>; }): void {
  26. // Restructuring the settings. I think they were some trouble doing this last time
  27. let newSetting: any = settings
  28. newSetting.setting = {
  29. ...this.settings.setting,
  30. ...settings.setting,
  31. customSetting: {
  32. ...this.settings.customSetting,
  33. ...settings.customSetting,
  34. }
  35. }
  36. this.settings = newSetting // Become stateful???
  37. // Transform incoming observables into Observable<MessageLog> to be logged
  38. let transformedOBS: Observable<MessageLog> = settings.incomingObservable.pipe(
  39. map(message => {
  40. let finalResponse: MessageLog = {
  41. appLogLocId: new Uuid().generateId(),
  42. appData: {
  43. msgId: message.header.messageID || new Uuid().generateId(),
  44. msgLogDateTime: new Date(),
  45. msgDateTime: new Date(),
  46. msgTag: ['Incoming'],
  47. msgPayload: JSON.stringify(message)
  48. }
  49. }
  50. return finalResponse
  51. })
  52. )
  53. // Once the messages has been transformed, then the logging can be executed
  54. this.logService.init(this.settings).then(() => {
  55. this.logService.subscribe(transformedOBS)
  56. }).catch((e) => console.error(e))
  57. }
  58. }