@kohost/api-client 3.0.0-beta.47 → 3.0.0-beta.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. package/dist/cjs/AMQPClient/index.js +290 -0
  2. package/dist/cjs/Commands/CheckInReservationCommand.js +19 -0
  3. package/dist/cjs/Commands/Command.js +34 -0
  4. package/dist/cjs/Commands/CreateImageUploadEndpointCommand.js +17 -0
  5. package/dist/cjs/Commands/CreateShortLinkCommand.js +20 -0
  6. package/dist/cjs/Commands/DiscoverReservationRoomTypeUpsellOptionsCommand.js +20 -0
  7. package/dist/cjs/Commands/DiscoverReservationsCommand.js +20 -0
  8. package/dist/cjs/Commands/DiscoverRoomTypesCommand.js +19 -0
  9. package/dist/cjs/Commands/DiscoverRoomsCommand.js +35 -0
  10. package/dist/cjs/Commands/DiscoverUsersCommand.js +19 -0
  11. package/dist/cjs/Commands/GetMobileKeyCommand.js +24 -0
  12. package/dist/cjs/Commands/GetProductsCommand.js +17 -0
  13. package/dist/cjs/Commands/OCRDocumentCommand.js +15 -0
  14. package/dist/cjs/Commands/SellProductsCommand.js +17 -0
  15. package/dist/cjs/Commands/SendEmailCommand.js +24 -0
  16. package/dist/cjs/Commands/SendSMSCommand.js +21 -0
  17. package/dist/cjs/Commands/SetAlarmCommand.js +17 -0
  18. package/dist/cjs/Commands/SetCourtesyCommand.js +17 -0
  19. package/dist/cjs/Commands/SetDimmerCommand.js +17 -0
  20. package/dist/cjs/Commands/SetLockCommand.js +17 -0
  21. package/dist/cjs/Commands/SetMediaCommand.js +17 -0
  22. package/dist/cjs/Commands/SetSceneCommand.js +17 -0
  23. package/dist/cjs/Commands/SetSwitchCommand.js +17 -0
  24. package/dist/cjs/Commands/SetThermostatCommand.js +17 -0
  25. package/dist/cjs/Commands/SetWindowCoveringCommand.js +17 -0
  26. package/dist/cjs/Commands/UpdateReservationCommand.js +19 -0
  27. package/dist/cjs/Commands/UpdateUserCommand.js +41 -0
  28. package/dist/cjs/Commands/UploadImageCommand.js +17 -0
  29. package/dist/cjs/Commands/index.js +55 -0
  30. package/dist/cjs/Errors/AppError.js +8 -0
  31. package/dist/cjs/Errors/AuthenticationError.js +9 -0
  32. package/dist/cjs/Errors/AuthorizationError.js +9 -0
  33. package/dist/cjs/Errors/DeviceCommError.js +9 -0
  34. package/dist/cjs/Errors/LoginError.js +9 -0
  35. package/dist/cjs/Errors/NotFoundError.js +9 -0
  36. package/dist/cjs/Errors/RequestError.js +9 -0
  37. package/dist/cjs/Errors/SystemCommError.js +9 -0
  38. package/dist/cjs/Errors/TokenExpiredError.js +9 -0
  39. package/dist/cjs/Errors/UnprocessableRequestError.js +9 -0
  40. package/dist/cjs/Errors/ValidationError.js +9 -0
  41. package/dist/cjs/Errors/index.js +15 -0
  42. package/dist/cjs/Events/ApplicationInUseEvent.js +17 -0
  43. package/dist/cjs/Events/ApplicationOutOfUseEvent.js +17 -0
  44. package/dist/cjs/Events/EmailEvent.js +18 -0
  45. package/dist/cjs/Events/Event.js +55 -0
  46. package/dist/cjs/Events/SMSEvent.js +18 -0
  47. package/dist/cjs/Events/SMSSentEvent.js +17 -0
  48. package/dist/cjs/Events/ShortLinkCreatedEvent.js +17 -0
  49. package/dist/cjs/Events/SystemCameraUpdatedEvent.js +17 -0
  50. package/dist/cjs/Events/SystemCourtesyUpdatedEvent.js +17 -0
  51. package/dist/cjs/Events/SystemDimmerUpdatedEvent.js +17 -0
  52. package/dist/cjs/Events/SystemGatewayUpdatedEvent.js +17 -0
  53. package/dist/cjs/Events/SystemLockUpdatedEvent.js +17 -0
  54. package/dist/cjs/Events/SystemMediaSourceUpdatedEvent.js +17 -0
  55. package/dist/cjs/Events/SystemMotionSensorUpdatedEvent.js +17 -0
  56. package/dist/cjs/Events/SystemProductUpdatedEvent.js +17 -0
  57. package/dist/cjs/Events/SystemReservationUpdatedEvent.js +17 -0
  58. package/dist/cjs/Events/SystemSceneControllerUpdatedEvent.js +17 -0
  59. package/dist/cjs/Events/SystemSpaceTypeUpdatedEvent.js +17 -0
  60. package/dist/cjs/Events/SystemSpaceUpdatedEvent.js +17 -0
  61. package/dist/cjs/Events/SystemSwitchUpdatedEvent.js +17 -0
  62. package/dist/cjs/Events/SystemThermostatUpdatedEvent.js +17 -0
  63. package/dist/cjs/Events/SystemUserUpdatedEvent.js +17 -0
  64. package/dist/cjs/Events/SystemWindowCoveringUpdatedEvent.js +17 -0
  65. package/dist/cjs/Events/index.js +50 -0
  66. package/dist/cjs/Models/Alarm.js +29 -0
  67. package/dist/cjs/Models/Camera.js +29 -0
  68. package/dist/cjs/Models/Courtesy.js +33 -0
  69. package/dist/cjs/Models/Credential.js +29 -0
  70. package/dist/cjs/Models/Dimmer.js +61 -0
  71. package/dist/cjs/Models/DiscoveredDevice.js +30 -0
  72. package/dist/cjs/Models/EmailMessage.js +29 -0
  73. package/dist/cjs/Models/EnergyReport.js +29 -0
  74. package/dist/cjs/Models/EnergyReportShard.js +29 -0
  75. package/dist/cjs/Models/Gateway.js +29 -0
  76. package/dist/cjs/Models/Identification.js +32 -0
  77. package/dist/cjs/Models/Kohost.js +96 -0
  78. package/dist/cjs/Models/Lock.js +33 -0
  79. package/dist/cjs/Models/MediaFile.js +41 -0
  80. package/dist/cjs/Models/MediaSource.js +29 -0
  81. package/dist/cjs/Models/MotionSensor.js +29 -0
  82. package/dist/cjs/Models/Organization.js +28 -0
  83. package/dist/cjs/Models/Product.js +29 -0
  84. package/dist/cjs/Models/Property.js +28 -0
  85. package/dist/cjs/Models/Reservation.js +98 -0
  86. package/dist/cjs/Models/Room.js +212 -0
  87. package/dist/cjs/Models/Scene.js +183 -0
  88. package/dist/cjs/Models/ShortLink.js +29 -0
  89. package/dist/cjs/Models/SmsMessage.js +29 -0
  90. package/dist/cjs/Models/Space.js +97 -0
  91. package/dist/cjs/Models/SpaceType.js +30 -0
  92. package/dist/cjs/Models/Switch.js +33 -0
  93. package/dist/cjs/Models/SystemUser.js +48 -0
  94. package/dist/cjs/Models/Thermostat.js +80 -0
  95. package/dist/cjs/Models/Ticket.js +105 -0
  96. package/dist/cjs/Models/User.js +73 -0
  97. package/dist/cjs/Models/WindowCovering.js +49 -0
  98. package/dist/cjs/Models/index.js +68 -0
  99. package/dist/cjs/{SocketIoClient.js → SocketIoClient/index.js} +19 -14
  100. package/dist/cjs/defs/deviceTypes.js +15 -0
  101. package/dist/cjs/defs/formalDeviceTypes.js +6 -0
  102. package/dist/cjs/defs/index.js +11 -0
  103. package/dist/cjs/index.js +23 -0
  104. package/dist/cjs/schemas/alarm.json +103 -0
  105. package/dist/cjs/schemas/camera.json +50 -0
  106. package/dist/cjs/schemas/courtesy.json +46 -0
  107. package/dist/cjs/schemas/credential.json +31 -0
  108. package/dist/cjs/schemas/definitions.json +191 -0
  109. package/dist/cjs/schemas/dimmer.json +43 -0
  110. package/dist/cjs/schemas/discoveredDevice.json +31 -0
  111. package/dist/cjs/schemas/emailMessage.json +71 -0
  112. package/dist/cjs/schemas/energyReport.json +86 -0
  113. package/dist/cjs/schemas/energyReportShard.json +75 -0
  114. package/dist/cjs/schemas/gateway.json +35 -0
  115. package/dist/cjs/schemas/identification.json +61 -0
  116. package/dist/cjs/schemas/lock.json +45 -0
  117. package/dist/cjs/schemas/mediaFile.json +67 -0
  118. package/dist/cjs/schemas/mediaSource.json +184 -0
  119. package/dist/cjs/schemas/motionSensor.json +32 -0
  120. package/dist/cjs/schemas/organization.json +36 -0
  121. package/dist/cjs/schemas/payment.json +47 -0
  122. package/dist/cjs/schemas/product.json +40 -0
  123. package/dist/cjs/schemas/property.json +246 -0
  124. package/dist/cjs/schemas/reservation.json +118 -0
  125. package/dist/cjs/schemas/room.json +98 -0
  126. package/dist/cjs/schemas/scene.json +121 -0
  127. package/dist/cjs/schemas/shortLink.json +30 -0
  128. package/dist/cjs/schemas/smsMessage.json +61 -0
  129. package/dist/cjs/schemas/space.json +120 -0
  130. package/dist/cjs/schemas/spaceType.json +30 -0
  131. package/dist/cjs/schemas/switch.json +42 -0
  132. package/dist/cjs/schemas/systemUser.json +95 -0
  133. package/dist/cjs/schemas/thermostat.json +153 -0
  134. package/dist/cjs/schemas/ticket.json +96 -0
  135. package/dist/cjs/schemas/user.json +152 -0
  136. package/dist/cjs/schemas/windowCovering.json +43 -0
  137. package/dist/cjs/utils/errorFactory.js +8 -0
  138. package/dist/cjs/utils/eventFactory.js +10 -0
  139. package/dist/cjs/utils/getDeviceTypes.js +7 -0
  140. package/dist/cjs/utils/getFormalDeviceType.js +5 -0
  141. package/dist/cjs/utils/index.js +11 -0
  142. package/dist/cjs/utils/schema.js +25 -0
  143. package/dist/esm/Models.js +451 -435
  144. package/dist/esm/Models.js.map +4 -4
  145. package/dist/esm/utils.js +7 -26
  146. package/dist/esm/utils.js.map +3 -3
  147. package/package.json +12 -7
  148. package/dist/cjs/AMQPClient.js +0 -462
  149. package/dist/cjs/Commands.js +0 -973
  150. package/dist/cjs/Errors.js +0 -209
  151. package/dist/cjs/Events.js +0 -578
  152. package/dist/cjs/Models.js +0 -4905
  153. package/dist/cjs/defs.js +0 -167
  154. package/dist/cjs/index.cjs.js +0 -6755
  155. package/dist/cjs/utils.js +0 -525
  156. /package/dist/cjs/{Client.js → Client/index.js} +0 -0
