Interfaces.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. export module Interfaces {
  2. export interface LoggerInterface {
  3. debug(message);
  4. error(message);
  5. }
  6. export interface ApiConnectorInterface {
  7. execute(endpointPath: string, data: any): Promise<string>;
  8. }
  9. export interface ClientInterface {
  10. id: string;
  11. coefficient: number;
  12. status: boolean;
  13. name: string;
  14. photo: string;
  15. payedTime: number;
  16. timeToPay: number;
  17. conversations: { [id: string]: ConversationInterface }
  18. send(event: string, data: {});
  19. addSocket(socket: SocketIO.Socket);
  20. sendMessage(messageData);
  21. }
  22. export interface ClientsManagerInterface {
  23. get(id: string): Promise<ClientInterface>
  24. add(socket: SocketIO.Socket, data);
  25. }
  26. export interface ConversationsManagerInterface {
  27. get(id: string): Promise<ConversationInterface>;
  28. add(conversation: ConversationInterface);
  29. remove(id: string);
  30. create(initiator: ClientInterface, recipient: ClientInterface): Promise<ConversationInterface>;
  31. }
  32. export interface ConversationInterface {
  33. id: string;
  34. duration: number;
  35. state: ConversationState;
  36. getInitiator(): ClientInterface;
  37. getRecipient(): ClientInterface;
  38. newMessage(message, from, to);
  39. }
  40. export class ConversationState {
  41. private value;
  42. private constructor(state) {
  43. if (ConversationState.availableStates().indexOf(state) < 0) {
  44. throw new Error(`State "${state}" is not valid value`);
  45. }
  46. this.value = state;
  47. }
  48. toString() {
  49. return this.value;
  50. }
  51. public isEqualsTo(state: ConversationState) {
  52. return this.value === state.value;
  53. }
  54. static create(state) {
  55. return new ConversationState(state);
  56. }
  57. private static availableStates() {
  58. return ['init', 'running', 'stopped', 'paused'];
  59. }
  60. static INIT() {
  61. return ConversationState.create('init');
  62. }
  63. static RUNNING() {
  64. return ConversationState.create('running');
  65. }
  66. static STOPPED() {
  67. return ConversationState.create('stopped');
  68. }
  69. static PAUSED() {
  70. return ConversationState.create('paused');
  71. }
  72. }
  73. export class AppMode {
  74. private value;
  75. private constructor(mode) {
  76. this.value = mode;
  77. }
  78. toString() {
  79. return this.value;
  80. }
  81. public isEqualsTo(mode: AppMode) {
  82. return this.value === mode.value;
  83. }
  84. static create(mode) {
  85. if (AppMode.availableModes().indexOf(mode) < 0) {
  86. throw new Error(`Mode "${mode}" is not valid value`);
  87. }
  88. return new AppMode(mode);
  89. }
  90. private static availableModes() {
  91. return ['dev', 'prod'];
  92. }
  93. static DEV() {
  94. return AppMode.create('dev');
  95. }
  96. static PROD() {
  97. return AppMode.create('prod');
  98. }
  99. }
  100. }