@flowerforce/flowerbase 1.0.3-beta.2 → 1.0.3-beta.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 (65) hide show
  1. package/dist/auth/plugins/jwt.d.ts.map +1 -1
  2. package/dist/auth/plugins/jwt.js +2 -1
  3. package/dist/auth/providers/local-userpass/controller.d.ts.map +1 -1
  4. package/dist/auth/providers/local-userpass/controller.js +31 -59
  5. package/dist/constants.d.ts +4 -0
  6. package/dist/constants.d.ts.map +1 -1
  7. package/dist/constants.js +5 -1
  8. package/dist/features/endpoints/utils.d.ts.map +1 -1
  9. package/dist/features/endpoints/utils.js +23 -4
  10. package/dist/features/functions/queue.d.ts +7 -0
  11. package/dist/features/functions/queue.d.ts.map +1 -0
  12. package/dist/features/functions/queue.js +69 -0
  13. package/dist/features/triggers/utils.d.ts.map +1 -1
  14. package/dist/features/triggers/utils.js +22 -6
  15. package/dist/services/auth/index.d.ts +4 -0
  16. package/dist/services/auth/index.d.ts.map +1 -0
  17. package/dist/services/auth/index.js +14 -0
  18. package/dist/services/auth/model.d.ts +12 -0
  19. package/dist/services/auth/model.d.ts.map +1 -0
  20. package/dist/services/auth/model.js +2 -0
  21. package/dist/services/aws/index.d.ts.map +1 -1
  22. package/dist/services/aws/index.js +7 -10
  23. package/dist/services/index.d.ts +1 -0
  24. package/dist/services/index.d.ts.map +1 -1
  25. package/dist/services/index.js +2 -0
  26. package/dist/services/mongodb-atlas/index.d.ts.map +1 -1
  27. package/dist/services/mongodb-atlas/index.js +28 -5
  28. package/dist/services/mongodb-atlas/utils.d.ts +17 -1
  29. package/dist/services/mongodb-atlas/utils.d.ts.map +1 -1
  30. package/dist/services/mongodb-atlas/utils.js +24 -11
  31. package/dist/shared/handleUserRegistration.d.ts +11 -0
  32. package/dist/shared/handleUserRegistration.d.ts.map +1 -0
  33. package/dist/shared/handleUserRegistration.js +62 -0
  34. package/dist/shared/models/handleUserRegistration.model.d.ts +16 -0
  35. package/dist/shared/models/handleUserRegistration.model.d.ts.map +1 -0
  36. package/dist/shared/models/handleUserRegistration.model.js +2 -0
  37. package/dist/state.d.ts.map +1 -1
  38. package/dist/state.js +3 -1
  39. package/dist/utils/context/helpers.d.ts +4 -0
  40. package/dist/utils/context/helpers.d.ts.map +1 -1
  41. package/dist/utils/context/index.d.ts +1 -1
  42. package/dist/utils/context/index.d.ts.map +1 -1
  43. package/dist/utils/context/index.js +30 -22
  44. package/dist/utils/context/interface.d.ts +2 -0
  45. package/dist/utils/context/interface.d.ts.map +1 -1
  46. package/dist/utils/roles/helpers.js +1 -1
  47. package/package.json +1 -1
  48. package/src/auth/plugins/jwt.ts +4 -2
  49. package/src/auth/providers/local-userpass/controller.ts +45 -71
  50. package/src/constants.ts +7 -0
  51. package/src/features/endpoints/utils.ts +26 -6
  52. package/src/features/functions/queue.ts +48 -0
  53. package/src/features/triggers/utils.ts +11 -6
  54. package/src/services/auth/index.ts +12 -0
  55. package/src/services/auth/model.ts +13 -0
  56. package/src/services/aws/index.ts +10 -11
  57. package/src/services/index.ts +2 -0
  58. package/src/services/mongodb-atlas/index.ts +61 -26
  59. package/src/services/mongodb-atlas/utils.ts +98 -59
  60. package/src/shared/handleUserRegistration.ts +64 -0
  61. package/src/shared/models/handleUserRegistration.model.ts +20 -0
  62. package/src/state.ts +4 -1
  63. package/src/utils/context/index.ts +44 -29
  64. package/src/utils/context/interface.ts +2 -0
  65. package/src/utils/roles/helpers.ts +1 -1
