@internetderdinge/api 1.224.2 → 1.229.1

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 (169) hide show
  1. package/dist/src/accounts/accounts.controller.js +89 -0
  2. package/dist/src/accounts/accounts.route.js +101 -0
  3. package/dist/src/accounts/accounts.schemas.js +12 -0
  4. package/dist/src/accounts/accounts.service.js +65 -0
  5. package/dist/src/accounts/accounts.validation.js +99 -0
  6. package/dist/src/accounts/auth0.service.js +188 -0
  7. package/dist/src/config/config.js +48 -0
  8. package/dist/src/config/logger.js +27 -0
  9. package/dist/src/config/morgan.js +16 -0
  10. package/dist/src/config/passport.cjs +28 -0
  11. package/dist/src/config/roles.js +11 -0
  12. package/dist/src/config/tokens.cjs +10 -0
  13. package/dist/src/devices/devices.controller.js +172 -0
  14. package/dist/src/devices/devices.model.js +94 -0
  15. package/dist/src/devices/devices.route.js +153 -0
  16. package/dist/src/devices/devices.schemas.js +84 -0
  17. package/dist/src/devices/devices.service.js +198 -0
  18. package/dist/src/devices/devices.types.js +1 -0
  19. package/dist/src/devices/devices.validation.js +257 -0
  20. package/dist/src/devicesNotifications/devicesNotifications.controller.js +69 -0
  21. package/dist/src/devicesNotifications/devicesNotifications.model.js +39 -0
  22. package/dist/src/devicesNotifications/devicesNotifications.route.js +124 -0
  23. package/dist/src/devicesNotifications/devicesNotifications.schemas.js +10 -0
  24. package/dist/src/devicesNotifications/devicesNotifications.service.js +181 -0
  25. package/dist/src/devicesNotifications/devicesNotifications.validation.js +46 -0
  26. package/dist/src/email/email.service.js +580 -0
  27. package/dist/src/files/upload.service.js +124 -0
  28. package/dist/src/i18n/i18n.js +38 -0
  29. package/dist/src/i18n/saveMissingLocalJsonBackend.js +53 -0
  30. package/dist/src/i18n/types.js +1 -0
  31. package/dist/src/index.js +48 -0
  32. package/dist/src/iotdevice/iotdevice.controller.js +96 -0
  33. package/dist/src/iotdevice/iotdevice.model.js +17 -0
  34. package/dist/src/iotdevice/iotdevice.route.js +143 -0
  35. package/dist/src/iotdevice/iotdevice.schemas.js +60 -0
  36. package/dist/src/iotdevice/iotdevice.service.js +579 -0
  37. package/dist/src/iotdevice/iotdevice.types.js +1 -0
  38. package/dist/src/iotdevice/iotdevice.validation.js +54 -0
  39. package/dist/src/middlewares/auth.js +75 -0
  40. package/dist/src/middlewares/checkJwt.cjs +17 -0
  41. package/dist/src/middlewares/error.js +36 -0
  42. package/dist/src/middlewares/mongooseValidations/ensureSameOrganization.js +13 -0
  43. package/dist/src/middlewares/rateLimiter.js +7 -0
  44. package/dist/src/middlewares/validate.js +18 -0
  45. package/dist/src/middlewares/validateAction.js +35 -0
  46. package/dist/src/middlewares/validateAdmin.js +18 -0
  47. package/dist/src/middlewares/validateAi.js +16 -0
  48. package/dist/src/middlewares/validateCurrentAuthUser.js +17 -0
  49. package/dist/src/middlewares/validateCurrentUser.js +20 -0
  50. package/dist/src/middlewares/validateDevice.js +98 -0
  51. package/dist/src/middlewares/validateDeviceUserOrganization.js +26 -0
  52. package/dist/src/middlewares/validateOrganization.js +63 -0
  53. package/dist/src/middlewares/validateQuerySearchUserAndOrganization.js +44 -0
  54. package/dist/src/middlewares/validateTokens.js +23 -0
  55. package/dist/src/middlewares/validateUser.js +38 -0
  56. package/dist/src/middlewares/validateZod.js +33 -0
  57. package/dist/src/models/plugins/index.js +4 -0
  58. package/dist/src/models/plugins/paginate.plugin.js +117 -0
  59. package/dist/src/models/plugins/paginateNew.plugin.js +185 -0
  60. package/dist/src/models/plugins/simplePopulate.js +16 -0
  61. package/dist/src/models/plugins/toJSON.plugin.js +35 -0
  62. package/dist/src/organizations/organizations.controller.js +64 -0
  63. package/dist/src/organizations/organizations.model.js +41 -0
  64. package/dist/src/organizations/organizations.route.js +98 -0
  65. package/dist/src/organizations/organizations.schemas.js +7 -0
  66. package/dist/src/organizations/organizations.service.js +59 -0
  67. package/dist/src/organizations/organizations.validation.js +62 -0
  68. package/dist/src/pdf/pdf.controller.js +24 -0
  69. package/dist/src/pdf/pdf.route.js +22 -0
  70. package/dist/src/pdf/pdf.schemas.js +6 -0
  71. package/dist/src/pdf/pdf.service.js +65 -0
  72. package/dist/src/pdf/pdf.validation.js +27 -0
  73. package/dist/src/tokens/tokens.controller.js +60 -0
  74. package/dist/src/tokens/tokens.model.js +18 -0
  75. package/dist/src/tokens/tokens.route.js +52 -0
  76. package/dist/src/tokens/tokens.schemas.js +14 -0
  77. package/dist/src/tokens/tokens.service.js +30 -0
  78. package/dist/src/tokens/tokens.validation.js +9 -0
  79. package/dist/src/types/routeSpec.js +1 -0
  80. package/dist/src/users/users.controller.js +147 -0
  81. package/dist/src/users/users.model.js +50 -0
  82. package/dist/src/users/users.route.js +137 -0
  83. package/dist/src/users/users.schemas.js +69 -0
  84. package/dist/src/users/users.service.js +295 -0
  85. package/dist/src/users/users.types.js +1 -0
  86. package/dist/src/users/users.validation.js +144 -0
  87. package/dist/src/utils/ApiError.js +16 -0
  88. package/dist/src/utils/buildRouterAndDocs.js +72 -0
  89. package/dist/src/utils/catchAsync.js +4 -0
  90. package/dist/src/utils/comparePapers.service.js +32 -0
  91. package/dist/src/utils/deviceUtils.js +63 -0
  92. package/dist/src/utils/filterOptions.js +24 -0
  93. package/dist/src/utils/medicationName.js +10 -0
  94. package/dist/src/utils/pick.js +16 -0
  95. package/dist/src/utils/registerOpenApi.js +28 -0
  96. package/dist/src/utils/urlUtils.js +15 -0
  97. package/dist/src/utils/userName.js +22 -0
  98. package/dist/src/utils/zValidations.js +124 -0
  99. package/dist/src/validations/auth.validation.cjs +53 -0
  100. package/dist/src/validations/custom.validation.js +19 -0
  101. package/dist/src/validations/index.cjs +3 -0
  102. package/package.json +15 -6
  103. package/scripts/release-and-sync-paperless.mjs +135 -0
  104. package/scripts/release-version.mjs +145 -0
  105. package/src/accounts/accounts.controller.ts +1 -0
  106. package/src/accounts/accounts.service.ts +1 -0
  107. package/src/accounts/accounts.validation.ts +6 -3
  108. package/src/accounts/auth0.service.ts +55 -28
  109. package/src/config/config.ts +6 -0
  110. package/src/config/logger.ts +15 -9
  111. package/src/devices/devices.controller.ts +7 -1
  112. package/src/devices/devices.model.ts +4 -1
  113. package/src/devices/devices.schemas.ts +10 -8
  114. package/src/devices/devices.service.ts +2 -1
  115. package/src/devices/devices.types.ts +1 -0
  116. package/src/devices/devices.validation.ts +85 -23
  117. package/src/devicesNotifications/devicesNotifications.controller.ts +57 -28
  118. package/src/devicesNotifications/devicesNotifications.model.ts +20 -12
  119. package/src/devicesNotifications/devicesNotifications.service.ts +35 -17
  120. package/src/files/upload.service.ts +52 -28
  121. package/src/i18n/i18n.ts +2 -2
  122. package/src/i18n/types.ts +1 -0
  123. package/src/index.ts +47 -0
  124. package/src/iotdevice/iotdevice.controller.ts +1 -0
  125. package/src/iotdevice/iotdevice.model.ts +6 -3
  126. package/src/iotdevice/iotdevice.route.ts +85 -76
  127. package/src/iotdevice/iotdevice.service.ts +5 -4
  128. package/src/iotdevice/iotdevice.types.ts +6 -0
  129. package/src/middlewares/auth.ts +2 -8
  130. package/src/middlewares/error.ts +26 -12
  131. package/src/middlewares/mongooseValidations/ensureSameOrganization.ts +4 -3
  132. package/src/middlewares/validateAi.ts +17 -9
  133. package/src/middlewares/validateDevice.ts +1 -0
  134. package/src/middlewares/validateDeviceUserOrganization.ts +1 -0
  135. package/src/middlewares/validateOrganization.ts +1 -1
  136. package/src/middlewares/validateQuerySearchUserAndOrganization.ts +1 -0
  137. package/src/middlewares/validateTokens.ts +2 -1
  138. package/src/middlewares/validateUser.ts +1 -0
  139. package/src/models/plugins/index.ts +5 -4
  140. package/src/models/plugins/paginate.plugin.ts +26 -16
  141. package/src/models/plugins/paginateNew.plugin.ts +33 -21
  142. package/src/models/plugins/simplePopulate.ts +8 -3
  143. package/src/models/plugins/toJSON.plugin.ts +12 -5
  144. package/src/organizations/organizations.controller.ts +1 -2
  145. package/src/organizations/organizations.model.ts +4 -4
  146. package/src/organizations/organizations.route.ts +1 -1
  147. package/src/organizations/organizations.service.ts +15 -6
  148. package/src/pdf/pdf.controller.ts +18 -1
  149. package/src/pdf/pdf.service.ts +25 -16
  150. package/src/tokens/tokens.controller.ts +6 -8
  151. package/src/tokens/tokens.model.ts +3 -1
  152. package/src/tokens/tokens.service.ts +3 -2
  153. package/src/types/express.d.ts +17 -0
  154. package/src/types/mongoose.d.ts +22 -0
  155. package/src/users/users.controller.ts +8 -9
  156. package/src/users/users.model.ts +6 -5
  157. package/src/users/users.route.ts +0 -1
  158. package/src/users/users.service.ts +16 -0
  159. package/src/users/users.types.ts +1 -0
  160. package/src/users/users.validation.ts +6 -2
  161. package/src/utils/ApiError.ts +8 -1
  162. package/src/utils/buildRouterAndDocs.ts +56 -21
  163. package/src/utils/catchAsync.ts +27 -3
  164. package/src/utils/deviceUtils.ts +109 -0
  165. package/src/utils/medicationName.ts +5 -4
  166. package/src/utils/pick.ts +5 -1
  167. package/src/utils/userName.ts +1 -0
  168. package/src/utils/zValidations.ts +78 -26
  169. package/tsconfig.json +13 -4
