@drax/identity-back 0.51.0 → 1.1.0

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 (74) hide show
  1. package/dist/controllers/UserController.js +1 -0
  2. package/dist/models/UserLoginFailModel.js +12 -1
  3. package/dist/models/UserSessionModel.js +12 -1
  4. package/dist/routes/RoleRoutes.js +3 -4
  5. package/dist/routes/TenantRoutes.js +1 -1
  6. package/dist/routes/UserLoginFailRoutes.js +1 -1
  7. package/dist/routes/UserRoutes.js +12 -12
  8. package/dist/routes/UserSessionRoutes.js +1 -1
  9. package/dist/schemas/RegisterSchema.js +6 -7
  10. package/dist/schemas/RoleSchema.js +4 -4
  11. package/dist/schemas/TenantSchema.js +4 -4
  12. package/dist/schemas/UserApiKeySchema.js +4 -4
  13. package/dist/schemas/UserLoginFailSchema.js +1 -1
  14. package/dist/schemas/UserSchema.js +10 -11
  15. package/dist/schemas/UserSessionSchema.js +1 -1
  16. package/package.json +11 -11
  17. package/src/controllers/UserController.ts +1 -0
  18. package/src/models/UserLoginFailModel.ts +14 -1
  19. package/src/models/UserSessionModel.ts +13 -1
  20. package/src/routes/RoleRoutes.ts +3 -4
  21. package/src/routes/TenantRoutes.ts +1 -1
  22. package/src/routes/UserLoginFailRoutes.ts +1 -1
  23. package/src/routes/UserRoutes.ts +12 -12
  24. package/src/routes/UserSessionRoutes.ts +1 -1
  25. package/src/schemas/RegisterSchema.ts +6 -7
  26. package/src/schemas/RoleSchema.ts +4 -4
  27. package/src/schemas/TenantSchema.ts +4 -4
  28. package/src/schemas/UserApiKeySchema.ts +4 -4
  29. package/src/schemas/UserLoginFailSchema.ts +1 -1
  30. package/src/schemas/UserSchema.ts +10 -11
  31. package/src/schemas/UserSessionSchema.ts +1 -1
  32. package/test/routes/tenant-route.test.ts +76 -52
  33. package/test/routes/user-route.test.ts +61 -53
  34. package/test/schemas/lab-schema.test.ts +8 -8
  35. package/test/{service → services}/role-service.test.ts +1 -2
  36. package/test/{service → services}/user-service.test.ts +25 -15
  37. package/test/setup/MongoInMemory.ts +44 -0
  38. package/test/setup/TestSetup.ts +153 -0
  39. package/test/{routes → setup}/data/admin-role.ts +1 -1
  40. package/tsconfig.tsbuildinfo +1 -1
  41. package/types/controllers/UserController.d.ts.map +1 -1
  42. package/types/models/UserLoginFailModel.d.ts +1 -1
  43. package/types/models/UserLoginFailModel.d.ts.map +1 -1
  44. package/types/models/UserSessionModel.d.ts +1 -1
  45. package/types/models/UserSessionModel.d.ts.map +1 -1
  46. package/types/routes/RoleRoutes.d.ts.map +1 -1
  47. package/types/schemas/LoginSchema.d.ts +2 -12
  48. package/types/schemas/LoginSchema.d.ts.map +1 -1
  49. package/types/schemas/PasswordSchema.d.ts +3 -17
  50. package/types/schemas/PasswordSchema.d.ts.map +1 -1
  51. package/types/schemas/RegisterSchema.d.ts +3 -21
  52. package/types/schemas/RegisterSchema.d.ts.map +1 -1
  53. package/types/schemas/RoleSchema.d.ts +9 -59
  54. package/types/schemas/RoleSchema.d.ts.map +1 -1
  55. package/types/schemas/SwitchTenantSchema.d.ts +2 -10
  56. package/types/schemas/SwitchTenantSchema.d.ts.map +1 -1
  57. package/types/schemas/TenantSchema.d.ts +5 -23
  58. package/types/schemas/TenantSchema.d.ts.map +1 -1
  59. package/types/schemas/TokenPayloadSchema.d.ts +1 -21
  60. package/types/schemas/TokenPayloadSchema.d.ts.map +1 -1
  61. package/types/schemas/UserApiKeySchema.d.ts +6 -29
  62. package/types/schemas/UserApiKeySchema.d.ts.map +1 -1
  63. package/types/schemas/UserLoginFailSchema.d.ts +3 -24
  64. package/types/schemas/UserLoginFailSchema.d.ts.map +1 -1
  65. package/types/schemas/UserSchema.d.ts +14 -157
  66. package/types/schemas/UserSchema.d.ts.map +1 -1
  67. package/types/schemas/UserSessionSchema.d.ts +4 -41
  68. package/types/schemas/UserSessionSchema.d.ts.map +1 -1
  69. package/test/routes/helpers/CreateRootUserAndAdminRole.ts +0 -17
  70. package/test/routes/helpers/FastifyTestServerFactory.ts +0 -26
  71. package/test/routes/helpers/InitializePermissions.ts +0 -23
  72. package/test/routes/helpers/SetupIdentityDrax.ts +0 -22
  73. /package/test/{service → services}/mock-service.test.ts +0 -0
  74. /package/test/{routes → setup}/data/root-user.ts +0 -0
