prow-authority 3.6.3

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 (168) hide show
  1. package/.claude/settings.local.json +11 -0
  2. package/.dockerignore +11 -0
  3. package/.github/workflows/_deploy.yml +191 -0
  4. package/.github/workflows/deploy-dev.yml +30 -0
  5. package/.github/workflows/deploy-prod.yml +34 -0
  6. package/CLAUDE.md +114 -0
  7. package/Dockerfile +61 -0
  8. package/Dockerfile.dev +24 -0
  9. package/README.md +1 -0
  10. package/package.json +61 -0
  11. package/src/App.ts +40 -0
  12. package/src/helpers/api-error.ts +21 -0
  13. package/src/helpers/authority.ts +216 -0
  14. package/src/helpers/config.ts +4 -0
  15. package/src/helpers/holded.ts +105 -0
  16. package/src/helpers/mysql.ts +15 -0
  17. package/src/helpers/notifications.ts +55 -0
  18. package/src/helpers/random.ts +18 -0
  19. package/src/helpers/s3.ts +43 -0
  20. package/src/helpers/stripe.ts +8 -0
  21. package/src/index.ts +47 -0
  22. package/src/v1/common-types.ts +12 -0
  23. package/src/v1/controllers/admin/accounting-seat-controller.ts +148 -0
  24. package/src/v1/controllers/admin/billing-cycle-controller.ts +171 -0
  25. package/src/v1/controllers/admin/invoice-controller.ts +222 -0
  26. package/src/v1/controllers/admin/license-controller.ts +190 -0
  27. package/src/v1/controllers/admin/license-limit-controller.ts +160 -0
  28. package/src/v1/controllers/admin/notification-controller.ts +115 -0
  29. package/src/v1/controllers/admin/organization-controller.ts +210 -0
  30. package/src/v1/controllers/admin/organization-license-controller.ts +224 -0
  31. package/src/v1/controllers/admin/organization-license-limit-controller.ts +196 -0
  32. package/src/v1/controllers/admin/organization-usage-controller.ts +167 -0
  33. package/src/v1/controllers/admin/payment-method-controller.ts +173 -0
  34. package/src/v1/controllers/admin/policy-controller.ts +138 -0
  35. package/src/v1/controllers/admin/policy-version-controller.ts +143 -0
  36. package/src/v1/controllers/admin/schedule-controller.ts +181 -0
  37. package/src/v1/controllers/admin/service-controller.ts +206 -0
  38. package/src/v1/controllers/admin/service-notification-controller.ts +166 -0
  39. package/src/v1/controllers/admin/user-controller.ts +282 -0
  40. package/src/v1/controllers/admin/user-organization-controller.ts +172 -0
  41. package/src/v1/controllers/admin/user-policy-controller.ts +164 -0
  42. package/src/v1/controllers/auth/@deprecated oauth-controller.ts +191 -0
  43. package/src/v1/controllers/auth/identity-token-controller.ts +286 -0
  44. package/src/v1/controllers/auth/organization-token-controller.ts +277 -0
  45. package/src/v1/controllers/auth/temp-code-controller.ts +60 -0
  46. package/src/v1/controllers/billing/billing-controller.ts +805 -0
  47. package/src/v1/controllers/organizations/public-organization-controller.ts +1796 -0
  48. package/src/v1/controllers/policies/public-policy-controller.ts +68 -0
  49. package/src/v1/controllers/services/public-service-controller.ts +177 -0
  50. package/src/v1/controllers/services/service-controller.ts +474 -0
  51. package/src/v1/controllers/users/public-user-controller.ts +471 -0
  52. package/src/v1/daos/@deprecated auth-dao.ts +173 -0
  53. package/src/v1/daos/accounting-seat-dao.ts +111 -0
  54. package/src/v1/daos/billing-cycle-dao.ts +107 -0
  55. package/src/v1/daos/invoice-dao.ts +131 -0
  56. package/src/v1/daos/license-dao.ts +152 -0
  57. package/src/v1/daos/license-limit-dao.ts +120 -0
  58. package/src/v1/daos/organization-dao.ts +109 -0
  59. package/src/v1/daos/organization-license-dao.ts +117 -0
  60. package/src/v1/daos/organization-license-limit-dao.ts +157 -0
  61. package/src/v1/daos/organization-usage-dao.ts +115 -0
  62. package/src/v1/daos/payment-method-dao.ts +110 -0
  63. package/src/v1/daos/policy-dao.ts +105 -0
  64. package/src/v1/daos/policy-version-dao.ts +112 -0
  65. package/src/v1/daos/redis-dao.ts +38 -0
  66. package/src/v1/daos/schema.sql +462 -0
  67. package/src/v1/daos/service-dao.ts +118 -0
  68. package/src/v1/daos/service-notification-dao.ts +120 -0
  69. package/src/v1/daos/user-dao.ts +127 -0
  70. package/src/v1/daos/user-organization-dao.ts +123 -0
  71. package/src/v1/daos/user-policy-dao.ts +109 -0
  72. package/src/v1/middlewares/auth/identity-token-middleware.ts +51 -0
  73. package/src/v1/middlewares/auth/organization-token-middleware.ts +73 -0
  74. package/src/v1/middlewares/auth/service-token-middleware.ts +167 -0
  75. package/src/v1/middlewares/authority-middleware.ts +97 -0
  76. package/src/v1/middlewares/organizations/organization-middleware.ts +94 -0
  77. package/src/v1/middlewares/static/static-middleware.ts +41 -0
  78. package/src/v1/middlewares/users/user-middleware.ts +41 -0
  79. package/src/v1/routes/admin/accounting-seat-router.ts +89 -0
  80. package/src/v1/routes/admin/admin-router.ts +61 -0
  81. package/src/v1/routes/admin/billing-cycle-router.ts +89 -0
  82. package/src/v1/routes/admin/invoice-router.ts +89 -0
  83. package/src/v1/routes/admin/license-limit-router.ts +89 -0
  84. package/src/v1/routes/admin/license-router.ts +89 -0
  85. package/src/v1/routes/admin/notification-router.ts +61 -0
  86. package/src/v1/routes/admin/organization-license-limit-router.ts +89 -0
  87. package/src/v1/routes/admin/organization-license-router.ts +89 -0
  88. package/src/v1/routes/admin/organization-router.ts +89 -0
  89. package/src/v1/routes/admin/payment-method-router.ts +89 -0
  90. package/src/v1/routes/admin/policy-router.ts +89 -0
  91. package/src/v1/routes/admin/policy-version-router.ts +89 -0
  92. package/src/v1/routes/admin/schedule-router.ts +75 -0
  93. package/src/v1/routes/admin/service-notification-router.ts +98 -0
  94. package/src/v1/routes/admin/service-router.ts +89 -0
  95. package/src/v1/routes/admin/user-organization-router.ts +89 -0
  96. package/src/v1/routes/admin/user-policy-router.ts +89 -0
  97. package/src/v1/routes/admin/user-router.ts +89 -0
  98. package/src/v1/routes/auth/@deprecated auth-router.ts +199 -0
  99. package/src/v1/routes/auth/identity-token-router.ts +147 -0
  100. package/src/v1/routes/auth/organization-token-router.ts +70 -0
  101. package/src/v1/routes/billing/billing-router.ts +227 -0
  102. package/src/v1/routes/organizations/invoices-router.ts +48 -0
  103. package/src/v1/routes/organizations/payments-router.ts +104 -0
  104. package/src/v1/routes/organizations/public-organization-router.ts +319 -0
  105. package/src/v1/routes/organizations/users-router.ts +139 -0
  106. package/src/v1/routes/policies/public-policy-router.ts +50 -0
  107. package/src/v1/routes/services/public-service-router.ts +89 -0
  108. package/src/v1/routes/services/service-router.ts +197 -0
  109. package/src/v1/routes/users/public-user-router.ts +230 -0
  110. package/src/v1/routes/v1-router.ts +50 -0
  111. package/src/v1/schemas/admin/accounting-seat-schemas.ts +22 -0
  112. package/src/v1/schemas/admin/billing-cycle-schemas.ts +20 -0
  113. package/src/v1/schemas/admin/invoice-schemas.ts +41 -0
  114. package/src/v1/schemas/admin/license-limit-schemas.ts +25 -0
  115. package/src/v1/schemas/admin/license-schemas.ts +30 -0
  116. package/src/v1/schemas/admin/organization-license-limit-schemas.ts +29 -0
  117. package/src/v1/schemas/admin/organization-license-schemas.ts +43 -0
  118. package/src/v1/schemas/admin/organization-schemas.ts +67 -0
  119. package/src/v1/schemas/admin/organization-usage-schemas.ts +15 -0
  120. package/src/v1/schemas/admin/payment-method-schemas.ts +40 -0
  121. package/src/v1/schemas/admin/policy-schemas.ts +31 -0
  122. package/src/v1/schemas/admin/service-notification-schemas.ts +23 -0
  123. package/src/v1/schemas/admin/service-schemas.ts +56 -0
  124. package/src/v1/schemas/admin/user-organization-schemas.ts +24 -0
  125. package/src/v1/schemas/admin/user-policy-schemas.ts +17 -0
  126. package/src/v1/schemas/admin/user-schemas.ts +52 -0
  127. package/src/v1/schemas/auth/auth-schemas.ts +55 -0
  128. package/src/v1/schemas/billing/billing-schemas.ts +32 -0
  129. package/src/v1/schemas/organizations/public-organization-schemas.ts +77 -0
  130. package/src/v1/schemas/payments/payment-method-schemas.ts +0 -0
  131. package/src/v1/schemas/services/public-service-schemas.ts +5 -0
  132. package/src/v1/schemas/services/service-schemas.ts +27 -0
  133. package/src/v1/schemas/users/public-user-schemas.ts +73 -0
  134. package/src/v1/types/admin/accounting-seat-types.ts +16 -0
  135. package/src/v1/types/admin/billing-cycle-types.ts +14 -0
  136. package/src/v1/types/admin/invoice-types.ts +39 -0
  137. package/src/v1/types/admin/license-limit-types.ts +16 -0
  138. package/src/v1/types/admin/license-types.ts +29 -0
  139. package/src/v1/types/admin/organization-license-limit-types.ts +13 -0
  140. package/src/v1/types/admin/organization-license-types.ts +21 -0
  141. package/src/v1/types/admin/organization-types.ts +60 -0
  142. package/src/v1/types/admin/organization-usage-types.ts +17 -0
  143. package/src/v1/types/admin/payment-method-types.ts +30 -0
  144. package/src/v1/types/admin/policy-types.ts +9 -0
  145. package/src/v1/types/admin/policy-version-types.ts +12 -0
  146. package/src/v1/types/admin/service-notification-types.ts +20 -0
  147. package/src/v1/types/admin/service-types.ts +36 -0
  148. package/src/v1/types/admin/user-organization-types.ts +31 -0
  149. package/src/v1/types/admin/user-types.ts +44 -0
  150. package/src/v1/types/auth/@deprecated auth-types.ts +26 -0
  151. package/src/v1/types/auth/common-types.ts +7 -0
  152. package/src/v1/types/auth/confirmation-types.ts +5 -0
  153. package/src/v1/types/auth/identity-token-types.ts +8 -0
  154. package/src/v1/types/auth/organization-token-types.ts +23 -0
  155. package/src/v1/types/auth/otp-types.ts +12 -0
  156. package/src/v1/types/billing/billing-types.ts +37 -0
  157. package/src/v1/types/billing/ue-types.ts +29 -0
  158. package/src/v1/types/organizations/public-organization-types.ts +76 -0
  159. package/src/v1/types/policies/public-policy-types.ts +7 -0
  160. package/src/v1/types/services/public-service-types.ts +20 -0
  161. package/src/v1/types/services/service-types.ts +43 -0
  162. package/src/v1/types/users/public-user-types.ts +15 -0
  163. package/src/v1/types/users/user-policy-types.ts +11 -0
  164. package/static/error.html +55 -0
  165. package/static/grant.html +117 -0
  166. package/static/login.html +54 -0
  167. package/static/oauth-client-logo.webp +0 -0
  168. package/tsconfig.json +20 -0
