@appsemble/node-utils 0.35.11-test.3 → 0.35.14
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.
- package/README.md +3 -3
- package/createUser.d.ts +10 -0
- package/createUser.js +50 -0
- package/deleteUser.d.ts +6 -0
- package/deleteUser.js +23 -0
- package/index.d.ts +3 -0
- package/index.js +3 -0
- package/package.json +10 -9
- package/updateSubscription.d.ts +8 -0
- package/updateSubscription.js +29 -0
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
#  Appsemble Node Utilities
|
|
2
2
|
|
|
3
3
|
> NodeJS utilities used by Appsemble internally.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@appsemble/node-utils)
|
|
6
|
-
[](https://gitlab.com/appsemble/appsemble/-/releases/0.35.14)
|
|
7
7
|
[](https://prettier.io)
|
|
8
8
|
|
|
9
9
|
## Table of Contents
|
|
@@ -26,5 +26,5 @@ compatibility is not guaranteed.
|
|
|
26
26
|
|
|
27
27
|
## License
|
|
28
28
|
|
|
29
|
-
[LGPL-3.0-only](https://gitlab.com/appsemble/appsemble/-/blob/0.35.
|
|
29
|
+
[LGPL-3.0-only](https://gitlab.com/appsemble/appsemble/-/blob/0.35.14/LICENSE.md) ©
|
|
30
30
|
[Appsemble](https://appsemble.com)
|
package/createUser.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new user in the database
|
|
3
|
+
*
|
|
4
|
+
* @param name Name of the user
|
|
5
|
+
* @param email Primary email of the user
|
|
6
|
+
* @param password Password
|
|
7
|
+
* @param timezone Timezone of the user
|
|
8
|
+
* @param clientCredentials Used to make OAuth client credentials for permissions
|
|
9
|
+
*/
|
|
10
|
+
export declare function createUser(name: string, email: string, password: string, timezone?: string, clientCredentials?: string): Promise<void>;
|
package/createUser.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { hash } from 'bcrypt';
|
|
2
|
+
import { Client } from 'pg';
|
|
3
|
+
import { v4 as uuid } from 'uuid';
|
|
4
|
+
const { DATABASE_HOST = 'localhost', DATABASE_NAME = 'appsemble', DATABASE_PASSWORD = 'password', DATABASE_PORT = 5432, DATABASE_USER = 'admin', } = process.env;
|
|
5
|
+
async function insert(table, columns, values, client) {
|
|
6
|
+
const params = columns.map((item, index) => `$${index + 1}`);
|
|
7
|
+
const query = `INSERT INTO "${table}" (${columns.join(', ')}) VALUES (${params.join(', ')})`;
|
|
8
|
+
const result = await client.query(query, values);
|
|
9
|
+
return result;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new user in the database
|
|
13
|
+
*
|
|
14
|
+
* @param name Name of the user
|
|
15
|
+
* @param email Primary email of the user
|
|
16
|
+
* @param password Password
|
|
17
|
+
* @param timezone Timezone of the user
|
|
18
|
+
* @param clientCredentials Used to make OAuth client credentials for permissions
|
|
19
|
+
*/
|
|
20
|
+
export async function createUser(name, email, password, timezone, clientCredentials) {
|
|
21
|
+
if (!DATABASE_HOST || !DATABASE_NAME || !DATABASE_PASSWORD || !DATABASE_PORT || !DATABASE_USER) {
|
|
22
|
+
throw new Error('Missing database credentials');
|
|
23
|
+
}
|
|
24
|
+
const client = new Client({
|
|
25
|
+
database: DATABASE_NAME,
|
|
26
|
+
host: DATABASE_HOST,
|
|
27
|
+
port: DATABASE_PORT ? Number(DATABASE_PORT) : undefined,
|
|
28
|
+
user: DATABASE_USER,
|
|
29
|
+
password: DATABASE_PASSWORD,
|
|
30
|
+
});
|
|
31
|
+
await client.connect();
|
|
32
|
+
const hashedPassword = await hash(password, 10);
|
|
33
|
+
const userId = uuid();
|
|
34
|
+
await insert('User', ['id', 'name', '"primaryEmail"', 'password', 'timezone', 'created', 'updated'], [userId, name, email, hashedPassword, timezone ?? 'Europe/Amsterdam', 'NOW()', 'NOW()'], client);
|
|
35
|
+
await insert('EmailAuthorization', ['email', 'verified', 'created', 'updated', '"UserId"'], [email, true, 'NOW()', 'NOW()', userId], client);
|
|
36
|
+
if (clientCredentials) {
|
|
37
|
+
const [clientId, clientPassword] = clientCredentials.split(':');
|
|
38
|
+
const hashedClientPassword = await hash(clientPassword, 10);
|
|
39
|
+
await insert('OAuth2ClientCredentials', ['id', 'secret', 'description', 'scopes', 'created', '"UserId"'], [
|
|
40
|
+
clientId,
|
|
41
|
+
hashedClientPassword,
|
|
42
|
+
'Used for provisioning the review environment',
|
|
43
|
+
'apps:write resources:write assets:write blocks:write organizations:write groups:write',
|
|
44
|
+
'NOW()',
|
|
45
|
+
userId,
|
|
46
|
+
], client);
|
|
47
|
+
}
|
|
48
|
+
await client.end();
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=createUser.js.map
|
package/deleteUser.d.ts
ADDED
package/deleteUser.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Client } from 'pg';
|
|
2
|
+
const { DATABASE_HOST = 'localhost', DATABASE_NAME = 'appsemble', DATABASE_PASSWORD = 'password', DATABASE_PORT = 5432, DATABASE_USER = 'admin', } = process.env;
|
|
3
|
+
/**
|
|
4
|
+
* Deletes user from the database
|
|
5
|
+
*
|
|
6
|
+
* @param email Primary email of the user to delete
|
|
7
|
+
*/
|
|
8
|
+
export async function deleteUser(email) {
|
|
9
|
+
if (!DATABASE_HOST || !DATABASE_NAME || !DATABASE_PASSWORD || !DATABASE_PORT || !DATABASE_USER) {
|
|
10
|
+
throw new Error('Missing database credentials');
|
|
11
|
+
}
|
|
12
|
+
const client = new Client({
|
|
13
|
+
database: DATABASE_NAME,
|
|
14
|
+
host: DATABASE_HOST,
|
|
15
|
+
port: DATABASE_PORT ? Number(DATABASE_PORT) : undefined,
|
|
16
|
+
user: DATABASE_USER,
|
|
17
|
+
password: DATABASE_PASSWORD,
|
|
18
|
+
});
|
|
19
|
+
await client.connect();
|
|
20
|
+
await client.query(`DELETE FROM "User" WHERE "primaryEmail" = '${email}'`);
|
|
21
|
+
await client.end();
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=deleteUser.js.map
|
package/index.d.ts
CHANGED
|
@@ -42,3 +42,6 @@ export * from './uploads.js';
|
|
|
42
42
|
export * from './assets.js';
|
|
43
43
|
export * from './getValidTrainings.js';
|
|
44
44
|
export * from './PhoneNumberValidationError.js';
|
|
45
|
+
export * from './createUser.js';
|
|
46
|
+
export * from './deleteUser.js';
|
|
47
|
+
export * from './updateSubscription.js';
|
package/index.js
CHANGED
|
@@ -42,4 +42,7 @@ export * from './uploads.js';
|
|
|
42
42
|
export * from './assets.js';
|
|
43
43
|
export * from './getValidTrainings.js';
|
|
44
44
|
export * from './PhoneNumberValidationError.js';
|
|
45
|
+
export * from './createUser.js';
|
|
46
|
+
export * from './deleteUser.js';
|
|
47
|
+
export * from './updateSubscription.js';
|
|
45
48
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@appsemble/node-utils",
|
|
3
|
-
"version": "0.35.
|
|
3
|
+
"version": "0.35.14",
|
|
4
4
|
"description": "NodeJS utilities used by Appsemble internally.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"app",
|
|
@@ -40,15 +40,16 @@
|
|
|
40
40
|
"test": "vitest"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@appsemble/
|
|
44
|
-
"@appsemble/
|
|
45
|
-
"@appsemble/
|
|
43
|
+
"@appsemble/lang-sdk": "0.35.14",
|
|
44
|
+
"@appsemble/types": "0.35.14",
|
|
45
|
+
"@appsemble/utils": "0.35.14",
|
|
46
46
|
"@formatjs/fast-memoize": "^2.0.0",
|
|
47
47
|
"@fortawesome/fontawesome-free": "^6.0.0",
|
|
48
48
|
"@kubernetes/client-node": "^0.22.2",
|
|
49
49
|
"@odata/parser": "^0.2.0",
|
|
50
50
|
"@types/koa": "^2.0.0",
|
|
51
51
|
"axios": "^1.0.0",
|
|
52
|
+
"bcrypt": "^5.1.1",
|
|
52
53
|
"bulma": "=0.9.3",
|
|
53
54
|
"bulma-checkradio": "=2.1.1",
|
|
54
55
|
"bulma-slider": "=2.0.4",
|
|
@@ -61,7 +62,7 @@
|
|
|
61
62
|
"date-fns": "^2.0.0",
|
|
62
63
|
"express-to-koa": "^2.0.0",
|
|
63
64
|
"fast-glob": "^3.0.0",
|
|
64
|
-
"form-data": "^4.0.
|
|
65
|
+
"form-data": "^4.0.4",
|
|
65
66
|
"fs-extra": "^11.0.0",
|
|
66
67
|
"inquirer": "^9.0.0",
|
|
67
68
|
"intl-messageformat": "^10.0.0",
|
|
@@ -69,18 +70,15 @@
|
|
|
69
70
|
"keytar": "^7.0.0",
|
|
70
71
|
"koa": "^2.0.0",
|
|
71
72
|
"koa-compose": "^4.0.0",
|
|
72
|
-
"koa-compress": "^5.
|
|
73
|
+
"koa-compress": "^5.1.1",
|
|
73
74
|
"koa-mount": "^4.0.0",
|
|
74
75
|
"koa-range": "^0.3.0",
|
|
75
76
|
"koa-static": "^5.0.0",
|
|
76
77
|
"koas-body-parser": "^0.7.0",
|
|
77
78
|
"koas-core": "^0.7.0",
|
|
78
|
-
"koas-operations": "^0.7.0",
|
|
79
79
|
"koas-parameters": "^0.7.0",
|
|
80
80
|
"koas-security": "^0.7.0",
|
|
81
81
|
"koas-serializer": "^0.7.0",
|
|
82
|
-
"koas-status-code": "^0.7.0",
|
|
83
|
-
"koas-swagger-ui": "^0.7.0",
|
|
84
82
|
"language-tags": "^1.0.0",
|
|
85
83
|
"lodash": "^4.0.0",
|
|
86
84
|
"lodash-es": "^4.0.0",
|
|
@@ -92,12 +90,14 @@
|
|
|
92
90
|
"openapi-types": "^12.1.3",
|
|
93
91
|
"parse-duration": "^1.0.0",
|
|
94
92
|
"parse-json": "^8.0.0",
|
|
93
|
+
"pg": "^8.16.3",
|
|
95
94
|
"sass": "^1.0.0",
|
|
96
95
|
"sharp": "^0.32.0",
|
|
97
96
|
"sort-keys": "^5.0.0",
|
|
98
97
|
"strip-bom": "^5.0.0",
|
|
99
98
|
"type-fest": "^4.0.0",
|
|
100
99
|
"type-is": "^1.6.18",
|
|
100
|
+
"uuid": "^8.3.2",
|
|
101
101
|
"webpack": "^5.0.0",
|
|
102
102
|
"webpack-dev-middleware": "^6.0.0",
|
|
103
103
|
"winston": "^3.0.0",
|
|
@@ -110,6 +110,7 @@
|
|
|
110
110
|
"@types/koa-compress": "4.0.6",
|
|
111
111
|
"@types/koa-range": "0.3.5",
|
|
112
112
|
"@types/type-is": "1.6.7",
|
|
113
|
+
"@types/uuid": "10.0.0",
|
|
113
114
|
"axios-mock-adapter": "1.22.0",
|
|
114
115
|
"axios-test-instance": "7.0.0",
|
|
115
116
|
"vitest": "2.1.9"
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type SubscriptionPlanType } from '@appsemble/types';
|
|
2
|
+
/**
|
|
3
|
+
* Updates the organization's subscription to the given plan
|
|
4
|
+
*
|
|
5
|
+
* @param organizationId ID of the organization to assign the subscription to
|
|
6
|
+
* @param subscriptionPlan Type of subscription plan
|
|
7
|
+
*/
|
|
8
|
+
export declare function updateSubscription(organizationId: string, subscriptionPlan: SubscriptionPlanType): Promise<void>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Client } from 'pg';
|
|
2
|
+
const { DATABASE_HOST = 'localhost', DATABASE_NAME = 'appsemble', DATABASE_PASSWORD = 'password', DATABASE_PORT = 5432, DATABASE_USER = 'admin', } = process.env;
|
|
3
|
+
async function update(organizationId, subscriptionPlan, client) {
|
|
4
|
+
const expirationDate = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000);
|
|
5
|
+
const query = 'UPDATE "OrganizationSubscription" SET "expirationDate"=$1, "subscriptionPlan"=$2, "cancelled"=false WHERE "OrganizationId"=$3';
|
|
6
|
+
await client.query(query, [expirationDate, subscriptionPlan, organizationId]);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Updates the organization's subscription to the given plan
|
|
10
|
+
*
|
|
11
|
+
* @param organizationId ID of the organization to assign the subscription to
|
|
12
|
+
* @param subscriptionPlan Type of subscription plan
|
|
13
|
+
*/
|
|
14
|
+
export async function updateSubscription(organizationId, subscriptionPlan) {
|
|
15
|
+
if (!DATABASE_HOST || !DATABASE_NAME || !DATABASE_PASSWORD || !DATABASE_PORT || !DATABASE_USER) {
|
|
16
|
+
throw new Error('Missing database credentials');
|
|
17
|
+
}
|
|
18
|
+
const client = new Client({
|
|
19
|
+
database: DATABASE_NAME,
|
|
20
|
+
host: DATABASE_HOST,
|
|
21
|
+
port: DATABASE_PORT ? Number(DATABASE_PORT) : undefined,
|
|
22
|
+
user: DATABASE_USER,
|
|
23
|
+
password: DATABASE_PASSWORD,
|
|
24
|
+
});
|
|
25
|
+
await client.connect();
|
|
26
|
+
await update(organizationId, subscriptionPlan, client);
|
|
27
|
+
await client.end();
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=updateSubscription.js.map
|