@@ -151,6 +151,7 @@ class UserController extends AbstractFastifyController {
151
151
  item.password = undefined;
152
152
  delete item.password;
153
153
  }
154
+ console.log("Paginated users: ", JSON.stringify(paginateResult, null, 4));
154
155
  return paginateResult;
155
156
  }
156
157
  catch (e) {
@@ -15,6 +15,17 @@ UserLoginFailMongoSchema.set('toJSON', { getters: true, virtuals: true });
15
15
  UserLoginFailMongoSchema.set('toObject', { getters: true, virtuals: true });
16
16
  const MODEL_NAME = 'UserLoginFail';
17
17
  const COLLECTION_NAME = 'UserLoginFail';
18
- const UserLoginFailModel = mongoose.model(MODEL_NAME, UserLoginFailMongoSchema, COLLECTION_NAME);
18
+ let UserLoginFailModel;
19
+ try {
20
+ UserLoginFailModel = mongoose.model(MODEL_NAME, UserLoginFailMongoSchema, COLLECTION_NAME);
21
+ }
22
+ catch (e) {
23
+ if (e.name === 'OverwriteModelError') {
24
+ UserLoginFailModel = mongoose.model(MODEL_NAME);
25
+ }
26
+ else {
27
+ throw e;
28
+ }
29
+ }
19
30
  export { UserLoginFailMongoSchema, UserLoginFailModel };
20
31
  export default UserLoginFailModel;
@@ -16,6 +16,17 @@ UserSessionMongoSchema.set('toJSON', { getters: true, virtuals: true });
16
16
  UserSessionMongoSchema.set('toObject', { getters: true, virtuals: true });
17
17
  const MODEL_NAME = 'UserSession';
18
18
  const COLLECTION_NAME = 'UserSession';
19
- const UserSessionModel = mongoose.model(MODEL_NAME, UserSessionMongoSchema, COLLECTION_NAME);
19
+ let UserSessionModel;
20
+ try {
21
+ UserSessionModel = mongoose.model(MODEL_NAME, UserSessionMongoSchema, COLLECTION_NAME);
22
+ }
23
+ catch (e) {
24
+ if (e.name === 'OverwriteModelError') {
25
+ UserSessionModel = mongoose.model(MODEL_NAME);
26
+ }
27
+ else {
28
+ throw e;
29
+ }
30
+ }
20
31
  export { UserSessionMongoSchema, UserSessionModel };
21
32
  export default UserSessionModel;
@@ -1,11 +1,10 @@
1
1
  import RoleController from "../controllers/RoleController.js";
2
2
  import { CrudSchemaBuilder } from "@drax/crud-back";
3
3
  import { RoleBaseSchema, RoleSchema } from "../schemas/RoleSchema.js";
4
- import { zodToJsonSchema } from "zod-to-json-schema";
5
4
  import zod from "zod";
6
5
  async function RoleRoutes(fastify, options) {
7
6
  const controller = new RoleController();
8
- const schemas = new CrudSchemaBuilder(RoleSchema, RoleBaseSchema, RoleBaseSchema, 'role', 'openApi3', ['Identity']);
7
+ const schemas = new CrudSchemaBuilder(RoleSchema, RoleBaseSchema, RoleBaseSchema, 'role', 'openapi-3.0', ['Identity']);
9
8
  fastify.get('/api/roles/search', { schema: schemas.searchSchema }, (req, rep) => controller.search(req, rep));
10
9
  fastify.get('/api/roles/:id', { schema: schemas.findByIdSchema }, (req, rep) => controller.findById(req, rep));
11
10
  fastify.get('/api/roles/all', { schema: schemas.allSchema }, (req, rep) => controller.all(req, rep));
@@ -18,7 +17,7 @@ async function RoleRoutes(fastify, options) {
18
17
  schema: {
19
18
  tags: ['Identity'],
20
19
  response: {
21
- 200: zodToJsonSchema(zod.array(zod.string())),
20
+ 200: zod.toJSONSchema(zod.array(zod.string())),
22
21
  401: schemas.jsonErrorBodyResponse,
23
22
  403: schemas.jsonErrorBodyResponse,
24
23
  }
@@ -27,7 +26,7 @@ async function RoleRoutes(fastify, options) {
27
26
  fastify.get('/api/roles/name/:name', {
28
27
  schema: {
29
28
  tags: ['Identity'],
30
- params: zodToJsonSchema(zod.object({ name: zod.string() })),
29
+ params: zod.toJSONSchema(zod.object({ name: zod.string() })),
31
30
  response: {
32
31
  200: schemas.jsonEntitySchema,
33
32
  401: schemas.jsonErrorBodyResponse,
@@ -3,7 +3,7 @@ import TenantController from '../controllers/TenantController.js';
3
3
  import { TenantSchema, TenantBaseSchema } from '../schemas/TenantSchema.js';
4
4
  async function TenantRoutes(fastify, options) {
5
5
  const controller = new TenantController();
6
- const schemas = new CrudSchemaBuilder(TenantSchema, TenantBaseSchema, TenantBaseSchema, 'tenant', 'openApi3', ['Identity']);
6
+ const schemas = new CrudSchemaBuilder(TenantSchema, TenantBaseSchema, TenantBaseSchema, 'tenant', 'openapi-3.0', ['Identity']);
7
7
  //Getters
8
8
  fastify.get('/api/tenants/search', { schema: schemas.searchSchema }, (req, rep) => controller.search(req, rep));
9
9
  fastify.get('/api/tenants/:id', { schema: schemas.findByIdSchema }, (req, rep) => controller.findById(req, rep));
@@ -3,7 +3,7 @@ import { CrudSchemaBuilder } from "@drax/crud-back";
3
3
  import { UserLoginFailSchema, UserLoginFailBaseSchema } from '../schemas/UserLoginFailSchema.js';
4
4
  async function UserLoginFailRoutes(fastify, options) {
5
5
  const controller = new UserLoginFailController();
6
- const schemas = new CrudSchemaBuilder(UserLoginFailSchema, UserLoginFailBaseSchema, UserLoginFailBaseSchema, 'UserLoginFail', 'openApi3', ['Identity']);
6
+ const schemas = new CrudSchemaBuilder(UserLoginFailSchema, UserLoginFailBaseSchema, UserLoginFailBaseSchema, 'UserLoginFail', 'openapi-3.0', ['Identity']);
7
7
  fastify.get('/api/user-login-fails', { schema: schemas.paginateSchema }, (req, rep) => controller.paginate(req, rep));
8
8
  fastify.get('/api/user-login-fails/group-by', { schema: schemas.groupBySchema }, (req, rep) => controller.groupBy(req, rep));
9
9
  fastify.get('/api/user-login-fails/export', { schema: schemas.exportSchema }, (req, rep) => controller.export(req, rep));
@@ -1,14 +1,14 @@
1
1
  import UserController from "../controllers/UserController.js";
2
- import { zodToJsonSchema } from "zod-to-json-schema";
3
2
  import { CrudSchemaBuilder } from "@drax/crud-back";
4
3
  import { LoginBodyRequestSchema, LoginBodyResponseSchema } from "../schemas/LoginSchema.js";
5
4
  import { UserSchema, UserCreateSchema, UserUpdateSchema } from "../schemas/UserSchema.js";
6
5
  import { RegisterBodyRequestSchema, RegisterBodyResponseSchema } from "../schemas/RegisterSchema.js";
7
6
  import { MyPasswordBodyRequestSchema, PasswordBodyRequestSchema, PasswordBodyResponseSchema } from "../schemas/PasswordSchema.js";
8
7
  import { SwitchTenantBodyRequestSchema, SwitchTenantBodyResponseSchema } from "../schemas/SwitchTenantSchema.js";
8
+ import zod from "zod";
9
9
  async function UserRoutes(fastify, options) {
10
10
  const controller = new UserController();
11
- const schemas = new CrudSchemaBuilder(UserSchema, UserCreateSchema, UserUpdateSchema, 'tenant', 'openApi3', ['Identity']);
11
+ const schemas = new CrudSchemaBuilder(UserSchema, UserCreateSchema, UserUpdateSchema, 'tenant', 'openapi-3.0', ['Identity']);
12
12
  fastify.get('/api/users/search', { schema: schemas.searchSchema }, async (req, rep) => await controller.search(req, rep));
13
13
  fastify.get('/api/users/group-by', { schema: schemas.groupBySchema }, async (req, rep) => await controller.groupBy(req, rep));
14
14
  fastify.get('/api/users/export', { schema: schemas.exportSchema }, (req, rep) => controller.export(req, rep));
@@ -19,9 +19,9 @@ async function UserRoutes(fastify, options) {
19
19
  fastify.post('/api/auth/login', {
20
20
  schema: {
21
21
  tags: ['Auth'],
22
- body: zodToJsonSchema(LoginBodyRequestSchema),
22
+ body: zod.toJSONSchema(LoginBodyRequestSchema),
23
23
  response: {
24
- 200: zodToJsonSchema(LoginBodyResponseSchema),
24
+ 200: zod.toJSONSchema(LoginBodyResponseSchema),
25
25
  400: schemas.jsonErrorBodyResponse,
26
26
  },
27
27
  },
@@ -39,9 +39,9 @@ async function UserRoutes(fastify, options) {
39
39
  fastify.post('/api/auth/switch-tenant', {
40
40
  schema: {
41
41
  tags: ['Auth'],
42
- body: zodToJsonSchema(SwitchTenantBodyRequestSchema),
42
+ body: zod.toJSONSchema(SwitchTenantBodyRequestSchema),
43
43
  response: {
44
- 200: zodToJsonSchema(SwitchTenantBodyResponseSchema),
44
+ 200: zod.toJSONSchema(SwitchTenantBodyResponseSchema),
45
45
  400: schemas.jsonErrorBodyResponse,
46
46
  },
47
47
  },
@@ -49,9 +49,9 @@ async function UserRoutes(fastify, options) {
49
49
  fastify.post('/api/users/register', {
50
50
  schema: {
51
51
  tags: ['Auth'],
52
- body: zodToJsonSchema(RegisterBodyRequestSchema),
52
+ body: zod.toJSONSchema(RegisterBodyRequestSchema),
53
53
  response: {
54
- 200: zodToJsonSchema(RegisterBodyResponseSchema),
54
+ 200: zod.toJSONSchema(RegisterBodyResponseSchema),
55
55
  400: schemas.jsonErrorBodyResponse,
56
56
  500: schemas.jsonErrorBodyResponse,
57
57
  },
@@ -70,8 +70,8 @@ async function UserRoutes(fastify, options) {
70
70
  fastify.post('/api/users/password/change', {
71
71
  schema: {
72
72
  tags: ['Auth'],
73
- body: zodToJsonSchema(MyPasswordBodyRequestSchema),
74
- 200: zodToJsonSchema(PasswordBodyResponseSchema),
73
+ body: zod.toJSONSchema(MyPasswordBodyRequestSchema),
74
+ 200: zod.toJSONSchema(PasswordBodyResponseSchema),
75
75
  400: schemas.jsonErrorBodyResponse,
76
76
  500: schemas.jsonErrorBodyResponse,
77
77
  }
@@ -79,8 +79,8 @@ async function UserRoutes(fastify, options) {
79
79
  fastify.post('/api/users/password/change/:id', {
80
80
  schema: {
81
81
  tags: ['Auth'],
82
- body: zodToJsonSchema(PasswordBodyRequestSchema),
83
- 200: zodToJsonSchema(PasswordBodyResponseSchema),
82
+ body: zod.toJSONSchema(PasswordBodyRequestSchema),
83
+ 200: zod.toJSONSchema(PasswordBodyResponseSchema),
84
84
  400: schemas.jsonErrorBodyResponse,
85
85
  500: schemas.jsonErrorBodyResponse,
86
86
  }
@@ -3,7 +3,7 @@ import { CrudSchemaBuilder } from "@drax/crud-back";
3
3
  import { UserSessionSchema, UserSessionBaseSchema } from '../schemas/UserSessionSchema.js';
4
4
  async function UserSessionRoutes(fastify, options) {
5
5
  const controller = new UserSessionController();
6
- const schemas = new CrudSchemaBuilder(UserSessionSchema, UserSessionBaseSchema, UserSessionBaseSchema, 'UserSession', 'openApi3', ['Identity']);
6
+ const schemas = new CrudSchemaBuilder(UserSessionSchema, UserSessionBaseSchema, UserSessionBaseSchema, 'UserSession', 'openapi-3.0', ['Identity']);
7
7
  fastify.get('/api/user-sessions', { schema: schemas.paginateSchema }, (req, rep) => controller.paginate(req, rep));
8
8
  fastify.get('/api/user-sessions/group-by', { schema: schemas.groupBySchema }, (req, rep) => controller.groupBy(req, rep));
9
9
  fastify.get('/api/user-sessions/export', { schema: schemas.exportSchema }, (req, rep) => controller.export(req, rep));
@@ -1,13 +1,12 @@
1
- import z, { string } from "zod";
1
+ import z, { string, email } from "zod";
2
2
  const RegisterBodyRequestSchema = z.object({
3
- name: string({ required_error: "validation.required" })
3
+ name: string({ error: "validation.required" })
4
4
  .min(1, "validation.required"),
5
- username: string({ required_error: "validation.required" })
5
+ username: string({ error: "validation.required" })
6
6
  .min(1, "validation.required"),
7
- email: string({ required_error: "validation.required" })
8
- .email("validation.email.invalid"),
9
- phone: string({ required_error: "validation.required" }).optional(),
10
- password: string({ required_error: "validation.required" })
7
+ email: email("validation.email.invalid"),
8
+ phone: string({ error: "validation.required" }).optional(),
9
+ password: string({ error: "validation.required" })
11
10
  .min(1, "validation.required")
12
11
  .min(8, "validation.password.min8")
13
12
  .max(64, "validation.password.max64"),
@@ -1,6 +1,6 @@
1
- import { date, object, string, array, boolean } from "zod";
1
+ import { object, string, array, boolean, iso } from "zod";
2
2
  const RoleBaseSchema = object({
3
- name: string({ required_error: "validation.required" })
3
+ name: string({ error: "validation.required" })
4
4
  .min(1, "validation.required")
5
5
  .regex(/^[A-Z]/, "validation.startWithUpperCase"),
6
6
  permissions: array(string()).optional(),
@@ -20,8 +20,8 @@ const RoleSchema = RoleBaseSchema.extend({
20
20
  id: string().optional(),
21
21
  name: string()
22
22
  })).optional(),
23
- createdAt: date().optional(),
24
- updatedAt: date().optional()
23
+ createdAt: iso.datetime().optional(),
24
+ updatedAt: iso.datetime().optional()
25
25
  });
26
26
  export default RoleSchema;
27
27
  export { RoleSchema, RoleBaseSchema };
@@ -1,6 +1,6 @@
1
- import { object, string, date, record, any } from "zod";
1
+ import { object, string, record, any, iso } from "zod";
2
2
  const TenantBaseSchema = object({
3
- name: string({ required_error: "validation.required" })
3
+ name: string({ error: "validation.required" })
4
4
  .min(1, "validation.required")
5
5
  //.regex(/^[A-Z]/, "validation.startWithUpperCase"),
6
6
  });
@@ -8,7 +8,7 @@ const TenantSchema = TenantBaseSchema.extend({
8
8
  _id: string(),
9
9
  id: string().optional(),
10
10
  custom: record(string(), any()).optional(),
11
- createdAt: date(),
12
- updatedAt: date()
11
+ createdAt: iso.datetime().optional(),
12
+ updatedAt: iso.datetime().optional()
13
13
  });
14
14
  export { TenantSchema, TenantBaseSchema };
@@ -1,9 +1,9 @@
1
- import { array, object, string } from "zod";
1
+ import { array, object, string, ipv4, ipv6 } from "zod";
2
2
  const UserApiKeyBaseSchema = object({
3
- name: string({ required_error: "validation.required" })
3
+ name: string({ error: "validation.required" })
4
4
  .min(1, "validation.required"),
5
- ipv4: array(string().ip({ version: "v4", message: 'validation.invalidIpv4' })),
6
- ipv6: array(string().ip({ version: "v6", message: 'validation.invalidIpv6' })),
5
+ ipv4: array(ipv4({ message: 'validation.invalidIpv4' })),
6
+ ipv6: array(ipv6({ message: 'validation.invalidIpv6' })),
7
7
  });
8
8
  const UserApiKeySchema = UserApiKeyBaseSchema.extend({
9
9
  _id: string(),
@@ -7,7 +7,7 @@ const UserLoginFailBaseSchema = z.object({
7
7
  const UserLoginFailSchema = UserLoginFailBaseSchema
8
8
  .extend({
9
9
  _id: z.string(),
10
- createdAt: z.coerce.date().nullable().optional()
10
+ createdAt: z.iso.datetime().nullable().optional()
11
11
  });
12
12
  export default UserLoginFailSchema;
13
13
  export { UserLoginFailSchema, UserLoginFailBaseSchema };
@@ -1,19 +1,18 @@
1
- import { date, object, string, boolean, array, record, any } from "zod";
1
+ import { email, iso, object, string, boolean, array, record, any } from "zod";
2
2
  const UserBaseSchema = object({
3
- name: string({ required_error: "validation.required" })
3
+ name: string({ error: "validation.required" })
4
4
  .min(1, "validation.required"),
5
- username: string({ required_error: "validation.required" })
5
+ username: string({ error: "validation.required" })
6
6
  .min(1, "validation.required"),
7
- email: string({ required_error: "validation.required" })
8
- .email("validation.email.invalid"),
9
- phone: string({ required_error: "validation.required" }).optional(),
7
+ email: email("validation.email.invalid"),
8
+ phone: string({ error: "validation.required" }).optional(),
10
9
  active: boolean().optional(),
11
- role: string({ required_error: "validation.required" })
10
+ role: string({ error: "validation.required" })
12
11
  .min(1, "validation.required"),
13
- tenant: string({ required_error: "validation.required" }).nullable().optional()
12
+ tenant: string({ error: "validation.required" }).nullable().optional()
14
13
  });
15
14
  const UserCreateSchema = UserBaseSchema.extend({
16
- password: string({ required_error: "validation.required" })
15
+ password: string({ error: "validation.required" })
17
16
  .min(1, "validation.required")
18
17
  .min(8, "validation.password.min8")
19
18
  .max(64, "validation.password.max64"),
@@ -37,8 +36,8 @@ const UserSchema = UserBaseSchema
37
36
  id: string().optional(),
38
37
  name: string(),
39
38
  custom: record(string(), any()).optional(),
40
- }).nullable().optional(),
41
- createdAt: date().optional(),
39
+ }).nullish(),
40
+ createdAt: iso.datetime().optional(),
42
41
  avatar: string().optional()
43
42
  });
44
43
  export { UserBaseSchema, UserSchema, UserCreateSchema, UserUpdateSchema, };
@@ -9,7 +9,7 @@ const UserSessionSchema = UserSessionBaseSchema
9
9
  .extend({
10
10
  _id: z.string(),
11
11
  user: z.object({ _id: z.string(), username: z.string() }),
12
- createdAt: z.coerce.date().nullable().optional()
12
+ createdAt: z.iso.datetime().nullable().optional()
13
13
  });
14
14
  export default UserSessionSchema;
15
15
  export { UserSessionSchema, UserSessionBaseSchema };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.51.0",
6
+ "version": "1.1.0",
7
7
  "description": "Identity module for user management, authentication and authorization.",
8
8
  "main": "dist/index.js",
9
9
  "types": "types/index.d.ts",
@@ -21,18 +21,18 @@
21
21
  "testMongoRepositoryUserApiKey": "node --import tsx --test test/repository/mongo/user-apikey-mongo*",
22
22
  "testSqliteRepositoryUser": "node --import tsx --test test/repository/sqlite/user*",
23
23
  "testSqliteRepositoryRole": "node --import tsx --test test/repository/sqlite/role*",
24
- "testServiceRole": "node --import tsx --test test/service/role*",
25
- "testServiceUser": "node --import tsx --test test/service/user*",
26
- "testcoverage": "node --import tsx --experimental-test-coverage test/service/*"
24
+ "testServiceRole": "node --import tsx --test test/services/role*",
25
+ "testServiceUser": "node --import tsx --test test/services/user*",
26
+ "testcoverage": "node --import tsx --experimental-test-coverage test/services/*"
27
27
  },
28
28
  "author": "Cristian Incarnato & Drax Team",
29
29
  "license": "ISC",
30
30
  "dependencies": {
31
- "@drax/common-back": "^0.51.0",
32
- "@drax/crud-back": "^0.51.0",
33
- "@drax/crud-share": "^0.51.0",
34
- "@drax/email-back": "^0.51.0",
35
- "@drax/identity-share": "^0.51.0",
31
+ "@drax/common-back": "^1.0.0",
32
+ "@drax/crud-back": "^1.1.0",
33
+ "@drax/crud-share": "^1.0.0",
34
+ "@drax/email-back": "^1.0.0",
35
+ "@drax/identity-share": "^1.0.0",
36
36
  "bcryptjs": "^2.4.3",
37
37
  "graphql": "^16.8.2",
38
38
  "jsonwebtoken": "^9.0.2"
@@ -43,7 +43,7 @@
43
43
  "mongoose": "^8.21.0",
44
44
  "mongoose-paginate-v2": "^1.8.3",
45
45
  "mongoose-unique-validator": "^5.0.1",
46
- "zod": "^3.23.8"
46
+ "zod": "^4.3.5"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@types/bcryptjs": "^2.4.6",
@@ -63,5 +63,5 @@
63
63
  "debug": "0"
64
64
  }
65
65
  },
66
- "gitHead": "a8fe4fe11637e3b899686b8dd8d658d62933ea5a"
66
+ "gitHead": "dbbb8bf014f02236b5a8597c592b0382372eb00f"
67
67
  }
@@ -180,6 +180,7 @@ class UserController extends AbstractFastifyController<IUser, IUserCreate, IUser
180
180
  item.password = undefined
181
181
  delete item.password
182
182
  }
183
+ console.log("Paginated users: ", JSON.stringify(paginateResult,null,4))
183
184
  return paginateResult
184
185
  } catch (e) {
185
186
  this.handleError(e, reply)
@@ -24,7 +24,20 @@ UserLoginFailMongoSchema.set('toObject', {getters: true, virtuals: true});
24
24
 
25
25
  const MODEL_NAME = 'UserLoginFail';
26
26
  const COLLECTION_NAME = 'UserLoginFail';
27
- const UserLoginFailModel = mongoose.model<IUserLoginFail, PaginateModel<IUserLoginFail>>(MODEL_NAME, UserLoginFailMongoSchema,COLLECTION_NAME);
27
+
28
+ let UserLoginFailModel;
29
+
30
+ try {
31
+ UserLoginFailModel = mongoose.model<IUserLoginFail, PaginateModel<IUserLoginFail>>(MODEL_NAME, UserLoginFailMongoSchema, COLLECTION_NAME);
32
+ } catch (e) {
33
+ if (e.name === 'OverwriteModelError') {
34
+ UserLoginFailModel = mongoose.model<IUserLoginFail, PaginateModel<IUserLoginFail>>(MODEL_NAME);
35
+ } else {
36
+ throw e;
37
+ }
38
+ }
39
+
40
+
28
41
 
29
42
  export {
30
43
  UserLoginFailMongoSchema,
@@ -26,7 +26,19 @@ UserSessionMongoSchema.set('toObject', {getters: true, virtuals: true});
26
26
 
27
27
  const MODEL_NAME = 'UserSession';
28
28
  const COLLECTION_NAME = 'UserSession';
29
- const UserSessionModel = mongoose.model<IUserSession, PaginateModel<IUserSession>>(MODEL_NAME, UserSessionMongoSchema,COLLECTION_NAME);
29
+
30
+ let UserSessionModel;
31
+
32
+ try {
33
+ UserSessionModel = mongoose.model<IUserSession, PaginateModel<IUserSession>>(MODEL_NAME, UserSessionMongoSchema,COLLECTION_NAME);
34
+ } catch (e) {
35
+ if (e.name === 'OverwriteModelError') {
36
+ UserSessionModel = mongoose.model<IUserSession, PaginateModel<IUserSession>>(MODEL_NAME);
37
+ } else {
38
+ throw e;
39
+ }
40
+ }
41
+
30
42
 
31
43
  export {
32
44
  UserSessionMongoSchema,
@@ -1,13 +1,12 @@
1
1
  import RoleController from "../controllers/RoleController.js";
2
2
  import {CrudSchemaBuilder} from "@drax/crud-back";
3
3
  import {RoleBaseSchema, RoleSchema} from "../schemas/RoleSchema.js";
4
- import {zodToJsonSchema} from "zod-to-json-schema";
5
4
  import zod from "zod";
6
5
 
7
6
  async function RoleRoutes(fastify, options) {
8
7
 
9
8
  const controller: RoleController = new RoleController()
10
- const schemas = new CrudSchemaBuilder(RoleSchema, RoleBaseSchema, RoleBaseSchema, 'role','openApi3', ['Identity']);
9
+ const schemas = new CrudSchemaBuilder(RoleSchema, RoleBaseSchema, RoleBaseSchema, 'role','openapi-3.0', ['Identity']);
11
10
 
12
11
  fastify.get('/api/roles/search', {schema: schemas.searchSchema}, (req, rep) => controller.search(req, rep))
13
12
 
@@ -29,7 +28,7 @@ async function RoleRoutes(fastify, options) {
29
28
  schema: {
30
29
  tags: ['Identity'],
31
30
  response: {
32
- 200: zodToJsonSchema(zod.array(zod.string())),
31
+ 200: zod.toJSONSchema(zod.array(zod.string())),
33
32
  401: schemas.jsonErrorBodyResponse,
34
33
  403: schemas.jsonErrorBodyResponse,
35
34
  }
@@ -39,7 +38,7 @@ async function RoleRoutes(fastify, options) {
39
38
  fastify.get('/api/roles/name/:name', {
40
39
  schema: {
41
40
  tags: ['Identity'],
42
- params: zodToJsonSchema(zod.object({name: zod.string()})),
41
+ params: zod.toJSONSchema(zod.object({name: zod.string()})),
43
42
  response: {
44
43
  200: schemas.jsonEntitySchema,
45
44
  401: schemas.jsonErrorBodyResponse,
@@ -7,7 +7,7 @@ import {TenantSchema, TenantBaseSchema} from '../schemas/TenantSchema.js'
7
7
  async function TenantRoutes(fastify, options) {
8
8
 
9
9
  const controller: TenantController = new TenantController()
10
- const schemas = new CrudSchemaBuilder(TenantSchema, TenantBaseSchema,TenantBaseSchema, 'tenant', 'openApi3', ['Identity']);
10
+ const schemas = new CrudSchemaBuilder(TenantSchema, TenantBaseSchema,TenantBaseSchema, 'tenant', 'openapi-3.0', ['Identity']);
11
11
 
12
12
  //Getters
13
13
  fastify.get('/api/tenants/search', {schema: schemas.searchSchema}, (req, rep) => controller.search(req, rep))
@@ -6,7 +6,7 @@ import {UserLoginFailSchema, UserLoginFailBaseSchema} from '../schemas/UserLogin
6
6
  async function UserLoginFailRoutes(fastify, options) {
7
7
 
8
8
  const controller: UserLoginFailController = new UserLoginFailController()
9
- const schemas = new CrudSchemaBuilder(UserLoginFailSchema, UserLoginFailBaseSchema,UserLoginFailBaseSchema, 'UserLoginFail', 'openApi3', ['Identity']);
9
+ const schemas = new CrudSchemaBuilder(UserLoginFailSchema, UserLoginFailBaseSchema,UserLoginFailBaseSchema, 'UserLoginFail', 'openapi-3.0', ['Identity']);
10
10
 
11
11
  fastify.get('/api/user-login-fails', {schema: schemas.paginateSchema}, (req,rep) => controller.paginate(req,rep))
12
12
 
@@ -1,5 +1,4 @@
1
1
  import UserController from "../controllers/UserController.js";
2
- import {zodToJsonSchema} from "zod-to-json-schema";
3
2
  import {CrudSchemaBuilder} from "@drax/crud-back";
4
3
  import {LoginBodyRequestSchema, LoginBodyResponseSchema} from "../schemas/LoginSchema.js"
5
4
  import {UserSchema, UserCreateSchema, UserUpdateSchema} from "../schemas/UserSchema.js";
@@ -10,11 +9,12 @@ import {
10
9
  PasswordBodyResponseSchema
11
10
  } from "../schemas/PasswordSchema.js";
12
11
  import {SwitchTenantBodyRequestSchema, SwitchTenantBodyResponseSchema} from "../schemas/SwitchTenantSchema.js";
12
+ import zod from "zod"
13
13
 
14
14
  async function UserRoutes(fastify, options) {
15
15
 
16
16
  const controller: UserController = new UserController()
17
- const schemas = new CrudSchemaBuilder(UserSchema, UserCreateSchema, UserUpdateSchema, 'tenant', 'openApi3', ['Identity']);
17
+ const schemas = new CrudSchemaBuilder(UserSchema, UserCreateSchema, UserUpdateSchema, 'tenant', 'openapi-3.0', ['Identity']);
18
18
 
19
19
 
20
20
  fastify.get('/api/users/search', {schema: schemas.searchSchema}, async (req, rep) => await controller.search(req, rep))
@@ -35,9 +35,9 @@ async function UserRoutes(fastify, options) {
35
35
  {
36
36
  schema: {
37
37
  tags: ['Auth'],
38
- body: zodToJsonSchema(LoginBodyRequestSchema),
38
+ body: zod.toJSONSchema(LoginBodyRequestSchema),
39
39
  response: {
40
- 200: zodToJsonSchema(LoginBodyResponseSchema),
40
+ 200: zod.toJSONSchema(LoginBodyResponseSchema),
41
41
  400: schemas.jsonErrorBodyResponse,
42
42
  },
43
43
  },
@@ -59,9 +59,9 @@ async function UserRoutes(fastify, options) {
59
59
  {
60
60
  schema: {
61
61
  tags: ['Auth'],
62
- body: zodToJsonSchema(SwitchTenantBodyRequestSchema),
62
+ body: zod.toJSONSchema(SwitchTenantBodyRequestSchema),
63
63
  response: {
64
- 200: zodToJsonSchema(SwitchTenantBodyResponseSchema),
64
+ 200: zod.toJSONSchema(SwitchTenantBodyResponseSchema),
65
65
  400: schemas.jsonErrorBodyResponse,
66
66
  },
67
67
  },
@@ -72,9 +72,9 @@ async function UserRoutes(fastify, options) {
72
72
  fastify.post('/api/users/register', {
73
73
  schema: {
74
74
  tags: ['Auth'],
75
- body: zodToJsonSchema(RegisterBodyRequestSchema),
75
+ body: zod.toJSONSchema(RegisterBodyRequestSchema),
76
76
  response: {
77
- 200: zodToJsonSchema(RegisterBodyResponseSchema),
77
+ 200: zod.toJSONSchema(RegisterBodyResponseSchema),
78
78
  400: schemas.jsonErrorBodyResponse,
79
79
  500: schemas.jsonErrorBodyResponse,
80
80
  },
@@ -96,8 +96,8 @@ async function UserRoutes(fastify, options) {
96
96
  fastify.post('/api/users/password/change', {
97
97
  schema: {
98
98
  tags: ['Auth'],
99
- body: zodToJsonSchema(MyPasswordBodyRequestSchema),
100
- 200: zodToJsonSchema(PasswordBodyResponseSchema),
99
+ body: zod.toJSONSchema(MyPasswordBodyRequestSchema),
100
+ 200: zod.toJSONSchema(PasswordBodyResponseSchema),
101
101
  400: schemas.jsonErrorBodyResponse,
102
102
  500: schemas.jsonErrorBodyResponse,
103
103
  }
@@ -106,8 +106,8 @@ async function UserRoutes(fastify, options) {
106
106
  fastify.post('/api/users/password/change/:id', {
107
107
  schema: {
108
108
  tags: ['Auth'],
109
- body: zodToJsonSchema(PasswordBodyRequestSchema),
110
- 200: zodToJsonSchema(PasswordBodyResponseSchema),
109
+ body: zod.toJSONSchema(PasswordBodyRequestSchema),
110
+ 200: zod.toJSONSchema(PasswordBodyResponseSchema),
111
111
  400: schemas.jsonErrorBodyResponse,
112
112
  500: schemas.jsonErrorBodyResponse,
113
113
  }
@@ -6,7 +6,7 @@ import {UserSessionSchema, UserSessionBaseSchema} from '../schemas/UserSessionSc
6
6
  async function UserSessionRoutes(fastify, options) {
7
7
 
8
8
  const controller: UserSessionController = new UserSessionController()
9
- const schemas = new CrudSchemaBuilder(UserSessionSchema, UserSessionBaseSchema,UserSessionBaseSchema, 'UserSession', 'openApi3', ['Identity']);
9
+ const schemas = new CrudSchemaBuilder(UserSessionSchema, UserSessionBaseSchema,UserSessionBaseSchema, 'UserSession', 'openapi-3.0', ['Identity']);
10
10
 
11
11
  fastify.get('/api/user-sessions', {schema: schemas.paginateSchema}, (req,rep) => controller.paginate(req,rep))
12
12
 
@@ -1,14 +1,13 @@
1
- import z, {string} from "zod"
1
+ import z, {string, email} from "zod"
2
2
 
3
3
  const RegisterBodyRequestSchema = z.object({
4
- name: string({ required_error: "validation.required" })
4
+ name: string({ error: "validation.required" })
5
5
  .min(1, "validation.required"),
6
- username: string({ required_error: "validation.required" })
6
+ username: string({ error: "validation.required" })
7
7
  .min(1, "validation.required"),
8
- email: string({ required_error: "validation.required" })
9
- .email("validation.email.invalid"),
10
- phone: string({ required_error: "validation.required" }).optional(),
11
- password: string({ required_error: "validation.required" })
8
+ email: email("validation.email.invalid"),
9
+ phone: string({ error: "validation.required" }).optional(),
10
+ password: string({ error: "validation.required" })
12
11
  .min(1, "validation.required")
13
12
  .min(8, "validation.password.min8")
14
13
  .max(64, "validation.password.max64"),
@@ -1,7 +1,7 @@
1
- import {date, object, string, array, boolean} from "zod"
1
+ import { object, string, array, boolean, iso} from "zod"
2
2
 
3
3
  const RoleBaseSchema = object({
4
- name: string({ required_error: "validation.required" })
4
+ name: string({ error: "validation.required" })
5
5
  .min(1, "validation.required")
6
6
  .regex(/^[A-Z]/, "validation.startWithUpperCase"),
7
7
  permissions: array(string()).optional(),
@@ -22,8 +22,8 @@ const RoleSchema = RoleBaseSchema.extend({
22
22
  id: string().optional(),
23
23
  name: string()
24
24
  })).optional(),
25
- createdAt: date().optional(),
26
- updatedAt: date().optional()
25
+ createdAt: iso.datetime().optional(),
26
+ updatedAt: iso.datetime().optional()
27
27
  })
28
28
 
29
29
 
@@ -1,7 +1,7 @@
1
- import { object, string, date, record, any } from "zod"
1
+ import { object, string, record, any, iso } from "zod"
2
2
 
3
3
  const TenantBaseSchema = object({
4
- name: string({ required_error: "validation.required" })
4
+ name: string({ error: "validation.required" })
5
5
  .min(1, "validation.required")
6
6
  //.regex(/^[A-Z]/, "validation.startWithUpperCase"),
7
7
  })
@@ -11,8 +11,8 @@ const TenantSchema = TenantBaseSchema.extend({
11
11
  _id: string(),
12
12
  id: string().optional(),
13
13
  custom: record(string(), any()).optional(),
14
- createdAt: date(),
15
- updatedAt: date()
14
+ createdAt: iso.datetime().optional(),
15
+ updatedAt: iso.datetime().optional()
16
16
  });
17
17
 
18
18