@@ -0,0 +1,227 @@
1
+ import { NextFunction, Request, Response, Router } from 'express';
2
+ import { billing } from '../../middlewares/static/static-middleware';
3
+ import httpcodes from 'http-status-codes';
4
+ import billingController from '../../controllers/billing/billing-controller';
5
+ import APIError from '../../../helpers/api-error';
6
+ import organizationController from '../../controllers/admin/organization-controller';
7
+ import { Organization, OrganizationStatus } from '../../types/admin/organization-types';
8
+
9
+ class BillingRouter {
10
+ router: Router;
11
+
12
+ constructor() {
13
+ this.router = Router();
14
+ this.init();
15
+ }
16
+
17
+ private readonly test_asOrganization = async (req: Request, res: Response, next: NextFunction) => {
18
+ try {
19
+ const organizationUUID = (req.query[`organization`] as string || ``).trim();
20
+ if (!organizationUUID) { throw new Error(`Missing or invalid 'organization' query parameter`); }
21
+
22
+ // validate organization
23
+ const org = (await organizationController.list({ uuid: organizationUUID, status: { is: OrganizationStatus.Blocked, inverse: true } }))[0];
24
+ if (!org) { throw new Error(`Invalid 'organization' query parameter value: unknown organization`); }
25
+
26
+ (req[`organization`] as Organization) = org;
27
+
28
+ next();
29
+ } catch (e) {
30
+ console.log(e);
31
+ res.status(httpcodes.UNAUTHORIZED).json({ error: e.message || e });
32
+ }
33
+ };
34
+
35
+ private readonly test_generateCycle = async (req: Request, res: Response) => {
36
+ try {
37
+ const response = await billingController.generateCycle((req[`organization`] as Organization), req.body);
38
+ res.json(response);
39
+ } catch (e) {
40
+ if (e instanceof APIError) {
41
+ res.status(e.code).json(e.data);
42
+ } else {
43
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
44
+ }
45
+ }
46
+ };
47
+
48
+ private readonly test_generateInvoice = async (req: Request, res: Response) => {
49
+ try {
50
+ const response = await billingController.generateLicensesInvoice((req[`organization`] as Organization), req.body);
51
+ res.json(response);
52
+ } catch (e) {
53
+ if (e instanceof APIError) {
54
+ res.status(e.code).json(e.data);
55
+ } else {
56
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
57
+ }
58
+ }
59
+ };
60
+
61
+ private readonly getInvoices = async (req: Request, res: Response) => {
62
+ try {
63
+ const response = await billingController.getInvoices(req.body);
64
+ res.json(response);
65
+ } catch (e) {
66
+ if (e instanceof APIError) {
67
+ res.status(e.code).json(e.data);
68
+ } else {
69
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
70
+ }
71
+ }
72
+ };
73
+
74
+ private readonly updateInvoice = async (req: Request, res: Response) => {
75
+ try {
76
+ const response = await billingController.updateInvoice(req.params[`uuid`], req.body);
77
+ res.json(response);
78
+ } catch (e) {
79
+ if (e instanceof APIError) {
80
+ res.status(e.code).json(e.data);
81
+ } else {
82
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
83
+ }
84
+ }
85
+ };
86
+
87
+ private readonly invoiceEmitted = async (req: Request, res: Response) => {
88
+ try {
89
+ const response = await billingController.invoiceEmitted(req.params[`uuid`], req.body);
90
+ res.json(response);
91
+ } catch (e) {
92
+ if (e instanceof APIError) {
93
+ res.status(e.code).json(e.data);
94
+ } else {
95
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
96
+ }
97
+ }
98
+ };
99
+
100
+ private readonly invoicePaid = async (req: Request, res: Response) => {
101
+ try {
102
+ const response = await billingController.invoicePaid(req.params[`uuid`], req.body);
103
+ res.json(response);
104
+ } catch (e) {
105
+ if (e instanceof APIError) {
106
+ res.status(e.code).json(e.data);
107
+ } else {
108
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
109
+ }
110
+ }
111
+ };
112
+
113
+ private readonly getAccountingSeats = async (req: Request, res: Response) => {
114
+ try {
115
+ const response = await billingController.getAccountingSeats(req.body);
116
+ res.json(response);
117
+ } catch (e) {
118
+ if (e instanceof APIError) {
119
+ res.status(e.code).json(e.data);
120
+ } else {
121
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
122
+ }
123
+ }
124
+ };
125
+
126
+ private readonly accountingSeatDone = async (req: Request, res: Response) => {
127
+ try {
128
+ const response = await billingController.accountingSeatDone(req.params[`uuid`]);
129
+ res.json(response);
130
+ } catch (e) {
131
+ if (e instanceof APIError) {
132
+ res.status(e.code).json(e.data);
133
+ } else {
134
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
135
+ }
136
+ }
137
+ };
138
+
139
+ private readonly blockOrganization = async (req: Request, res: Response) => {
140
+ try {
141
+ const response = await billingController.blockOrganization(req.params[`uuid`]);
142
+ res.json(response);
143
+ } catch (e) {
144
+ if (e instanceof APIError) {
145
+ res.status(e.code).json(e.data);
146
+ } else {
147
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
148
+ }
149
+ }
150
+ };
151
+
152
+ private readonly unblockOrganization = async (req: Request, res: Response) => {
153
+ try {
154
+ const response = await billingController.unblockOrganization(req.params[`uuid`]);
155
+ res.json(response);
156
+ } catch (e) {
157
+ if (e instanceof APIError) {
158
+ res.status(e.code).json(e.data);
159
+ } else {
160
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
161
+ }
162
+ }
163
+ };
164
+
165
+ private readonly organizationAccountingData = async (req: Request, res: Response) => {
166
+ try {
167
+ const response = await billingController.organizationAccountingData(req.params[`uuid`]);
168
+ res.json(response);
169
+ } catch (e) {
170
+ if (e instanceof APIError) {
171
+ res.status(e.code).json(e.data);
172
+ } else {
173
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
174
+ }
175
+ }
176
+ };
177
+
178
+ private readonly organizationPaymentData = async (req: Request, res: Response) => {
179
+ try {
180
+ const response = await billingController.organizationPaymentData(req.params[`uuid`]);
181
+ res.json(response);
182
+ } catch (e) {
183
+ if (e instanceof APIError) {
184
+ res.status(e.code).json(e.data);
185
+ } else {
186
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
187
+ }
188
+ }
189
+ };
190
+
191
+
192
+ private readonly check = async (req: Request, res: Response) => {
193
+ try {
194
+ const response = { valid: true };
195
+ res.json(response);
196
+ } catch (e) {
197
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
198
+ }
199
+ };
200
+
201
+ private readonly init = () => {
202
+ this.router.use(billing);
203
+
204
+ this.router.get(`/check`, this.check);
205
+
206
+ this.router.post(`/invoices`, this.getInvoices);
207
+ this.router.post(`/invoices/:uuid/emitted`, this.invoiceEmitted);
208
+ this.router.post(`/invoices/:uuid/paid`, this.invoicePaid);
209
+ this.router.patch(`/invoices/:uuid`, this.updateInvoice);
210
+
211
+ this.router.post(`/accounting-seats`, this.getAccountingSeats);
212
+ this.router.post(`/accounting-seats/:uuid/done`, this.accountingSeatDone);
213
+
214
+ this.router.patch(`/organizations/:uuid/block`, this.blockOrganization);
215
+ this.router.patch(`/organizations/:uuid/unblock`, this.unblockOrganization);
216
+ this.router.get(`/organizations/:uuid/accounting`, this.organizationAccountingData);
217
+ this.router.get(`/organizations/:uuid/payment`, this.organizationPaymentData);
218
+
219
+ // for testing purposes
220
+ this.router.use(this.test_asOrganization);
221
+ this.router.post(`/cycles/generate`, this.test_generateCycle);
222
+ this.router.post(`/invoices/generate`, this.test_generateInvoice);
223
+ };
224
+ }
225
+
226
+ const billingRouter = (new BillingRouter()).router;
227
+ export default billingRouter;
@@ -0,0 +1,48 @@
1
+ import { Request, Response, Router } from 'express';
2
+ import httpcodes from 'http-status-codes';
3
+ import APIError from '../../../helpers/api-error';
4
+ import publicOrganizationController from '../../controllers/organizations/public-organization-controller';
5
+ import { Environment } from '../../types/auth/organization-token-types';
6
+
7
+ class InvoiceRouter {
8
+ router: Router;
9
+
10
+ constructor() {
11
+ this.router = Router();
12
+ this.init();
13
+ }
14
+
15
+ private readonly listInvoices = async (req: Request, res: Response) => {
16
+ try {
17
+ const response = await publicOrganizationController.listInvoices(req[`environment`] as Environment);
18
+ res.json(response);
19
+ } catch (e) {
20
+ if (e instanceof APIError) {
21
+ res.status(e.code).json(e.data);
22
+ } else {
23
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
24
+ }
25
+ }
26
+ };
27
+
28
+ private readonly getInvoicePDF = async (req: Request, res: Response) => {
29
+ try {
30
+ const response = await publicOrganizationController.getInvoicePDF(req[`environment`] as Environment, req.params[`uuid`]);
31
+ res.json(response);
32
+ } catch (e) {
33
+ if (e instanceof APIError) {
34
+ res.status(e.code).json(e.data);
35
+ } else {
36
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
37
+ }
38
+ }
39
+ };
40
+
41
+ private readonly init = () => {
42
+ this.router.post(`/list`, this.listInvoices);
43
+ this.router.get(`/:uuid`, this.getInvoicePDF);
44
+ };
45
+ }
46
+
47
+ const invoiceRouter = (new InvoiceRouter()).router;
48
+ export default invoiceRouter;
@@ -0,0 +1,104 @@
1
+ import { Request, Response, Router } from 'express';
2
+ import httpcodes from 'http-status-codes';
3
+ import APIError from '../../../helpers/api-error';
4
+ import publicOrganizationController from '../../controllers/organizations/public-organization-controller';
5
+ import { Environment } from '../../types/auth/organization-token-types';
6
+
7
+ class PaymentMethodRouter {
8
+ router: Router;
9
+
10
+ constructor() {
11
+ this.router = Router();
12
+ this.init();
13
+ }
14
+
15
+ private readonly setupPaymentMethod = async (req: Request, res: Response) => {
16
+ try {
17
+ const response = await publicOrganizationController.setupPaymentMethod(req[`environment`] as Environment);
18
+ res.json(response);
19
+ } catch (e) {
20
+ if (e instanceof APIError) {
21
+ res.status(e.code).json(e.data);
22
+ } else {
23
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
24
+ }
25
+ }
26
+ };
27
+
28
+ private readonly getPaymentMethod = async (req: Request, res: Response) => {
29
+ try {
30
+ const response = await publicOrganizationController.getPaymentMethod(req[`environment`] as Environment, req.params[`uuid`]);
31
+ res.json(response);
32
+ } catch (e) {
33
+ if (e instanceof APIError) {
34
+ res.status(e.code).json(e.data);
35
+ } else {
36
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
37
+ }
38
+ }
39
+ };
40
+
41
+ private readonly listPaymentMethods = async (req: Request, res: Response) => {
42
+ try {
43
+ const response = await publicOrganizationController.listPaymentMethods(req[`environment`] as Environment);
44
+ res.json(response);
45
+ } catch (e) {
46
+ if (e instanceof APIError) {
47
+ res.status(e.code).json(e.data);
48
+ } else {
49
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
50
+ }
51
+ }
52
+ };
53
+
54
+ private readonly addPaymentMethod = async (req: Request, res: Response) => {
55
+ try {
56
+ const response = await publicOrganizationController.addPaymentMethod(req[`environment`] as Environment, req.body);
57
+ res.json(response);
58
+ } catch (e) {
59
+ if (e instanceof APIError) {
60
+ res.status(e.code).json(e.data);
61
+ } else {
62
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
63
+ }
64
+ }
65
+ };
66
+
67
+ private readonly updatePaymentMethod = async (req: Request, res: Response) => {
68
+ try {
69
+ const response = await publicOrganizationController.updatePaymentMethod(req[`environment`] as Environment, req.params[`uuid`], req.body);
70
+ res.json(response);
71
+ } catch (e) {
72
+ if (e instanceof APIError) {
73
+ res.status(e.code).json(e.data);
74
+ } else {
75
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
76
+ }
77
+ }
78
+ };
79
+
80
+ private readonly deletePaymentMethod = async (req: Request, res: Response) => {
81
+ try {
82
+ const response = await publicOrganizationController.deletePaymentMethod(req[`environment`] as Environment, req.params[`uuid`]);
83
+ res.json(response);
84
+ } catch (e) {
85
+ if (e instanceof APIError) {
86
+ res.status(e.code).json(e.data);
87
+ } else {
88
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
89
+ }
90
+ }
91
+ };
92
+
93
+ private readonly init = () => {
94
+ this.router.post(`/setup`, this.setupPaymentMethod);
95
+ this.router.post(`/list`, this.listPaymentMethods);
96
+ this.router.get(`/:uuid`, this.getPaymentMethod);
97
+ this.router.post(`/`, this.addPaymentMethod);
98
+ this.router.patch(`/:uuid`, this.updatePaymentMethod);
99
+ this.router.delete(`/:uuid`, this.deletePaymentMethod);
100
+ };
101
+ }
102
+
103
+ const paymentMethodRouter = (new PaymentMethodRouter()).router;
104
+ export default paymentMethodRouter;
@@ -0,0 +1,319 @@
1
+ import { Request, Response, Router } from 'express';
2
+ import httpcodes, { StatusCodes } from 'http-status-codes';
3
+ import APIError from '../../../helpers/api-error';
4
+ import publicOrganizationController from '../../controllers/organizations/public-organization-controller';
5
+ import identityTokenMiddleware from '../../middlewares/auth/identity-token-middleware';
6
+ import organizationTokenMiddleware from '../../middlewares/auth/organization-token-middleware';
7
+ import authorityMiddleware from '../../middlewares/authority-middleware';
8
+ import organizationMiddleware from '../../middlewares/organizations/organization-middleware';
9
+ import userMiddleware from '../../middlewares/users/user-middleware';
10
+ import { User } from '../../types/admin/user-types';
11
+ import { Environment } from '../../types/auth/organization-token-types';
12
+ import usersRouter from './users-router';
13
+ import paymentMethodRouter from './payments-router';
14
+ import invoiceRouter from './invoices-router';
15
+
16
+ class PublicOrganizationRouter {
17
+ router: Router;
18
+
19
+ constructor() {
20
+ this.router = Router();
21
+ this.init();
22
+ }
23
+
24
+ private readonly create = async (req: Request, res: Response) => {
25
+ try {
26
+ const response = await publicOrganizationController.create(req[`user`] as User, req.body);
27
+ res.json(response);
28
+ } catch (e) {
29
+ if (e instanceof APIError) {
30
+ res.status(e.code).json(e.data);
31
+ } else {
32
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
33
+ }
34
+ }
35
+ };
36
+
37
+ private readonly authority = async (req: Request, res: Response) => {
38
+ try {
39
+ const response = await publicOrganizationController.authority(req.params[`uuid`]);
40
+ res.json(response);
41
+ } catch (e) {
42
+ if (e instanceof APIError) {
43
+ res.status(e.code).json(e.data);
44
+ } else {
45
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
46
+ }
47
+ }
48
+ };
49
+
50
+ private readonly get = async (req: Request, res: Response) => {
51
+ try {
52
+ const response = await publicOrganizationController.get(req[`environment`] as Environment);
53
+ res.json(response);
54
+ } catch (e) {
55
+ if (e instanceof APIError) {
56
+ res.status(e.code).json(e.data);
57
+ } else {
58
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
59
+ }
60
+ }
61
+ };
62
+
63
+ private readonly update = async (req: Request, res: Response) => {
64
+ try {
65
+ const response = await publicOrganizationController.update((req[`environment`] as Environment), req.body);
66
+ res.json(response);
67
+ } catch (e) {
68
+ if (e instanceof APIError) {
69
+ res.status(e.code).json(e.data);
70
+ } else {
71
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
72
+ }
73
+ }
74
+ };
75
+
76
+ private readonly delete = async (req: Request, res: Response) => {
77
+ try {
78
+ await publicOrganizationController.delete((req[`environment`] as Environment), `force` in req.query);
79
+ res.status(StatusCodes.NO_CONTENT).json();
80
+ } catch (e) {
81
+ if (e instanceof APIError) {
82
+ res.status(e.code).json(e.data);
83
+ } else {
84
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
85
+ }
86
+ }
87
+ };
88
+
89
+ private readonly ownership = async (req: Request, res: Response) => {
90
+ try {
91
+ await publicOrganizationController.ownership(req[`environment`] as Environment, req.body);
92
+ res.status(StatusCodes.NO_CONTENT).json();
93
+ } catch (e) {
94
+ if (e instanceof APIError) {
95
+ res.status(e.code).json(e.data);
96
+ } else {
97
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
98
+ }
99
+ }
100
+ };
101
+
102
+ private readonly getServiceLicenses = async (req: Request, res: Response) => {
103
+ try {
104
+ const response = await publicOrganizationController.getServiceLicenses(req[`environment`] as Environment, req.params[`service`], req.query);
105
+ res.json(response);
106
+ } catch (e) {
107
+ if (e instanceof APIError) {
108
+ res.status(e.code).json(e.data);
109
+ } else {
110
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
111
+ }
112
+ }
113
+ };
114
+
115
+ private readonly activateServiceLicense = async (req: Request, res: Response) => {
116
+ try {
117
+ const response = await publicOrganizationController.activateServiceLicense(req[`environment`] as Environment, req.params[`service`], req.body);
118
+ res.json(response);
119
+ } catch (e) {
120
+ console.log(e);
121
+ if (e instanceof APIError) {
122
+ res.status(e.code).json(e.data);
123
+ } else {
124
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
125
+ }
126
+ }
127
+ };
128
+
129
+ private readonly renewServiceLicense = async (req: Request, res: Response) => {
130
+ try {
131
+ const response = await publicOrganizationController.renewServiceLicense(req[`environment`] as Environment, req.params[`service`], req.params[`uuid`]);
132
+ res.status(response ? StatusCodes.OK : StatusCodes.NO_CONTENT).json(response);
133
+ } catch (e) {
134
+ if (e instanceof APIError) {
135
+ res.status(e.code).json(e.data);
136
+ } else {
137
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
138
+ }
139
+ }
140
+ };
141
+
142
+ private readonly cancelServiceLicense = async (req: Request, res: Response) => {
143
+ try {
144
+ const response = await publicOrganizationController.cancelServiceLicense(req[`environment`] as Environment, req.params[`service`], req.params[`uuid`]);
145
+ res.status(response ? StatusCodes.OK : StatusCodes.NO_CONTENT).json(response);
146
+ } catch (e) {
147
+ if (e instanceof APIError) {
148
+ res.status(e.code).json(e.data);
149
+ } else {
150
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
151
+ }
152
+ }
153
+ };
154
+
155
+ private readonly getServicePowerUps = async (req: Request, res: Response) => {
156
+ try {
157
+ const response = await publicOrganizationController.getServicePowerUps(req[`environment`] as Environment, req.params[`service`], req.query);
158
+ res.json(response);
159
+ } catch (e) {
160
+ if (e instanceof APIError) {
161
+ res.status(e.code).json(e.data);
162
+ } else {
163
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
164
+ }
165
+ }
166
+ };
167
+
168
+ private readonly activateServicePowerUp = async (req: Request, res: Response) => {
169
+ try {
170
+ const response = await publicOrganizationController.activateServicePowerUp(req[`environment`] as Environment, req.params[`service`], req.body);
171
+ res.json(response);
172
+ } catch (e) {
173
+ if (e instanceof APIError) {
174
+ res.status(e.code).json(e.data);
175
+ } else {
176
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
177
+ }
178
+ }
179
+ };
180
+
181
+ private readonly cancelServicePowerUp = async (req: Request, res: Response) => {
182
+ try {
183
+ const response = await publicOrganizationController.cancelServicePowerUp(req[`environment`] as Environment, req.params[`service`], req.params[`uuid`]);
184
+ res.status(response ? StatusCodes.OK : StatusCodes.NO_CONTENT).json(response);
185
+ } catch (e) {
186
+ if (e instanceof APIError) {
187
+ res.status(e.code).json(e.data);
188
+ } else {
189
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
190
+ }
191
+ }
192
+ };
193
+
194
+ private readonly endTrial = async (req: Request, res: Response) => {
195
+ try {
196
+ await publicOrganizationController.endTrial(req[`environment`] as Environment);
197
+ res.status(StatusCodes.NO_CONTENT).json();
198
+ } catch (e) {
199
+ if (e instanceof APIError) {
200
+ res.status(e.code).json(e.data);
201
+ } else {
202
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
203
+ }
204
+ }
205
+ };
206
+
207
+ private readonly getServices = async (req: Request, res: Response) => {
208
+ try {
209
+ const response = await publicOrganizationController.getServices(req[`environment`]);
210
+ res.json(response);
211
+ } catch (e) {
212
+ if (e instanceof APIError) {
213
+ res.status(e.code).json(e.data);
214
+ } else {
215
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
216
+ }
217
+ }
218
+ };
219
+
220
+ private readonly getServiceQuota = async (req: Request, res: Response) => {
221
+ try {
222
+ const response = await publicOrganizationController.getServiceQuota(req[`environment`] as Environment, req.params[`service`], req.query);
223
+ res.json(response);
224
+ } catch (e) {
225
+ if (e instanceof APIError) {
226
+ res.status(e.code).json(e.data);
227
+ } else {
228
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
229
+ }
230
+ }
231
+ };
232
+
233
+ private readonly checkServiceQuota = async (req: Request, res: Response) => {
234
+ try {
235
+ const response = await publicOrganizationController.checkServiceQuota(req[`environment`] as Environment, req.params[`service`]);
236
+ res.json(response);
237
+ } catch (e) {
238
+ if (e instanceof APIError) {
239
+ res.status(e.code).json(e.data);
240
+ } else {
241
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
242
+ }
243
+ }
244
+ };
245
+
246
+ private readonly updateOverconsume = async (req: Request, res: Response) => {
247
+ try {
248
+ const response = await publicOrganizationController.updateOverconsume(req[`environment`] as Environment, req.params[`service`], req.body);
249
+ res.json(response);
250
+ } catch (e) {
251
+ if (e instanceof APIError) {
252
+ res.status(e.code).json(e.data);
253
+ } else {
254
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
255
+ }
256
+ }
257
+ };
258
+
259
+ private readonly information = async (req: Request, res: Response) => {
260
+ try {
261
+ const response = await publicOrganizationController.getInformation(req[`environment`] as Environment);
262
+ res.json(response);
263
+ } catch (e) {
264
+ if (e instanceof APIError) {
265
+ res.status(e.code).json(e.data);
266
+ } else {
267
+ res.status(httpcodes.INTERNAL_SERVER_ERROR).json({ error: e.message || e });
268
+ }
269
+ }
270
+ };
271
+
272
+ private readonly init = () => {
273
+ this.router.post(`/`, identityTokenMiddleware.exists, identityTokenMiddleware.validate, userMiddleware.active, this.create);
274
+
275
+ this.router.get(`/authority/:uuid`, authorityMiddleware.service, this.authority);
276
+
277
+ this.router.use(organizationTokenMiddleware.exists, organizationTokenMiddleware.validate);
278
+
279
+ this.router.get(`/`, this.get);
280
+
281
+ this.router.use(`/users`, usersRouter);
282
+
283
+ // read-only information public for all organization members
284
+ this.router.get(`/services`, this.getServices);
285
+ this.router.get(`/services/:service/licenses`, this.getServiceLicenses);
286
+ this.router.get(`/services/:service/power-ups`, this.getServicePowerUps);
287
+ this.router.get(`/services/:service/quota`, this.getServiceQuota);
288
+
289
+ this.router.use(organizationMiddleware.admin);
290
+
291
+ this.router.patch(`/`, this.update);
292
+
293
+ this.router.use(organizationMiddleware.active);
294
+
295
+ this.router.delete(`/trial`, this.endTrial);
296
+
297
+ this.router.patch(`/services/:service/overconsume`, this.updateOverconsume);
298
+ this.router.patch(`/services/:service/quota`, this.checkServiceQuota);
299
+
300
+ this.router.post(`/services/:service/licenses`, this.activateServiceLicense);
301
+ this.router.post(`/services/:service/licenses/:uuid/renew`, this.renewServiceLicense);
302
+ this.router.delete(`/services/:service/licenses/:uuid`, this.cancelServiceLicense);
303
+
304
+ this.router.post(`/services/:service/power-ups`, this.activateServicePowerUp);
305
+ this.router.delete(`/services/:service/power-ups/:uuid`, this.cancelServicePowerUp);
306
+
307
+ this.router.use(`/payments`, paymentMethodRouter);
308
+ this.router.use(`/invoices`, invoiceRouter);
309
+
310
+ this.router.use(organizationMiddleware.owner);
311
+
312
+ this.router.delete(`/`, this.delete);
313
+ this.router.patch(`/ownership`, this.ownership);
314
+ this.router.get(`/information`, this.information);
315
+ };
316
+ }
317
+
318
+ const publicOrganizationRouter = (new PublicOrganizationRouter()).router;
319
+ export default publicOrganizationRouter;