@@ -0,0 +1,64 @@
1
+ import { PROVIDER_TYPE } from "../auth/utils"
2
+ import { AUTH_CONFIG, DB_NAME } from "../constants"
3
+ import { hashPassword } from "../utils/crypto"
4
+ import { HandleUserRegistration } from "./models/handleUserRegistration.model"
5
+
6
+ /**
7
+ * Register user
8
+ *
9
+ * @param {FastifyInstance} app The Fastify instance.
10
+ * @param {Object} [opt] The options from the context
11
+ * @returns {Promise<InsertOneResult<Document>>} A promise resolving to the result of the insert operation.
12
+ */
13
+ const handleUserRegistration: HandleUserRegistration = (app, opt) => async ({ email, password }) => {
14
+ const { run_as_system } = opt ?? {}
15
+
16
+ if (!run_as_system) {
17
+ throw new Error('only run_as_system')
18
+ }
19
+
20
+ const { authCollection } = AUTH_CONFIG
21
+ const db = app?.mongo.client.db(DB_NAME)
22
+ const hashedPassword = await hashPassword(password)
23
+
24
+ const existingUser = await db?.collection(authCollection!).findOne({ email })
25
+ if (existingUser) {
26
+ throw new Error('This email address is already used')
27
+ }
28
+
29
+ const result = await db?.collection(authCollection!).insertOne({
30
+ email,
31
+ password: hashedPassword,
32
+ status: 'pending', // confirmed
33
+ custom_data: {
34
+ // TODO: aggiungere dati personalizzati alla registrazione
35
+ },
36
+ identities: [
37
+ {
38
+ provider_type: PROVIDER_TYPE,
39
+ provider_data: { email }
40
+ }
41
+ ]
42
+ })
43
+
44
+ await db?.collection(authCollection!).updateOne(
45
+ { email },
46
+ {
47
+ $set: {
48
+ identities: [
49
+ {
50
+ id: result?.insertedId.toString(),
51
+ provider_id: result?.insertedId.toString(),
52
+ provider_type: PROVIDER_TYPE,
53
+ provider_data: { email }
54
+ }
55
+ ]
56
+ }
57
+ }
58
+ )
59
+
60
+ return result
61
+
62
+ }
63
+
64
+ export default handleUserRegistration
@@ -0,0 +1,20 @@
1
+ import { FastifyInstance } from "fastify/types/instance"
2
+ import { InsertOneResult } from "mongodb/mongodb"
3
+ import { User } from "../../auth/dtos"
4
+ import { Rules } from "../../features/rules/interface"
5
+
6
+ type RegistrationParams = {
7
+ email: string
8
+ password: string
9
+ }
10
+
11
+ export type Options = {
12
+ user?: User
13
+ rules?: Rules
14
+ run_as_system?: boolean
15
+ }
16
+
17
+ export type HandleUserRegistration = (
18
+ app: FastifyInstance,
19
+ opt: Options
20
+ ) => (params: RegistrationParams) => Promise<InsertOneResult<Document>>
package/src/state.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { FastifyInstance } from 'fastify'
2
2
  import { Endpoints } from './features/endpoints/interface'
3
3
  import { Functions } from './features/functions/interface'
4
+ import { FunctionsQueue } from './features/functions/queue'
4
5
  import { Rules } from './features/rules/interface'
5
6
  import { Triggers } from './features/triggers/interface'
6
7
  import { Services } from './services/interface'