@@ -0,0 +1,183 @@
1
+ const schemas = require("../utils/schema");
2
+ const schema = require("../schemas/scene.json");
3
+ const Kohost = require("./Kohost");
4
+ const SetSceneCommand = require("../Commands/SetSceneCommand");
5
+
6
+ schemas.add(schema);
7
+ const validator = schemas.compile(schema);
8
+
9
+ class Scene extends Kohost {
10
+ constructor(data) {
11
+ super(data);
12
+ }
13
+
14
+ static createSceneCommandPayload(room, scene, restore) {
15
+ const commandsByDriver = [];
16
+
17
+ const sceneDevices = scene?.devices || {};
18
+ const sceneId = scene.id;
19
+
20
+ for (const deviceType in sceneDevices) {
21
+ const sceneData = sceneDevices[deviceType];
22
+
23
+ const roomDevices = room[deviceType];
24
+
25
+ if (sceneId === "1" && restore && deviceType !== "thermostats") continue;
26
+
27
+ for (const data of sceneData) {
28
+ const { id, ...deviceProps } = data;
29
+ if (id === "*") {
30
+ for (const device of roomDevices) {
31
+ const driver = device.driver;
32
+ const deviceCmd = {
33
+ id: device.systemData?.id,
34
+ };
35
+ for (const prop in deviceProps) {
36
+ if (prop === "setpointDelta") {
37
+ const delta = deviceProps[prop];
38
+ const currentMode = device.hvacMode;
39
+ const setpoints = device.setpoints;
40
+ const minAutoDelta = device.minAutoDelta;
41
+ const currentSetpoint = setpoints[currentMode];
42
+
43
+ if (currentMode === "heat") {
44
+ let setpointValue = Math.min(
45
+ currentSetpoint.min,
46
+ currentSetpoint.value - delta
47
+ );
48
+
49
+ if (restore) {
50
+ setpointValue = Math.min(
51
+ currentSetpoint.max,
52
+ currentSetpoint.value + delta
53
+ );
54
+ }
55
+
56
+ deviceCmd.setpoints = {
57
+ heat: {
58
+ value: setpointValue,
59
+ },
60
+ };
61
+ }
62
+ if (currentMode === "cool") {
63
+ let setpointValue = Math.max(
64
+ currentSetpoint.max,
65
+ currentSetpoint.value + delta
66
+ );
67
+
68
+ if (restore) {
69
+ setpointValue = Math.max(
70
+ currentSetpoint.min,
71
+ currentSetpoint.value - delta
72
+ );
73
+ }
74
+ deviceCmd.setpoints = {
75
+ cool: {
76
+ value: setpointValue,
77
+ },
78
+ };
79
+ }
80
+
81
+ if (currentMode === "auto") {
82
+ if (!currentSetpoint && setpoints.cool && setpoints.heat) {
83
+ let heatSetpoint = Math.min(
84
+ setpoints.heat.min,
85
+ setpoints.heat.value - delta
86
+ );
87
+
88
+ let coolSetpoint = Math.max(
89
+ setpoints.cool.max,
90
+ setpoints.cool.value + delta
91
+ );
92
+
93
+ if (restore) {
94
+ heatSetpoint = Math.min(
95
+ setpoints.heat.max,
96
+ setpoints.heat.value + delta
97
+ );
98
+
99
+ coolSetpoint = Math.max(
100
+ setpoints.cool.min,
101
+ setpoints.cool.value - delta
102
+ );
103
+ }
104
+
105
+ // make sure the delta is not less than the minAutoDelta
106
+ if (Math.abs(heatSetpoint - coolSetpoint) < minAutoDelta) {
107
+ continue;
108
+ }
109
+
110
+ deviceCmd.setpoints = {
111
+ heat: {
112
+ value: heatSetpoint,
113
+ },
114
+ cool: {
115
+ value: coolSetpoint,
116
+ },
117
+ };
118
+ }
119
+ }
120
+
121
+ if (currentMode === "off") {
122
+ // move to next loop
123
+ continue;
124
+ }
125
+ } else {
126
+ deviceCmd[prop] = deviceProps[prop];
127
+ }
128
+ }
129
+
130
+ // move to next loop if the only key in deviceCmd is id
131
+ if (Object.keys(deviceCmd).length === 1) {
132
+ continue;
133
+ }
134
+
135
+ const driverIndex = commandsByDriver.findIndex(
136
+ (c) => c.driver === driver
137
+ );
138
+ if (driverIndex >= 0) {
139
+ commandsByDriver[driverIndex].command.devices.push(deviceCmd);
140
+ } else {
141
+ commandsByDriver.push({
142
+ driver,
143
+ command: {
144
+ id: sceneId,
145
+ devices: [deviceCmd],
146
+ },
147
+ });
148
+ }
149
+ }
150
+ }
151
+ /**
152
+ * TODO: handle individual device ids
153
+ */
154
+ }
155
+ }
156
+
157
+ const commands = commandsByDriver.map((c) => {
158
+ const command = new SetSceneCommand(c.command);
159
+ return {
160
+ driver: c.driver,
161
+ command,
162
+ };
163
+ });
164
+
165
+ return commands;
166
+ }
167
+ }
168
+
169
+ Object.defineProperty(Scene.prototype, "schema", {
170
+ value: schema,
171
+ });
172
+
173
+ Object.defineProperty(Scene.prototype, "validator", {
174
+ get: function () {
175
+ return validator;
176
+ },
177
+ });
178
+
179
+ Object.defineProperty(Scene, "validProperties", {
180
+ value: Object.keys(schema.properties),
181
+ });
182
+
183
+ module.exports = Scene;
@@ -0,0 +1,29 @@
1
+ // Create the Lock Model
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/shortLink.json");
4
+ const Kohost = require("./Kohost");
5
+
6
+ schemas.add(schema);
7
+ const validator = schemas.compile(schema);
8
+
9
+ class ShortLink extends Kohost {
10
+ constructor(data) {
11
+ super(data);
12
+ }
13
+ }
14
+
15
+ Object.defineProperty(ShortLink.prototype, "schema", {
16
+ value: schema,
17
+ });
18
+
19
+ Object.defineProperty(ShortLink.prototype, "validator", {
20
+ get: function () {
21
+ return validator;
22
+ },
23
+ });
24
+
25
+ Object.defineProperty(ShortLink, "validProperties", {
26
+ value: Object.keys(schema.properties),
27
+ });
28
+
29
+ module.exports = ShortLink;
@@ -0,0 +1,29 @@
1
+ // Create the SMS Message Model
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/smsMessage.json");
4
+ const Kohost = require("./Kohost");
5
+
6
+ schemas.add(schema);
7
+ const validator = schemas.compile(schema);
8
+
9
+ class SMSMessage extends Kohost {
10
+ constructor(data) {
11
+ super(data);
12
+ }
13
+ }
14
+
15
+ Object.defineProperty(SMSMessage.prototype, "schema", {
16
+ value: schema,
17
+ });
18
+
19
+ Object.defineProperty(SMSMessage.prototype, "validator", {
20
+ get: function () {
21
+ return validator;
22
+ },
23
+ });
24
+
25
+ Object.defineProperty(SMSMessage, "validProperties", {
26
+ value: Object.keys(schema.properties),
27
+ });
28
+
29
+ module.exports = SMSMessage;
@@ -0,0 +1,97 @@
1
+ // Create the Space Model
2
+ // A group of rooms -> rooms could become a space later...
3
+ const schemas = require("../utils/schema");
4
+ const schema = require("../schemas/space.json");
5
+ const Kohost = require("./Kohost");
6
+
7
+ const Room = require("./Room");
8
+
9
+ schemas.add(schema);
10
+ const validator = schemas.compile(schema);
11
+
12
+ class Space extends Kohost {
13
+ /**
14
+ * @typedef {import("../schemas/SpaceSchema").Space} SpaceSchema
15
+ * Create a Dimmer instance.
16
+ * @constructor
17
+ * @param {SpaceSchema} space - The dimmer object of type Dimmer.
18
+ */
19
+ constructor(space) {
20
+ const spaceData = mapSpaceData(space);
21
+ super(spaceData);
22
+ }
23
+
24
+ get floor() {
25
+ const floors = new Set();
26
+
27
+ this.room.forEach((room) => {
28
+ if (room.floor) floors.add(room.floor);
29
+ });
30
+
31
+ return floors.size == 1 ? [...floors][0] : undefined;
32
+ }
33
+
34
+ get hasDimmer() {
35
+ return this.rooms.some((room) => room.hasDimmer);
36
+ }
37
+
38
+ get hasSwitch() {
39
+ return this.rooms.some((room) => room.hasSwitch);
40
+ }
41
+
42
+ get hasWindowCovering() {
43
+ return this.rooms.some((room) => room.hasWindowCovering);
44
+ }
45
+
46
+ get hasThermostat() {
47
+ return this.rooms.some((room) => room.hasThermostat);
48
+ }
49
+
50
+ get hasLock() {
51
+ return this.rooms.some((room) => room.hasLock);
52
+ }
53
+
54
+ get hasCourtesy() {
55
+ return this.rooms.some((room) => room.hasCourtesy);
56
+ }
57
+
58
+ get hasCamera() {
59
+ return this.rooms.some((room) => room.hasCamera);
60
+ }
61
+
62
+ get hasAlarm() {
63
+ return this.rooms.some((room) => room.hasAlarm);
64
+ }
65
+
66
+ get hasMedia() {
67
+ return this.rooms.some((room) => room.hasMedia);
68
+ }
69
+ }
70
+
71
+ Object.defineProperty(Space.prototype, "schema", {
72
+ value: schema,
73
+ });
74
+
75
+ Object.defineProperty(Space.prototype, "validator", {
76
+ get: function () {
77
+ return validator;
78
+ },
79
+ });
80
+
81
+ Object.defineProperty(Space, "validProperties", {
82
+ value: Object.keys(schema.properties),
83
+ });
84
+
85
+ function mapSpaceData(data) {
86
+ const spaceData = structuredClone(data);
87
+ if (spaceData.rooms?.length) {
88
+ spaceData.rooms.map((room) => {
89
+ if (typeof room === "string") return room;
90
+ if (room instanceof Room) return room;
91
+ return new Room(room);
92
+ });
93
+ }
94
+ return spaceData;
95
+ }
96
+
97
+ module.exports = Space;
@@ -0,0 +1,30 @@
1
+ // Create the Space Type Model
2
+ // Originally used for hotel room type e.g. Double Queen
3
+ const schemas = require("../utils/schema");
4
+ const schema = require("../schemas/spaceType.json");
5
+ const Kohost = require("./Kohost");
6
+
7
+ schemas.add(schema);
8
+ const validator = schemas.compile(schema);
9
+
10
+ class SpaceType extends Kohost {
11
+ constructor(data) {
12
+ super(data);
13
+ }
14
+ }
15
+
16
+ Object.defineProperty(SpaceType.prototype, "schema", {
17
+ value: schema,
18
+ });
19
+
20
+ Object.defineProperty(SpaceType.prototype, "validator", {
21
+ get: function () {
22
+ return validator;
23
+ },
24
+ });
25
+
26
+ Object.defineProperty(SpaceType, "validProperties", {
27
+ value: Object.keys(schema.properties),
28
+ });
29
+
30
+ module.exports = SpaceType;
@@ -0,0 +1,33 @@
1
+ // create the Switch model
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/switch.json");
4
+ const Kohost = require("./Kohost");
5
+
6
+ schemas.add(schema);
7
+ const validator = schemas.compile(schema);
8
+
9
+ class Switch extends Kohost {
10
+ constructor(data) {
11
+ super(data);
12
+ }
13
+ }
14
+
15
+ Object.defineProperty(Switch.prototype, "schema", {
16
+ value: schema,
17
+ });
18
+
19
+ Object.defineProperty(Switch.prototype, "validator", {
20
+ get: function () {
21
+ return validator;
22
+ },
23
+ });
24
+
25
+ Object.defineProperty(Switch, "validProperties", {
26
+ value: Object.keys(schema.properties),
27
+ });
28
+
29
+ Object.defineProperty(Switch, "actionProperties", {
30
+ value: ["state"],
31
+ });
32
+
33
+ module.exports = Switch;
@@ -0,0 +1,48 @@
1
+ // Create the User Model
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/systemUser.json");
4
+ const Kohost = require("./Kohost");
5
+
6
+ schemas.add(schema);
7
+
8
+ const validator = schemas.compile(schema);
9
+
10
+ class SystemUser extends Kohost {
11
+ constructor(data) {
12
+ super(data);
13
+ }
14
+
15
+ static validatePhone(phoneNumber) {
16
+ const regex = /^\+?[1-9]\d{1,14}$/;
17
+ return regex.test(phoneNumber);
18
+ }
19
+
20
+ static validateEmail(email) {
21
+ //eslint-disable-next-line no-useless-escape
22
+ const regex =
23
+ /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
24
+ return regex.test(email);
25
+ }
26
+ }
27
+
28
+ Object.defineProperty(SystemUser.prototype, "schema", {
29
+ value: schema,
30
+ });
31
+
32
+ Object.defineProperty(SystemUser.prototype, "validator", {
33
+ get: function () {
34
+ return validator;
35
+ },
36
+ });
37
+
38
+ Object.defineProperty(SystemUser, "validProperties", {
39
+ value: Object.keys(schema.properties),
40
+ });
41
+
42
+ Object.defineProperty(SystemUser.prototype, "fullName", {
43
+ get: function () {
44
+ return `${this.firstName} ${this.lastName}`;
45
+ },
46
+ });
47
+
48
+ module.exports = SystemUser;
@@ -0,0 +1,80 @@
1
+ const schemas = require("../utils/schema");
2
+ const schema = require("../schemas/thermostat.json");
3
+ const Kohost = require("./Kohost");
4
+
5
+ schemas.add(schema);
6
+ const validator = schemas.compile(schema);
7
+
8
+ class Thermostat extends Kohost {
9
+ constructor(data) {
10
+ super(data);
11
+ }
12
+
13
+ toCelsius() {
14
+ if (this.temperatureScale === "fahrenheit")
15
+ this.currentTemperature = ((this.currentTemperature - 32) * 5) / 9;
16
+ this.temperatureScale = "celsius";
17
+ return this.currentTemperature;
18
+ }
19
+
20
+ toFahrenheit() {
21
+ if (this.temperatureScale === "celsius")
22
+ this.currentTemperature = (this.currentTemperature * 9) / 5 + 32;
23
+ this.temperatureScale = "fahrenheit";
24
+ return this.currentTemperature;
25
+ }
26
+
27
+ static getActionDelta(old, _new) {
28
+ const delta = {};
29
+ for (const action in _new) {
30
+ if (this.actionProperties.includes(action)) {
31
+ switch (action) {
32
+ case "hvacMode":
33
+ case "fanMode": {
34
+ if (old[action] !== _new[action]) delta[action] = 1;
35
+ break;
36
+ }
37
+ case "setpoints": {
38
+ const setpoints = _new[action];
39
+ for (const setpoint in setpoints) {
40
+ if (old[action][setpoint].value !== setpoints[setpoint].value) {
41
+ const min =
42
+ setpoints[setpoint].min || old[action][setpoint].min;
43
+ const max =
44
+ setpoints[setpoint].max || old[action][setpoint].max;
45
+ const oldValue = old[action][setpoint].value;
46
+ const value = setpoints[setpoint].value;
47
+ // get percentage change relative to min and max
48
+ const percentChange = (value - oldValue) / (max - min);
49
+ // get the delta
50
+ delta[`setpoints.${setpoint}`] = percentChange;
51
+ }
52
+ }
53
+ }
54
+ }
55
+ }
56
+ }
57
+
58
+ return delta;
59
+ }
60
+ }
61
+
62
+ Object.defineProperty(Thermostat.prototype, "schema", {
63
+ value: schema,
64
+ });
65
+
66
+ Object.defineProperty(Thermostat.prototype, "validator", {
67
+ get: function () {
68
+ return validator;
69
+ },
70
+ });
71
+
72
+ Object.defineProperty(Thermostat, "validProperties", {
73
+ value: Object.keys(schema.properties),
74
+ });
75
+
76
+ Object.defineProperty(Thermostat, "actionProperties", {
77
+ value: ["hvacMode", "fanMode", "setpoints"],
78
+ });
79
+
80
+ module.exports = Thermostat;
@@ -0,0 +1,105 @@
1
+ // Create the User Model
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/ticket.json");
4
+ const Kohost = require("./Kohost");
5
+ const MediaFile = require("./MediaFile");
6
+
7
+ const sortBy = require("lodash.sortby");
8
+ const findLast = require("lodash.findlast");
9
+
10
+ const { nanoid } = require("nanoid");
11
+ const cloneDeep = require("lodash.clonedeep");
12
+
13
+ schemas.add(schema);
14
+ const validator = schemas.compile(schema);
15
+
16
+ class Ticket extends Kohost {
17
+ constructor(data) {
18
+ const ticketData = mapConversationData(data);
19
+ super(ticketData);
20
+ }
21
+
22
+ static generateMessageId(len = 16) {
23
+ return nanoid(len);
24
+ }
25
+ }
26
+
27
+ Object.defineProperty(Ticket.prototype, "schema", {
28
+ value: schema,
29
+ });
30
+
31
+ Object.defineProperty(Ticket.prototype, "validator", {
32
+ get: function () {
33
+ return validator;
34
+ },
35
+ });
36
+
37
+ Object.defineProperty(Ticket, "validProperties", {
38
+ value: Object.keys(schema.properties),
39
+ });
40
+
41
+ Object.defineProperty(Ticket.prototype, "responseTime", {
42
+ get: function () {
43
+ const conversation = this.conversation;
44
+ const requester = this.requester;
45
+
46
+ if (conversation.length === 0) return 0;
47
+ const mapped = conversation.map((msg) => {
48
+ if (typeof msg.timestamp === "string")
49
+ msg.timestamp = new Date(msg.timestamp);
50
+ return msg;
51
+ });
52
+ const sorted = sortBy(mapped, ["timestamp"]);
53
+ const firstMsg = sorted.find((entry) => entry.userId === requester);
54
+ const firstResponse = sorted.find((entry) => entry.userId !== requester);
55
+
56
+ if (firstMsg && firstResponse) {
57
+ const firstMsgTime = firstMsg.timestamp.getTime() / 1000;
58
+ const firstResponseTime = firstResponse.timestamp.getTime() / 1000;
59
+ return firstResponseTime - firstMsgTime;
60
+ }
61
+
62
+ return 0;
63
+ },
64
+ });
65
+
66
+ Object.defineProperty(Ticket.prototype, "resolutionTime", {
67
+ get: function () {
68
+ if (this.status !== "closed") return 0;
69
+ const createdAt = this.createdAt.getTime() / 1000;
70
+ const solvedAt = this.solvedAt
71
+ ? this.solvedAt.getTime() / 1000
72
+ : this.updatedAt.getTime() / 1000;
73
+
74
+ return Math.abs(solvedAt - createdAt);
75
+ },
76
+ });
77
+
78
+ Object.defineProperty(Ticket.prototype, "lastResponder", {
79
+ get: function () {
80
+ const conversation = this.conversation;
81
+ const requester = this.requester;
82
+
83
+ const sorted = sortBy(conversation, ["timestamp"]);
84
+
85
+ const lastFromNonRequester = findLast(sorted, function (c) {
86
+ return c.userId !== requester;
87
+ });
88
+
89
+ if (!lastFromNonRequester) return null;
90
+ else return lastFromNonRequester.userId;
91
+ },
92
+ });
93
+
94
+ function mapConversationData(data) {
95
+ const ticketData = cloneDeep(data);
96
+ ticketData.conversation = ticketData.conversation.map((msg) => {
97
+ if (msg.media) {
98
+ msg.media = new MediaFile(msg.media);
99
+ }
100
+ return msg;
101
+ });
102
+ return ticketData;
103
+ }
104
+
105
+ module.exports = Ticket;