@drax/identity-back 0.0.22 → 0.0.25
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/dist/factory/RoleServiceFactory.js +0 -1
- package/dist/factory/TenantServiceFactory.js +0 -1
- package/dist/factory/UserServiceFactory.js +0 -1
- package/dist/graphql/resolvers/role.resolvers.js +2 -2
- package/dist/graphql/resolvers/tenant.resolvers.js +2 -2
- package/dist/graphql/resolvers/user.resolvers.js +2 -2
- package/dist/graphql/types/role.graphql +1 -1
- package/dist/graphql/types/tenant.graphql +1 -1
- package/dist/graphql/types/user.graphql +1 -1
- package/dist/models/UserModel.js +0 -1
- package/dist/repository/mongo/RoleMongoRepository.js +1 -1
- package/dist/repository/mongo/TenantMongoRepository.js +1 -1
- package/dist/repository/mongo/UserMongoRepository.js +1 -1
- package/dist/repository/sqlite/RoleSqliteRepository.js +33 -33
- package/dist/repository/sqlite/TenantSqliteRepository.js +1 -1
- package/dist/repository/sqlite/UserSqliteRepository.js +1 -1
- package/dist/routes/RoleRoutes.js +3 -1
- package/dist/routes/TenantRoutes.js +3 -1
- package/dist/routes/UserRoutes.js +3 -1
- package/dist/services/RoleService.js +2 -2
- package/dist/services/TenantService.js +2 -2
- package/dist/services/UserService.js +2 -2
- package/package.json +5 -2
- package/src/factory/RoleServiceFactory.ts +0 -1
- package/src/factory/TenantServiceFactory.ts +0 -1
- package/src/factory/UserServiceFactory.ts +0 -1
- package/src/graphql/resolvers/role.resolvers.ts +2 -2
- package/src/graphql/resolvers/tenant.resolvers.ts +2 -2
- package/src/graphql/resolvers/user.resolvers.ts +2 -2
- package/src/graphql/types/role.graphql +1 -1
- package/src/graphql/types/tenant.graphql +1 -1
- package/src/graphql/types/user.graphql +1 -1
- package/src/index.ts +6 -9
- package/src/interfaces/IRoleRepository.ts +5 -10
- package/src/interfaces/ITenantRepository.ts +4 -10
- package/src/interfaces/IUserRepository.ts +6 -12
- package/src/middleware/jwtMiddleware.ts +1 -1
- package/src/middleware/rbacMiddleware.ts +1 -2
- package/src/models/RoleModel.ts +1 -1
- package/src/models/TenantModel.ts +1 -1
- package/src/models/UserGroupModel.ts +1 -1
- package/src/models/UserModel.ts +1 -2
- package/src/rbac/Rbac.ts +1 -2
- package/src/repository/mongo/RoleMongoRepository.ts +14 -8
- package/src/repository/mongo/TenantMongoRepository.ts +14 -7
- package/src/repository/mongo/UserMongoRepository.ts +14 -9
- package/src/repository/sqlite/RoleSqliteRepository.ts +53 -44
- package/src/repository/sqlite/TenantSqliteRepository.ts +13 -8
- package/src/repository/sqlite/UserSqliteRepository.ts +15 -10
- package/src/routes/RoleRoutes.ts +7 -4
- package/src/routes/TenantRoutes.ts +7 -5
- package/src/routes/UserRoutes.ts +7 -4
- package/src/services/RoleService.ts +15 -8
- package/src/services/TenantService.ts +15 -9
- package/src/services/UserService.ts +13 -7
- package/src/setup/CreateOrUpdateRole.ts +2 -2
- package/src/setup/CreateUserIfNotExist.ts +1 -1
- package/test/data-obj/roles/admin-mongo-role.ts +1 -1
- package/test/initializers/RoleMongoInitializer.ts +1 -1
- package/test/initializers/RoleSqliteInitializer.ts +1 -1
- package/test/repository/mongo/role-mongo-repository.test.ts +1 -1
- package/test/repository/mongo/user-mongo-repository.test.ts +1 -1
- package/test/repository/sqlite/role-sqlite-repository.test.ts +1 -1
- package/test/repository/sqlite/user-sqlite-repository.test.ts +2 -2
- package/test/service/mock-service.test.ts +2 -2
- package/test/service/role-service.test.ts +2 -2
- package/test/service/user-service.test.ts +3 -3
- package/tsconfig.tsbuildinfo +1 -1
- package/types/factory/RoleServiceFactory.d.ts.map +1 -1
- package/types/factory/TenantServiceFactory.d.ts.map +1 -1
- package/types/factory/UserServiceFactory.d.ts.map +1 -1
- package/types/graphql/resolvers/role.resolvers.d.ts +10 -8
- package/types/graphql/resolvers/role.resolvers.d.ts.map +1 -1
- package/types/graphql/resolvers/tenant.resolvers.d.ts +10 -8
- package/types/graphql/resolvers/tenant.resolvers.d.ts.map +1 -1
- package/types/graphql/resolvers/user.resolvers.d.ts +8 -6
- package/types/graphql/resolvers/user.resolvers.d.ts.map +1 -1
- package/types/index.d.ts +3 -5
- package/types/index.d.ts.map +1 -1
- package/types/interfaces/IRoleRepository.d.ts +4 -10
- package/types/interfaces/IRoleRepository.d.ts.map +1 -1
- package/types/interfaces/ITenantRepository.d.ts +4 -10
- package/types/interfaces/ITenantRepository.d.ts.map +1 -1
- package/types/interfaces/IUserRepository.d.ts +5 -11
- package/types/interfaces/IUserRepository.d.ts.map +1 -1
- package/types/middleware/rbacMiddleware.d.ts.map +1 -1
- package/types/models/RoleModel.d.ts +1 -1
- package/types/models/RoleModel.d.ts.map +1 -1
- package/types/models/TenantModel.d.ts +1 -1
- package/types/models/TenantModel.d.ts.map +1 -1
- package/types/models/UserGroupModel.d.ts +1 -1
- package/types/models/UserGroupModel.d.ts.map +1 -1
- package/types/models/UserModel.d.ts +1 -1
- package/types/models/UserModel.d.ts.map +1 -1
- package/types/rbac/Rbac.d.ts +1 -2
- package/types/rbac/Rbac.d.ts.map +1 -1
- package/types/repository/mongo/RoleMongoRepository.d.ts +7 -8
- package/types/repository/mongo/RoleMongoRepository.d.ts.map +1 -1
- package/types/repository/mongo/TenantMongoRepository.d.ts +7 -8
- package/types/repository/mongo/TenantMongoRepository.d.ts.map +1 -1
- package/types/repository/mongo/UserMongoRepository.d.ts +7 -9
- package/types/repository/mongo/UserMongoRepository.d.ts.map +1 -1
- package/types/repository/sqlite/RoleSqliteRepository.d.ts +10 -11
- package/types/repository/sqlite/RoleSqliteRepository.d.ts.map +1 -1
- package/types/repository/sqlite/TenantSqliteRepository.d.ts +7 -8
- package/types/repository/sqlite/TenantSqliteRepository.d.ts.map +1 -1
- package/types/repository/sqlite/UserSqliteRepository.d.ts +9 -12
- package/types/repository/sqlite/UserSqliteRepository.d.ts.map +1 -1
- package/types/routes/RoleRoutes.d.ts.map +1 -1
- package/types/routes/TenantRoutes.d.ts.map +1 -1
- package/types/routes/UserRoutes.d.ts.map +1 -1
- package/types/services/RoleService.d.ts +7 -7
- package/types/services/RoleService.d.ts.map +1 -1
- package/types/services/TenantService.d.ts +7 -7
- package/types/services/TenantService.d.ts.map +1 -1
- package/types/services/UserService.d.ts +6 -6
- package/types/services/UserService.d.ts.map +1 -1
- package/types/setup/CreateOrUpdateRole.d.ts +2 -2
- package/types/setup/CreateOrUpdateRole.d.ts.map +1 -1
- package/types/setup/CreateUserIfNotExist.d.ts +1 -1
- package/types/setup/CreateUserIfNotExist.d.ts.map +1 -1
- package/src/interfaces/IID.ts +0 -5
- package/src/interfaces/IJwtUser.ts +0 -7
- package/src/interfaces/IRole.ts +0 -21
- package/src/interfaces/ITenant.ts +0 -9
- package/src/interfaces/IUser.ts +0 -48
- package/src/interfaces/IUserGroup.ts +0 -11
package/src/rbac/Rbac.ts
CHANGED
|
@@ -1,30 +1,31 @@
|
|
|
1
1
|
import {RoleModel} from "../../models/RoleModel.js";
|
|
2
|
-
import {IRole} from '../../interfaces/IRole'
|
|
3
2
|
import {IRoleRepository} from '../../interfaces/IRoleRepository'
|
|
4
|
-
import {
|
|
3
|
+
import { mongoose} from "@drax/common-back";
|
|
5
4
|
import {FilterQuery, PaginateOptions, PaginateResult} from "mongoose";
|
|
6
5
|
import {DeleteResult} from "mongodb";
|
|
6
|
+
import {IDraxPaginateOptions, IDraxPaginateResult} from "@drax/common-share";
|
|
7
|
+
import {IRoleBase, IRole} from "@drax/identity-share";
|
|
7
8
|
|
|
8
9
|
class RoleMongoRepository implements IRoleRepository{
|
|
9
10
|
|
|
10
|
-
async create(roleData:
|
|
11
|
+
async create(roleData: IRoleBase): Promise<IRole> {
|
|
11
12
|
const role : mongoose.HydratedDocument<IRole> = new RoleModel(roleData)
|
|
12
13
|
await role.save()
|
|
13
14
|
await role.populate('childRoles')
|
|
14
15
|
return role
|
|
15
16
|
}
|
|
16
17
|
|
|
17
|
-
async update(id:
|
|
18
|
+
async update(id: string, roleData: IRoleBase): Promise<IRole> {
|
|
18
19
|
const role : mongoose.HydratedDocument<IRole> = await RoleModel.findOneAndUpdate({_id: id}, roleData, {new: true}).populate('childRoles').exec()
|
|
19
20
|
return role
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
async delete(id:
|
|
23
|
+
async delete(id: string): Promise<boolean> {
|
|
23
24
|
const result : DeleteResult = await RoleModel.deleteOne({_id: id}).exec()
|
|
24
25
|
return result.deletedCount == 1
|
|
25
26
|
}
|
|
26
27
|
|
|
27
|
-
async findById(id:
|
|
28
|
+
async findById(id: string): Promise<IRole | null>{
|
|
28
29
|
const role: mongoose.HydratedDocument<IRole> | null = await RoleModel.findById(id).populate('childRoles').exec()
|
|
29
30
|
return role
|
|
30
31
|
}
|
|
@@ -39,8 +40,13 @@ class RoleMongoRepository implements IRoleRepository{
|
|
|
39
40
|
return roles
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
async paginate(
|
|
43
|
-
|
|
43
|
+
async paginate({
|
|
44
|
+
page= 1,
|
|
45
|
+
limit= 5,
|
|
46
|
+
orderBy= '',
|
|
47
|
+
orderDesc= false,
|
|
48
|
+
search= '',
|
|
49
|
+
filters= []} : IDraxPaginateOptions): Promise<IDraxPaginateResult<IRole>> {
|
|
44
50
|
const query = {}
|
|
45
51
|
|
|
46
52
|
if(search){
|
|
@@ -1,29 +1,30 @@
|
|
|
1
1
|
import {TenantModel} from "../../models/TenantModel.js";
|
|
2
|
-
import {ITenant} from '../../interfaces/ITenant'
|
|
3
2
|
import {ITenantRepository} from '../../interfaces/ITenantRepository'
|
|
4
|
-
import {
|
|
3
|
+
import { mongoose} from "@drax/common-back";
|
|
5
4
|
import {FilterQuery, PaginateOptions, PaginateResult} from "mongoose";
|
|
6
5
|
import {DeleteResult} from "mongodb";
|
|
6
|
+
import {IDraxPaginateOptions, IDraxPaginateResult} from "@drax/common-share";
|
|
7
|
+
import {ITenant, ITenantBase} from "@drax/identity-share";
|
|
7
8
|
|
|
8
9
|
class TenantMongoRepository implements ITenantRepository{
|
|
9
10
|
|
|
10
|
-
async create(tenantData:
|
|
11
|
+
async create(tenantData: ITenantBase): Promise<ITenant> {
|
|
11
12
|
const tenant : mongoose.HydratedDocument<ITenant> = new TenantModel(tenantData)
|
|
12
13
|
await tenant.save()
|
|
13
14
|
return tenant
|
|
14
15
|
}
|
|
15
16
|
|
|
16
|
-
async update(id:
|
|
17
|
+
async update(id: string, tenantData: ITenantBase): Promise<ITenant> {
|
|
17
18
|
const tenant : mongoose.HydratedDocument<ITenant> = await TenantModel.findOneAndUpdate({_id: id}, tenantData, {new: true}).exec()
|
|
18
19
|
return tenant
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
async delete(id:
|
|
22
|
+
async delete(id: string): Promise<boolean> {
|
|
22
23
|
const result : DeleteResult = await TenantModel.deleteOne({_id:id}).exec()
|
|
23
24
|
return result.deletedCount == 1
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
async findById(id:
|
|
27
|
+
async findById(id: string): Promise<ITenant | null>{
|
|
27
28
|
const tenant: mongoose.HydratedDocument<ITenant> | null = await TenantModel.findById(id).exec()
|
|
28
29
|
return tenant
|
|
29
30
|
}
|
|
@@ -38,7 +39,13 @@ class TenantMongoRepository implements ITenantRepository{
|
|
|
38
39
|
return tenants
|
|
39
40
|
}
|
|
40
41
|
|
|
41
|
-
async paginate(
|
|
42
|
+
async paginate({
|
|
43
|
+
page= 1,
|
|
44
|
+
limit= 5,
|
|
45
|
+
orderBy= '',
|
|
46
|
+
orderDesc= false,
|
|
47
|
+
search= '',
|
|
48
|
+
filters= []} : IDraxPaginateOptions): Promise<IDraxPaginateResult<ITenant>> {
|
|
42
49
|
|
|
43
50
|
const query = {}
|
|
44
51
|
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {UserModel} from "../../models/UserModel.js";
|
|
2
2
|
import {mongoose, MongooseErrorToValidationError, MongoServerErrorToValidationError, ValidationError} from "@drax/common-back"
|
|
3
|
-
import type {
|
|
4
|
-
import type {IUser, IUserCreate, IUserUpdate} from "../../interfaces/IUser";
|
|
3
|
+
import type {IUser, IUserCreate, IUserUpdate} from "@drax/identity-share";
|
|
5
4
|
import {DeleteResult, MongoServerError} from "mongodb";
|
|
6
5
|
import type {IUserRepository} from "../../interfaces/IUserRepository";
|
|
7
6
|
import {PaginateResult} from "mongoose";
|
|
8
7
|
import RoleMongoRepository from "./RoleMongoRepository.js";
|
|
8
|
+
import {IDraxPaginateOptions, IDraxPaginateResult} from "@drax/common-share";
|
|
9
9
|
|
|
10
10
|
class UserMongoRepository implements IUserRepository {
|
|
11
11
|
private roleRepository: RoleMongoRepository;
|
|
@@ -18,7 +18,7 @@ class UserMongoRepository implements IUserRepository {
|
|
|
18
18
|
async create(userData: IUserCreate): Promise<IUser> {
|
|
19
19
|
try{
|
|
20
20
|
|
|
21
|
-
if(!await this.roleRepository.findById(userData.role
|
|
21
|
+
if(!await this.roleRepository.findById(userData.role)){
|
|
22
22
|
throw new ValidationError([{field: 'role', reason: 'validation.notfound', value: userData.role}])
|
|
23
23
|
}
|
|
24
24
|
|
|
@@ -35,7 +35,7 @@ class UserMongoRepository implements IUserRepository {
|
|
|
35
35
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
async update(id:
|
|
38
|
+
async update(id: string, userData: IUserUpdate): Promise<IUser> {
|
|
39
39
|
try{
|
|
40
40
|
const user: mongoose.HydratedDocument<IUser> = await UserModel.findOneAndUpdate({_id: id}, userData, {new: true}).populate(['role','tenant']).exec()
|
|
41
41
|
return user
|
|
@@ -50,13 +50,13 @@ class UserMongoRepository implements IUserRepository {
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
async delete(id:
|
|
53
|
+
async delete(id: string): Promise<boolean> {
|
|
54
54
|
const result: DeleteResult = await UserModel.deleteOne({_id: id}).exec()
|
|
55
55
|
return result.deletedCount == 1
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
|
|
59
|
-
async findById(id:
|
|
59
|
+
async findById(id:string): Promise<IUser> {
|
|
60
60
|
const user: mongoose.HydratedDocument<IUser> = await UserModel.findById(id).populate(['role','tenant']).exec()
|
|
61
61
|
return user
|
|
62
62
|
}
|
|
@@ -66,8 +66,13 @@ class UserMongoRepository implements IUserRepository {
|
|
|
66
66
|
return user
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
async paginate(
|
|
70
|
-
|
|
69
|
+
async paginate({
|
|
70
|
+
page= 1,
|
|
71
|
+
limit= 5,
|
|
72
|
+
orderBy= '',
|
|
73
|
+
orderDesc= false,
|
|
74
|
+
search= '',
|
|
75
|
+
filters= []} : IDraxPaginateOptions): Promise<IDraxPaginateResult<IUser>> {
|
|
71
76
|
|
|
72
77
|
const query = {}
|
|
73
78
|
|
|
@@ -104,7 +109,7 @@ class UserMongoRepository implements IUserRepository {
|
|
|
104
109
|
}
|
|
105
110
|
}
|
|
106
111
|
|
|
107
|
-
async changePassword(id:
|
|
112
|
+
async changePassword(id: string, password: string):Promise<boolean> {
|
|
108
113
|
try{
|
|
109
114
|
await UserModel.findOneAndUpdate({_id: id}, {password}).exec()
|
|
110
115
|
return true
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import {IRole} from '../../interfaces/IRole'
|
|
2
1
|
import {IRoleRepository} from '../../interfaces/IRoleRepository'
|
|
3
2
|
import {UUID} from "crypto";
|
|
4
3
|
import sqlite from "better-sqlite3";
|
|
5
4
|
import {randomUUID} from "node:crypto";
|
|
6
|
-
import {
|
|
5
|
+
import {IDraxPaginateResult, IDraxPaginateOptions} from "@drax/common-share";
|
|
6
|
+
import {IRole, IRoleBase} from "@drax/identity-share";
|
|
7
7
|
import {SqliteErrorToValidationError} from "@drax/common-back";
|
|
8
|
-
import {IID} from "../../interfaces/IID";
|
|
9
8
|
|
|
10
9
|
const roleTableSQL: string = `
|
|
11
10
|
CREATE TABLE IF NOT EXISTS roles
|
|
@@ -32,7 +31,7 @@ class RoleSqliteRepository implements IRoleRepository{
|
|
|
32
31
|
this.db.exec(roleTableSQL);
|
|
33
32
|
}
|
|
34
33
|
|
|
35
|
-
normalizeData(roleData:
|
|
34
|
+
normalizeData(roleData: IRoleBase){
|
|
36
35
|
|
|
37
36
|
roleData.readonly = roleData.readonly ? 1 : 0
|
|
38
37
|
|
|
@@ -45,7 +44,7 @@ class RoleSqliteRepository implements IRoleRepository{
|
|
|
45
44
|
}
|
|
46
45
|
}
|
|
47
46
|
|
|
48
|
-
async create(roleData:
|
|
47
|
+
async create(roleData: IRoleBase): Promise<IRole> {
|
|
49
48
|
try{
|
|
50
49
|
|
|
51
50
|
if(!roleData.id){
|
|
@@ -76,25 +75,9 @@ class RoleSqliteRepository implements IRoleRepository{
|
|
|
76
75
|
}
|
|
77
76
|
}
|
|
78
77
|
|
|
79
|
-
async findById(id: IID): Promise<IRole | null>{
|
|
80
|
-
const role = this.db.prepare('SELECT * FROM roles WHERE id = ?').get(id);
|
|
81
|
-
if(role){
|
|
82
|
-
await this.populateRole(role)
|
|
83
|
-
return role
|
|
84
|
-
}
|
|
85
|
-
return undefined
|
|
86
|
-
}
|
|
87
78
|
|
|
88
|
-
async findByName(name: string): Promise<IRole | null>{
|
|
89
|
-
const role = this.db.prepare('SELECT * FROM roles WHERE name = ?').get(name);
|
|
90
|
-
if(role){
|
|
91
|
-
await this.populateRole(role)
|
|
92
|
-
return role
|
|
93
|
-
}
|
|
94
|
-
return undefined
|
|
95
|
-
}
|
|
96
79
|
|
|
97
|
-
async update(id:
|
|
80
|
+
async update(id: string, roleData: IRoleBase): Promise<IRole> {
|
|
98
81
|
try{
|
|
99
82
|
this.normalizeData(roleData)
|
|
100
83
|
const setClauses = Object.keys(roleData)
|
|
@@ -111,27 +94,13 @@ class RoleSqliteRepository implements IRoleRepository{
|
|
|
111
94
|
|
|
112
95
|
}
|
|
113
96
|
|
|
114
|
-
async
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const stmt = this.db.prepare('DELETE FROM roles');
|
|
122
|
-
stmt.run();
|
|
123
|
-
return true
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
async fetchAll(): Promise<IRole[]>{
|
|
127
|
-
const roles = this.db.prepare('SELECT * FROM roles').all();
|
|
128
|
-
for (const role of roles) {
|
|
129
|
-
await this.populateRole(role)
|
|
130
|
-
}
|
|
131
|
-
return roles
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
async paginate(page = 1, limit = 5, search=""): Promise<IPaginateResult>{
|
|
97
|
+
async paginate({
|
|
98
|
+
page= 1,
|
|
99
|
+
limit= 5,
|
|
100
|
+
orderBy= '',
|
|
101
|
+
orderDesc= false,
|
|
102
|
+
search= '',
|
|
103
|
+
filters= []} : IDraxPaginateOptions): Promise<IDraxPaginateResult<IRole>>{
|
|
135
104
|
const offset = page > 1 ? (page - 1) * limit : 0
|
|
136
105
|
|
|
137
106
|
let where=""
|
|
@@ -154,7 +123,47 @@ class RoleSqliteRepository implements IRoleRepository{
|
|
|
154
123
|
}
|
|
155
124
|
}
|
|
156
125
|
|
|
157
|
-
async
|
|
126
|
+
async delete(id: string): Promise<boolean> {
|
|
127
|
+
const stmt = this.db.prepare('DELETE FROM roles WHERE id = ?');
|
|
128
|
+
stmt.run(id);
|
|
129
|
+
return true
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async deleteAll(): Promise<boolean> {
|
|
133
|
+
const stmt = this.db.prepare('DELETE FROM roles');
|
|
134
|
+
stmt.run();
|
|
135
|
+
return true
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async findById(id: string): Promise<IRole | null>{
|
|
139
|
+
const role = this.db.prepare('SELECT * FROM roles WHERE id = ?').get(id);
|
|
140
|
+
if(role){
|
|
141
|
+
await this.populateRole(role)
|
|
142
|
+
return role
|
|
143
|
+
}
|
|
144
|
+
return undefined
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async findByName(name: string): Promise<IRole | null>{
|
|
148
|
+
const role = this.db.prepare('SELECT * FROM roles WHERE name = ?').get(name);
|
|
149
|
+
if(role){
|
|
150
|
+
await this.populateRole(role)
|
|
151
|
+
return role
|
|
152
|
+
}
|
|
153
|
+
return undefined
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async fetchAll(): Promise<IRole[]>{
|
|
157
|
+
const roles = this.db.prepare('SELECT * FROM roles').all();
|
|
158
|
+
for (const role of roles) {
|
|
159
|
+
await this.populateRole(role)
|
|
160
|
+
}
|
|
161
|
+
return roles
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
async findWithoutPopulateById(id: string): Promise<IRole | null>{
|
|
158
167
|
const role = this.db.prepare('SELECT * FROM roles WHERE id = ?').get(id);
|
|
159
168
|
if(role){
|
|
160
169
|
return role
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import {ITenant} from '
|
|
1
|
+
import {ITenant, ITenantBase} from '@drax/identity-share'
|
|
2
2
|
import {ITenantRepository} from '../../interfaces/ITenantRepository'
|
|
3
3
|
import {UUID} from "crypto";
|
|
4
4
|
import sqlite from "better-sqlite3";
|
|
5
5
|
import {randomUUID} from "node:crypto";
|
|
6
|
-
import {
|
|
6
|
+
import {IDraxPaginateResult, IDraxPaginateOptions} from "@drax/common-share";
|
|
7
7
|
import {SqliteErrorToValidationError} from "@drax/common-back";
|
|
8
|
-
import {IID} from "../../interfaces/IID";
|
|
9
8
|
|
|
10
9
|
const tenantTableSQL: string = `
|
|
11
10
|
CREATE TABLE IF NOT EXISTS tenants
|
|
@@ -30,7 +29,7 @@ class TenantSqliteRepository implements ITenantRepository{
|
|
|
30
29
|
|
|
31
30
|
|
|
32
31
|
|
|
33
|
-
async create(tenantData:
|
|
32
|
+
async create(tenantData: ITenantBase): Promise<ITenant> {
|
|
34
33
|
try{
|
|
35
34
|
|
|
36
35
|
if(!tenantData.id){
|
|
@@ -55,7 +54,7 @@ class TenantSqliteRepository implements ITenantRepository{
|
|
|
55
54
|
}
|
|
56
55
|
}
|
|
57
56
|
|
|
58
|
-
async findById(id:
|
|
57
|
+
async findById(id: string): Promise<ITenant | null>{
|
|
59
58
|
const tenant = this.db.prepare('SELECT * FROM tenants WHERE id = ?').get(id);
|
|
60
59
|
return tenant
|
|
61
60
|
}
|
|
@@ -65,7 +64,7 @@ class TenantSqliteRepository implements ITenantRepository{
|
|
|
65
64
|
return tenant
|
|
66
65
|
}
|
|
67
66
|
|
|
68
|
-
async update(id:
|
|
67
|
+
async update(id: string, tenantData: ITenantBase): Promise<ITenant> {
|
|
69
68
|
try{
|
|
70
69
|
const setClauses = Object.keys(tenantData)
|
|
71
70
|
.map(field => `${field} = @${field}`)
|
|
@@ -81,7 +80,7 @@ class TenantSqliteRepository implements ITenantRepository{
|
|
|
81
80
|
|
|
82
81
|
}
|
|
83
82
|
|
|
84
|
-
async delete(id:
|
|
83
|
+
async delete(id: string): Promise<boolean> {
|
|
85
84
|
const stmt = this.db.prepare('DELETE FROM tenants WHERE id = ?');
|
|
86
85
|
stmt.run(id);
|
|
87
86
|
return true
|
|
@@ -101,7 +100,13 @@ class TenantSqliteRepository implements ITenantRepository{
|
|
|
101
100
|
return tenants
|
|
102
101
|
}
|
|
103
102
|
|
|
104
|
-
async paginate(
|
|
103
|
+
async paginate({
|
|
104
|
+
page= 1,
|
|
105
|
+
limit= 5,
|
|
106
|
+
orderBy= '',
|
|
107
|
+
orderDesc= false,
|
|
108
|
+
search= '',
|
|
109
|
+
filters= []} : IDraxPaginateOptions): Promise<IDraxPaginateResult<ITenant>>{
|
|
105
110
|
const offset = page > 1 ? (page - 1) * limit : 0
|
|
106
111
|
|
|
107
112
|
let where=""
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import {IUser, IUserCreate, IUserUpdate} from "
|
|
1
|
+
import {IUser, IUserCreate, IUserUpdate} from "@drax/identity-share";
|
|
2
2
|
import sqlite from "better-sqlite3";
|
|
3
3
|
import {randomUUID} from "node:crypto";
|
|
4
4
|
import {UUID} from "crypto";
|
|
5
5
|
import {IUserRepository} from "../../interfaces/IUserRepository";
|
|
6
|
-
import
|
|
6
|
+
import {IDraxPaginateResult, IDraxPaginateOptions} from "@drax/common-share";
|
|
7
7
|
import { SqliteErrorToValidationError, ValidationError} from "@drax/common-back";
|
|
8
8
|
import RoleSqliteRepository from "./RoleSqliteRepository.js";
|
|
9
9
|
import TenantSqliteRepository from "./TenantSqliteRepository.js";
|
|
10
|
-
import {IID} from "../../interfaces/IID";
|
|
11
10
|
|
|
12
11
|
|
|
13
12
|
const userTableSQL: string = `
|
|
@@ -81,7 +80,7 @@ class UserSqliteRepository implements IUserRepository {
|
|
|
81
80
|
|
|
82
81
|
}
|
|
83
82
|
|
|
84
|
-
async update(id:
|
|
83
|
+
async update(id: string, userData: IUserUpdate): Promise<IUser> {
|
|
85
84
|
try {
|
|
86
85
|
if (!await this.findRoleById(userData.role)) {
|
|
87
86
|
throw new ValidationError([{field: 'role', reason: 'validation.notfound', value: userData.role}])
|
|
@@ -103,7 +102,7 @@ class UserSqliteRepository implements IUserRepository {
|
|
|
103
102
|
return this.findById(id)
|
|
104
103
|
}
|
|
105
104
|
|
|
106
|
-
async delete(id:
|
|
105
|
+
async delete(id: string): Promise<boolean> {
|
|
107
106
|
const stmt = this.db.prepare('DELETE FROM users WHERE id = ?');
|
|
108
107
|
stmt.run(id);
|
|
109
108
|
return true
|
|
@@ -115,7 +114,7 @@ class UserSqliteRepository implements IUserRepository {
|
|
|
115
114
|
return true
|
|
116
115
|
}
|
|
117
116
|
|
|
118
|
-
async findById(id:
|
|
117
|
+
async findById(id: string): Promise<IUser> {
|
|
119
118
|
const user = this.db.prepare('SELECT * FROM users WHERE id = ?').get(id);
|
|
120
119
|
if (!user) {
|
|
121
120
|
return null
|
|
@@ -135,7 +134,13 @@ class UserSqliteRepository implements IUserRepository {
|
|
|
135
134
|
return user
|
|
136
135
|
}
|
|
137
136
|
|
|
138
|
-
async paginate(
|
|
137
|
+
async paginate({
|
|
138
|
+
page= 1,
|
|
139
|
+
limit= 5,
|
|
140
|
+
orderBy= '',
|
|
141
|
+
orderDesc= false,
|
|
142
|
+
search= '',
|
|
143
|
+
filters= []} : IDraxPaginateOptions): Promise<IDraxPaginateResult<IUser>> {
|
|
139
144
|
|
|
140
145
|
const offset = page > 1 ? (page - 1) * limit : 0
|
|
141
146
|
|
|
@@ -186,15 +191,15 @@ class UserSqliteRepository implements IUserRepository {
|
|
|
186
191
|
}
|
|
187
192
|
}
|
|
188
193
|
|
|
189
|
-
async findRoleById(id:
|
|
194
|
+
async findRoleById(id: string) {
|
|
190
195
|
return await this.roleRepository.findById(id)
|
|
191
196
|
}
|
|
192
197
|
|
|
193
|
-
async findTenantById(id:
|
|
198
|
+
async findTenantById(id: string) {
|
|
194
199
|
return await this.tenantRepository.findById(id)
|
|
195
200
|
}
|
|
196
201
|
|
|
197
|
-
async changePassword(id:
|
|
202
|
+
async changePassword(id: string, password: string): Promise<boolean> {
|
|
198
203
|
const stmt = this.db.prepare(`UPDATE users
|
|
199
204
|
SET password = @password
|
|
200
205
|
WHERE id = @id `);
|
package/src/routes/RoleRoutes.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ValidationError} from "@drax/common-back";
|
|
2
2
|
import RoleServiceFactory from "../factory/RoleServiceFactory.js";
|
|
3
|
-
import {IRole} from "
|
|
3
|
+
import {IRole} from "@drax/identity-share";
|
|
4
4
|
import {IdentityPermissions} from "../permissions/IdentityPermissions.js";
|
|
5
5
|
import {PermissionService} from "../services/PermissionService.js";
|
|
6
6
|
import UnauthorizedError from "../errors/UnauthorizedError.js";
|
|
7
|
+
import {IDraxPaginateResult} from "@drax/common-share";
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
|
|
@@ -95,14 +96,16 @@ async function RoleRoutes(fastify, options) {
|
|
|
95
96
|
}
|
|
96
97
|
})
|
|
97
98
|
|
|
98
|
-
fastify.get('/api/roles', async (request, reply): Promise<
|
|
99
|
+
fastify.get('/api/roles', async (request, reply): Promise<IDraxPaginateResult<IRole>> => {
|
|
99
100
|
try {
|
|
100
101
|
request.rbac.assertPermission(IdentityPermissions.ViewRole)
|
|
101
102
|
const page = request.query.page
|
|
102
103
|
const limit = request.query.limit
|
|
104
|
+
const orderBy = request.query.orderBy
|
|
105
|
+
const orderDesc = request.query.orderDesc
|
|
103
106
|
const search = request.query.search
|
|
104
107
|
const roleService = RoleServiceFactory()
|
|
105
|
-
let paginateResult = await roleService.paginate(page, limit, search)
|
|
108
|
+
let paginateResult = await roleService.paginate({page, limit, search, orderBy, orderDesc})
|
|
106
109
|
return paginateResult
|
|
107
110
|
} catch (e) {
|
|
108
111
|
console.error(e)
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ValidationError} from "@drax/common-back";
|
|
2
2
|
import TenantServiceFactory from "../factory/TenantServiceFactory.js";
|
|
3
|
-
import {ITenant} from "
|
|
3
|
+
import {ITenant} from "@drax/identity-share";
|
|
4
4
|
import {IdentityPermissions} from "../permissions/IdentityPermissions.js";
|
|
5
|
-
import {PermissionService} from "../services/PermissionService.js";
|
|
6
5
|
import UnauthorizedError from "../errors/UnauthorizedError.js";
|
|
6
|
+
import {IDraxPaginateResult} from "@drax/common-share";
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
|
|
@@ -78,14 +78,16 @@ async function TenantRoutes(fastify, options) {
|
|
|
78
78
|
}
|
|
79
79
|
})
|
|
80
80
|
|
|
81
|
-
fastify.get('/api/tenants', async (request, reply): Promise<
|
|
81
|
+
fastify.get('/api/tenants', async (request, reply): Promise<IDraxPaginateResult<ITenant>> => {
|
|
82
82
|
try {
|
|
83
83
|
request.rbac.assertPermission(IdentityPermissions.ViewTenant)
|
|
84
84
|
const page = request.query.page
|
|
85
85
|
const limit = request.query.limit
|
|
86
|
+
const orderBy = request.query.orderBy
|
|
87
|
+
const orderDesc = request.query.orderDesc
|
|
86
88
|
const search = request.query.search
|
|
87
89
|
const tenantService = TenantServiceFactory()
|
|
88
|
-
let paginateResult = await tenantService.paginate(page, limit, search)
|
|
90
|
+
let paginateResult = await tenantService.paginate({page, limit,orderBy, orderDesc, search})
|
|
89
91
|
return paginateResult
|
|
90
92
|
} catch (e) {
|
|
91
93
|
console.error(e)
|
package/src/routes/UserRoutes.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import UserServiceFactory from "../factory/UserServiceFactory.js";
|
|
2
|
-
import {IUser} from "
|
|
3
|
-
import {
|
|
2
|
+
import {IUser} from "@drax/identity-share";
|
|
3
|
+
import {ValidationError} from "@drax/common-back";
|
|
4
4
|
import {IdentityPermissions} from "../permissions/IdentityPermissions.js";
|
|
5
5
|
import UnauthorizedError from "../errors/UnauthorizedError.js";
|
|
6
6
|
import BadCredentialsError from "../errors/BadCredentialsError.js";
|
|
7
|
+
import {IDraxPaginateResult} from "@drax/common-share";
|
|
7
8
|
|
|
8
9
|
async function UserRoutes(fastify, options) {
|
|
9
10
|
fastify.post('/api/auth', async (request, reply) => {
|
|
@@ -53,19 +54,21 @@ async function UserRoutes(fastify, options) {
|
|
|
53
54
|
|
|
54
55
|
})
|
|
55
56
|
|
|
56
|
-
fastify.get('/api/users', async (request, reply): Promise<
|
|
57
|
+
fastify.get('/api/users', async (request, reply): Promise<IDraxPaginateResult<IUser>> => {
|
|
57
58
|
|
|
58
59
|
try {
|
|
59
60
|
request.rbac.assertPermission(IdentityPermissions.ViewUser)
|
|
60
61
|
const page = request.query.page
|
|
61
62
|
const limit = request.query.limit
|
|
63
|
+
const orderBy = request.query.orderBy
|
|
64
|
+
const orderDesc = request.query.orderDesc
|
|
62
65
|
const search = request.query.search
|
|
63
66
|
const userService = UserServiceFactory()
|
|
64
67
|
const filters = []
|
|
65
68
|
if(request.rbac.getAuthUser.tenantId){
|
|
66
69
|
filters.push({field: 'tenant', operator: '$eq', value: request.rbac.getAuthUser.tenantId})
|
|
67
70
|
}
|
|
68
|
-
let paginateResult = await userService.paginate(page, limit, search, filters)
|
|
71
|
+
let paginateResult = await userService.paginate({page, limit, orderBy, orderDesc, search, filters})
|
|
69
72
|
return paginateResult
|
|
70
73
|
} catch (e) {
|
|
71
74
|
console.log("/api/users",e)
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {IRole} from "../interfaces/IRole";
|
|
2
1
|
import {IRoleRepository} from "../interfaces/IRoleRepository";
|
|
3
|
-
import {
|
|
2
|
+
import { ValidationError, ZodErrorToValidationError} from "@drax/common-back"
|
|
4
3
|
import {roleSchema} from "../zod/RoleZod.js";
|
|
5
4
|
import {ZodError} from "zod";
|
|
6
5
|
import UnauthorizedError from "../errors/UnauthorizedError.js";
|
|
6
|
+
import {IDraxPaginateOptions, IDraxPaginateResult} from "@drax/common-share";
|
|
7
|
+
import {IRoleBase, IRole} from "@drax/identity-share";
|
|
7
8
|
|
|
8
9
|
class RoleService {
|
|
9
10
|
|
|
@@ -14,7 +15,7 @@ class RoleService {
|
|
|
14
15
|
console.log("RoleService constructor")
|
|
15
16
|
}
|
|
16
17
|
|
|
17
|
-
async create(roleData:
|
|
18
|
+
async create(roleData: IRoleBase): Promise<IRole> {
|
|
18
19
|
try {
|
|
19
20
|
roleData.name = roleData?.name?.trim()
|
|
20
21
|
await roleSchema.parseAsync(roleData)
|
|
@@ -28,7 +29,7 @@ class RoleService {
|
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
async update(id:
|
|
32
|
+
async update(id: string, roleData: IRoleBase) {
|
|
32
33
|
try {
|
|
33
34
|
roleData.name = roleData?.name?.trim()
|
|
34
35
|
await roleSchema.parseAsync(roleData)
|
|
@@ -42,13 +43,13 @@ class RoleService {
|
|
|
42
43
|
}
|
|
43
44
|
}
|
|
44
45
|
|
|
45
|
-
async delete(id:
|
|
46
|
+
async delete(id: string): Promise<boolean> {
|
|
46
47
|
|
|
47
48
|
const deletedRole = await this._repository.delete(id);
|
|
48
49
|
return deletedRole;
|
|
49
50
|
}
|
|
50
51
|
|
|
51
|
-
async findById(id:
|
|
52
|
+
async findById(id: string): Promise<IRole | null> {
|
|
52
53
|
const role: IRole = await this._repository.findById(id);
|
|
53
54
|
return role
|
|
54
55
|
}
|
|
@@ -63,8 +64,14 @@ class RoleService {
|
|
|
63
64
|
return roles
|
|
64
65
|
}
|
|
65
66
|
|
|
66
|
-
async paginate(
|
|
67
|
-
|
|
67
|
+
async paginate({
|
|
68
|
+
page= 1,
|
|
69
|
+
limit= 5,
|
|
70
|
+
orderBy= '',
|
|
71
|
+
orderDesc= false,
|
|
72
|
+
search= '',
|
|
73
|
+
filters= []} : IDraxPaginateOptions): Promise<IDraxPaginateResult<IRole>>{
|
|
74
|
+
const pagination = await this._repository.paginate({page, limit, orderBy, orderDesc, search, filters});
|
|
68
75
|
return pagination;
|
|
69
76
|
}
|
|
70
77
|
|