@@ -12,6 +13,7 @@ type State = {
12
13
  endpoints: Endpoints
13
14
  rules: Rules
14
15
  app?: FastifyInstance
16
+ functionsQueue: FunctionsQueue
15
17
  }
16
18
 
17
19
  export class StateManager {
@@ -19,7 +21,8 @@ export class StateManager {
19
21
  functions: {},
20
22
  triggers: [],
21
23
  endpoints: [],
22
- rules: {}
24
+ rules: {},
25
+ functionsQueue: new FunctionsQueue()
23
26
  }
24
27
  static select<K extends keyof typeof this._state>(
25
28
  key: K
@@ -1,6 +1,7 @@
1
1
  import { createRequire } from 'node:module'
2
2
  import vm from 'vm'
3
3
  import { EJSON } from 'bson'
4
+ import { StateManager } from '../../state'
4
5
  import { generateContextData } from './helpers'
5
6
  import { GenerateContextParams } from './interface'
6
7
 
@@ -23,35 +24,49 @@ export async function GenerateContext({
23
24
  currentFunction,
24
25
  functionsList,
25
26
  services,
26
- runAsSystem
27
+ runAsSystem,
28
+ deserializeArgs = true,
29
+ enqueue
27
30
  }: GenerateContextParams) {
28
- const contextFunction = { run_as_system: runAsSystem, ...currentFunction }
29
- const contextData = generateContextData({
30
- user,
31
- services,
32
- app,
33
- rules,
34
- currentFunction: contextFunction,
35
- functionsList,
36
- GenerateContext,
37
- runAsSystem
38
- })
39
-
40
- try {
41
- const entryFile = require.main?.filename ?? process.cwd();
42
- const customRequire = createRequire(entryFile);
43
-
44
- vm.runInContext(contextFunction.code, vm.createContext({
45
- ...contextData, require: customRequire,
46
- exports,
47
- module,
48
- __filename: __filename,
49
- __dirname: __dirname
50
- }));
51
- }
52
- catch (e) {
53
- console.log(e)
54
- }
55
31
 
56
- return await module.exports(...EJSON.deserialize(args))
32
+ const functionsQueue = StateManager.select("functionsQueue")
33
+
34
+ const functionToRun = { run_as_system: runAsSystem, ...currentFunction }
35
+
36
+ const run = async () => {
37
+
38
+ const contextData = generateContextData({
39
+ user,
40
+ services,
41
+ app,
42
+ rules,
43
+ currentFunction: functionToRun,
44
+ functionsList,
45
+ GenerateContext
46
+ })
47
+
48
+ try {
49
+ const entryFile = require.main?.filename ?? process.cwd();
50
+ const customRequire = createRequire(entryFile);
51
+
52
+ vm.runInContext(functionToRun.code, vm.createContext({
53
+ ...contextData, require: customRequire,
54
+ exports,
55
+ module,
56
+ __filename: __filename,
57
+ __dirname: __dirname
58
+ }));
59
+ }
60
+ catch (e) {
61
+ console.log(e)
62
+ }
63
+
64
+ if (deserializeArgs) {
65
+ return await module.exports(...EJSON.deserialize(args))
66
+ }
67
+
68
+ return await module.exports(...args)
69
+ }
70
+ const res = await functionsQueue.add(run, enqueue)
71
+ return res
57
72
  }
@@ -13,6 +13,8 @@ export interface GenerateContextParams {
13
13
  services: Services
14
14
  args: Arguments
15
15
  runAsSystem?: boolean
16
+ deserializeArgs?: boolean
17
+ enqueue?: boolean
16
18
  }
17
19
 
18
20
  export interface GenerateContextDataParams extends Omit<GenerateContextParams, 'args'> {
@@ -44,7 +44,7 @@ const evaluateComplexExpression = async (
44
44
  const response = await GenerateContext({
45
45
  args: [params.cursor],
46
46
  app,
47
- rules: {},
47
+ rules: StateManager.select("rules"),
48
48
  user,
49
49
  currentFunction,
50
50
  functionsList,