@@ -0,0 +1,10 @@
1
+ import { z } from 'zod';
2
+ export const deviceNotificationResponseSchema = z.object({
3
+ id: z.string().uuid(),
4
+ userId: z.string().uuid(),
5
+ deviceToken: z.string(),
6
+ // optionally include platform or other metadata:
7
+ platform: z.enum(['ios', 'android']).optional(),
8
+ createdAt: z.string().datetime(),
9
+ updatedAt: z.string().datetime(),
10
+ });
@@ -0,0 +1,181 @@
1
+ // @ts-nocheck
2
+ import httpStatus from "http-status";
3
+ import { SESv2Client, ListSuppressedDestinationsCommand, } from "@aws-sdk/client-sesv2";
4
+ import DeviceNotification from "./devicesNotifications.model";
5
+ import ApiError from "../../src/utils/ApiError";
6
+ import { auth0 } from "../accounts/auth0.service";
7
+ /**
8
+ * Create a user
9
+ * @param {Object} userBody
10
+ * @returns {Promise<Notification>}
11
+ */
12
+ export const createNotificationUser = async ({ user, }) => {
13
+ const result = await DeviceNotification.findOne({ user });
14
+ if (result)
15
+ throw new ApiError(httpStatus.NOT_FOUND, "DevicesNotifications user existing");
16
+ const devicesNotification = await DeviceNotification.create({ user });
17
+ return devicesNotification;
18
+ };
19
+ /**
20
+ * Set the device token for a user
21
+ * @param {Object} userBody
22
+ * @returns {Promise<Notification>}
23
+ */
24
+ export const setDeviceToken = async ({ user, body, }) => {
25
+ const { token, ...meta } = body;
26
+ let result = await DeviceNotification.findOne({ user });
27
+ if (!result) {
28
+ result = await createNotificationUser({ user });
29
+ }
30
+ result.tokens = [
31
+ ...new Map(result.tokens.map((m) => [m.deviceId, m])).values(),
32
+ ];
33
+ if (result.tokens.find((t) => t.token === token))
34
+ return result;
35
+ result.tokens = result.tokens.filter((t) => t.deviceId !== meta.deviceId);
36
+ result.tokens.push({ token, ...meta });
37
+ await result.save();
38
+ return result;
39
+ };
40
+ /**
41
+ * Remove an existing device token usually used when the user is getting logged out
42
+ * @param {Object} userBody
43
+ * @returns {Promise<Notification>}
44
+ */
45
+ export const removeDeviceToken = async ({ user, body: { deviceId }, }) => {
46
+ let result = await DeviceNotification.findOne({ user });
47
+ if (!result) {
48
+ result = await createNotificationUser({ user });
49
+ }
50
+ if (result.tokens.find((t) => t.deviceId === deviceId)) {
51
+ result.tokens = result.tokens.filter((t) => t.deviceId !== deviceId);
52
+ await result.save();
53
+ }
54
+ return result;
55
+ };
56
+ /*
57
+ const convertAllTokens = async () => {
58
+ const devicesNotifications = await DeviceNotification.find();
59
+
60
+ await Promise.all(
61
+ devicesNotifications.map(async (user) => {
62
+ //const tokenListNewFiltered = user.tokens.filter((v, i, a) => a.findIndex((v2) => v2 === v) === i);
63
+
64
+ const tokenListNew = user.tokens.map((ttt) => {
65
+ if (typeof ttt === 'string') {
66
+ return { token: ttt };
67
+ }
68
+ return ttt;
69
+ });
70
+
71
+ console.log(tokenListNew);
72
+
73
+ const devicesNotification = await updateById(user.id, {
74
+ tokens: [],
75
+ });
76
+ })
77
+ );
78
+ };*/
79
+ /**
80
+ * Query for users
81
+ * @param {Object} filter - Mongo filter
82
+ * @param {Object} options - Query options
83
+ * @param {string} [options.sortBy] - Sort option in the format: sortField:(desc|asc)
84
+ * @param {number} [options.limit] - Maximum number of results per page (default = 10)
85
+ * @param {number} [options.page] - Current page (default = 1)
86
+ * @returns {Promise<QueryResult>}
87
+ */
88
+ const queryNotificationsByUser = async (filter, options) => {
89
+ return DeviceNotification.paginate(filter, options);
90
+ };
91
+ const getById = async (id) => {
92
+ return DeviceNotification.findById(id);
93
+ };
94
+ const getByUser = async (user) => {
95
+ return DeviceNotification.findOne({ user });
96
+ };
97
+ const updateById = async (notificationId, updateBody) => {
98
+ const user = await getById(notificationId);
99
+ if (!user) {
100
+ throw new ApiError(httpStatus.NOT_FOUND, "Notification not found");
101
+ }
102
+ Object.assign(user, updateBody);
103
+ await user.save();
104
+ return user;
105
+ };
106
+ const updateByUserId = async (user, updateBody) => {
107
+ const userEntryExists = await DeviceNotification.findOne({ user });
108
+ if (!userEntryExists) {
109
+ const newUser = await createNotification({ user });
110
+ //throw new ApiError(httpStatus.NOT_FOUND, 'DeviceNotification not found');
111
+ }
112
+ const userEntry = await DeviceNotification.findOne({ user });
113
+ Object.assign(userEntry, updateBody);
114
+ await userEntry.save();
115
+ return userEntry;
116
+ };
117
+ /**
118
+ * Delete user by id
119
+ * @param {ObjectId} userId
120
+ * @returns {Promise<Notification>}
121
+ */
122
+ const deleteById = async (userId) => {
123
+ const user = await getById(userId);
124
+ if (!user) {
125
+ throw new ApiError(httpStatus.NOT_FOUND, "Notification not found");
126
+ }
127
+ await user.deleteOne();
128
+ return user;
129
+ };
130
+ /**
131
+ * Delete user by id
132
+ * @param {ObjectId} userId
133
+ * @returns {Promise<Notification>}
134
+ */
135
+ const cleanup = async () => {
136
+ const deviceNotifications = await DeviceNotification.find();
137
+ const auth0Users = await auth0.getUsers();
138
+ const sesV2 = new SESv2Client({ region: "eu-central-1" });
139
+ const input = {
140
+ Reasons: ["BOUNCE" || "COMPLAINT"],
141
+ PageSize: 300,
142
+ };
143
+ const command = new ListSuppressedDestinationsCommand(input);
144
+ const suppressedDestinations = await sesV2.send(command);
145
+ const suppressedEmailList = suppressedDestinations.SuppressedDestinationSummaries.map((a) => a.EmailAddress);
146
+ const suppressedEmailAuth0Users = auth0Users.filter((a) => suppressedEmailList.includes(a.email));
147
+ await Promise.all(suppressedEmailAuth0Users.map(async (a) => {
148
+ updateByUserId(a.user_id, { bounceEmail: a.email });
149
+ }));
150
+ return { suppressedEmailList, suppressedEmailAuth0Users };
151
+ /* deviceNotifications.map((d) => {
152
+ d.tokens.forEach((token) => {
153
+
154
+ if token
155
+ });
156
+ });*/
157
+ //const resultJson = result.toJSON();
158
+ //const resultsFiltered = result.filter((e) => e.patientData === null);
159
+ //const idList = resultsFiltered.map((e) => e._id);
160
+ // const deleted = await calendarsService.deleteMany(idList);
161
+ return { deviceNotifications, auth0Users, response };
162
+ /* res.send({
163
+ deleted,
164
+ idList,
165
+ resultsFiltered,
166
+ resultsFilteredLength: resultsFiltered.length,
167
+ resultLength: result.length,
168
+ });*/
169
+ };
170
+ export default {
171
+ createNotificationUser,
172
+ getById,
173
+ cleanup,
174
+ queryNotificationsByUser,
175
+ setDeviceToken,
176
+ removeDeviceToken,
177
+ getByUser,
178
+ updateById,
179
+ updateByUserId,
180
+ deleteById,
181
+ };
@@ -0,0 +1,46 @@
1
+ import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
2
+ import { z } from 'zod';
3
+ import { zPagination, zGet, zPatchBody, zUpdate, zDelete } from '../../src/utils/zValidations';
4
+ extendZodWithOpenApi(z);
5
+ export const createEntrySchema = {
6
+ body: z.object({
7
+ token: z.string().optional().openapi({ description: 'Push token' }),
8
+ }),
9
+ };
10
+ export const setDeviceTokenSchema = {
11
+ body: z.object({
12
+ token: z.string().openapi({ description: 'Device token' }),
13
+ deviceId: z.string().openapi({ description: 'Device identifier' }),
14
+ plattform: z.string().openapi({ description: 'Platform name' }),
15
+ app: z.string().optional().openapi({ description: 'Application name' }),
16
+ }),
17
+ };
18
+ export const removeDeviceTokenSchema = {
19
+ body: z.object({
20
+ deviceId: z.string().openapi({ description: 'Device identifier' }),
21
+ plattform: z.string().optional().openapi({ description: 'Platform name' }),
22
+ app: z.string().optional().openapi({ description: 'Application name' }),
23
+ token: z.string().optional().openapi({ description: 'Push token' }),
24
+ }),
25
+ };
26
+ export const getUsersSchema = {
27
+ ...zPagination,
28
+ query: zPagination.query.extend({
29
+ name: z.string().optional().openapi({ description: 'Filter by user name' }),
30
+ role: z.string().optional().openapi({ description: 'Filter by role' }),
31
+ sortBy: z.string().optional().openapi({ description: 'Sort order' }),
32
+ }),
33
+ };
34
+ export const getEntrySchema = zGet('notificationId');
35
+ export const updateEntrySchema = {
36
+ ...zUpdate('notificationId'),
37
+ body: zPatchBody({
38
+ name: z.string().optional().openapi({ description: 'Notification name' }),
39
+ conditions: z.array(z.any()).optional().openapi({ description: 'Conditions array' }),
40
+ actions: z.array(z.any()).optional().openapi({ description: 'Actions array' }),
41
+ organization: z.string().optional().openapi({ description: 'Organization ID' }),
42
+ }),
43
+ };
44
+ export const deleteEntrySchema = zDelete('notificationId');
45
+ export const cleanupSchema = {};
46
+ export const queryNotificationsByUserSchema = {};