Skip to content

Commit 10c87c6

Browse files
resolve conflict
2 parents 171bb9e + fd08cdf commit 10c87c6

File tree

14 files changed

+563
-201
lines changed

14 files changed

+563
-201
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ack-nestjs-boilerplate-kafka",
3-
"version": "3.3.0",
3+
"version": "3.3.1",
44
"description": "Ack NestJs Boilerplate Kafka",
55
"repository": {
66
"type": "git",

src/common/auth/guards/payload/auth.payload.access-for.guard.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ export class AuthPayloadAccessForGuard implements CanActivate {
2424
[context.getHandler(), context.getClass()]
2525
);
2626

27-
if (!requiredFor) {
27+
const { user } = context.switchToHttp().getRequest();
28+
const { accessFor } = user;
29+
30+
if (!requiredFor || accessFor === ENUM_AUTH_ACCESS_FOR.SUPER_ADMIN) {
2831
return true;
2932
}
3033

31-
const { user } = context.switchToHttp().getRequest();
32-
const { accessFor } = user;
3334
const hasFor: boolean = this.helperArrayService.includes(
3435
requiredFor,
3536
accessFor

src/common/auth/guards/payload/auth.payload.permission.guard.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
CanActivate,
44
ExecutionContext,
55
ForbiddenException,
6+
UnauthorizedException,
67
} from '@nestjs/common';
78
import { Reflector } from '@nestjs/core';
89
import { AUTH_PERMISSION_META_KEY } from 'src/common/auth/constants/auth.constant';
@@ -24,14 +25,18 @@ export class AuthPayloadPermissionGuard implements CanActivate {
2425
AUTH_PERMISSION_META_KEY,
2526
[context.getHandler(), context.getClass()]
2627
);
27-
if (!requiredPermission) {
28-
return true;
29-
}
3028

3129
const { permissions, user } = context.switchToHttp().getRequest();
32-
const { accessFor } = user;
33-
34-
if (accessFor === ENUM_AUTH_ACCESS_FOR.SUPER_ADMIN) {
30+
if (!user) {
31+
throw new UnauthorizedException({
32+
statusCode:
33+
ENUM_AUTH_STATUS_CODE_ERROR.AUTH_JWT_ACCESS_TOKEN_ERROR,
34+
message: 'auth.error.accessTokenUnauthorized',
35+
});
36+
} else if (
37+
!requiredPermission ||
38+
user.accessFor === ENUM_AUTH_ACCESS_FOR.SUPER_ADMIN
39+
) {
3540
return true;
3641
}
3742

src/common/auth/guards/permission/auth.permission.guard.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
ForbiddenException,
77
} from '@nestjs/common';
88
import { ConfigService } from '@nestjs/config';
9+
import { ENUM_AUTH_ACCESS_FOR } from 'src/common/auth/constants/auth.enum.constant';
910
import { ENUM_AUTH_STATUS_CODE_ERROR } from 'src/common/auth/constants/auth.status-code.constant';
1011
import { AuthService } from 'src/common/auth/services/auth.service';
1112

@@ -25,6 +26,17 @@ export class AuthPermissionGuard implements CanActivate {
2526
async canActivate(context: ExecutionContext): Promise<boolean> {
2627
const request = context.switchToHttp().getRequest();
2728
const { user } = request;
29+
30+
if (!user) {
31+
throw new UnauthorizedException({
32+
statusCode:
33+
ENUM_AUTH_STATUS_CODE_ERROR.AUTH_JWT_ACCESS_TOKEN_ERROR,
34+
message: 'auth.error.accessTokenUnauthorized',
35+
});
36+
} else if (user.accessFor === ENUM_AUTH_ACCESS_FOR.SUPER_ADMIN) {
37+
return true;
38+
}
39+
2840
const { [this.headerName]: token } = request.headers;
2941

3042
if (!token) {

src/common/database/abstracts/database.base-repository.abstract.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ export abstract class DatabaseBaseRepositoryAbstract<T> {
2020
options?: IDatabaseFindAllOptions<any>
2121
): Promise<Y[]>;
2222

23+
abstract findAllDistinct<Y = T>(
24+
fieldDistinct: string,
25+
find?: Record<string, any> | Record<string, any>[],
26+
options?: IDatabaseFindAllOptions<any>
27+
): Promise<Y[]>;
28+
2329
abstract findOne<Y = T>(
2430
find: Record<string, any> | Record<string, any>[],
2531
options?: IDatabaseFindOneOptions<any>

src/common/database/abstracts/mongo/repositories/database.mongo.object-id.repository.abstract.ts

Lines changed: 95 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,63 @@ export abstract class DatabaseMongoObjectIdRepositoryAbstract<T>
6060
const findAll = this._repository.find(find);
6161

6262
if (options?.withDeleted) {
63-
findAll.where(DATABASE_DELETED_AT_FIELD_NAME).exists(true);
63+
findAll.or([
64+
{
65+
[DATABASE_DELETED_AT_FIELD_NAME]: { $exists: false },
66+
},
67+
{
68+
[DATABASE_DELETED_AT_FIELD_NAME]: { $exists: true },
69+
},
70+
]);
71+
} else {
72+
findAll.where(DATABASE_DELETED_AT_FIELD_NAME).exists(false);
73+
}
74+
75+
if (options?.select) {
76+
findAll.select(options.select);
77+
}
78+
79+
if (options?.paging) {
80+
findAll.limit(options.paging.limit).skip(options.paging.offset);
81+
}
82+
83+
if (options?.sort) {
84+
findAll.sort(
85+
this._convertSort(options.sort) as { [key: string]: SortOrder }
86+
);
87+
}
88+
89+
if (options?.join) {
90+
findAll.populate(
91+
typeof options.join === 'boolean'
92+
? this._joinOnFind
93+
: (options.join as PopulateOptions | PopulateOptions[])
94+
);
95+
}
96+
97+
if (options?.session) {
98+
findAll.session(options.session);
99+
}
100+
101+
return findAll.lean();
102+
}
103+
104+
async findAllDistinct<Y = T>(
105+
fieldDistinct: string,
106+
find?: Record<string, any> | Record<string, any>[],
107+
options?: IDatabaseFindAllOptions<ClientSession>
108+
): Promise<Y[]> {
109+
const findAll = this._repository.distinct(fieldDistinct, find);
110+
111+
if (options?.withDeleted) {
112+
findAll.or([
113+
{
114+
[DATABASE_DELETED_AT_FIELD_NAME]: { $exists: false },
115+
},
116+
{
117+
[DATABASE_DELETED_AT_FIELD_NAME]: { $exists: true },
118+
},
119+
]);
64120
} else {
65121
findAll.where(DATABASE_DELETED_AT_FIELD_NAME).exists(false);
66122
}
@@ -101,7 +157,14 @@ export abstract class DatabaseMongoObjectIdRepositoryAbstract<T>
101157
const findOne = this._repository.findOne(find);
102158

103159
if (options?.withDeleted) {
104-
findOne.where(DATABASE_DELETED_AT_FIELD_NAME).exists(true);
160+
findOne.or([
161+
{
162+
[DATABASE_DELETED_AT_FIELD_NAME]: { $exists: false },
163+
},
164+
{
165+
[DATABASE_DELETED_AT_FIELD_NAME]: { $exists: true },
166+
},
167+
]);
105168
} else {
106169
findOne.where(DATABASE_DELETED_AT_FIELD_NAME).exists(false);
107170
}
@@ -138,7 +201,14 @@ export abstract class DatabaseMongoObjectIdRepositoryAbstract<T>
138201
const findOne = this._repository.findById(_id);
139202

140203
if (options?.withDeleted) {
141-
findOne.where(DATABASE_DELETED_AT_FIELD_NAME).exists(true);
204+
findOne.or([
205+
{
206+
[DATABASE_DELETED_AT_FIELD_NAME]: { $exists: false },
207+
},
208+
{
209+
[DATABASE_DELETED_AT_FIELD_NAME]: { $exists: true },
210+
},
211+
]);
142212
} else {
143213
findOne.where(DATABASE_DELETED_AT_FIELD_NAME).exists(false);
144214
}
@@ -175,7 +245,14 @@ export abstract class DatabaseMongoObjectIdRepositoryAbstract<T>
175245
const count = this._repository.countDocuments(find);
176246

177247
if (options?.withDeleted) {
178-
count.where(DATABASE_DELETED_AT_FIELD_NAME).exists(true);
248+
count.or([
249+
{
250+
[DATABASE_DELETED_AT_FIELD_NAME]: { $exists: false },
251+
},
252+
{
253+
[DATABASE_DELETED_AT_FIELD_NAME]: { $exists: true },
254+
},
255+
]);
179256
} else {
180257
count.where(DATABASE_DELETED_AT_FIELD_NAME).exists(false);
181258
}
@@ -209,7 +286,14 @@ export abstract class DatabaseMongoObjectIdRepositoryAbstract<T>
209286
});
210287

211288
if (options?.withDeleted) {
212-
exist.where(DATABASE_DELETED_AT_FIELD_NAME).exists(true);
289+
exist.or([
290+
{
291+
[DATABASE_DELETED_AT_FIELD_NAME]: { $exists: false },
292+
},
293+
{
294+
[DATABASE_DELETED_AT_FIELD_NAME]: { $exists: true },
295+
},
296+
]);
213297
} else {
214298
exist.where(DATABASE_DELETED_AT_FIELD_NAME).exists(false);
215299
}
@@ -508,14 +592,11 @@ export abstract class DatabaseMongoObjectIdRepositoryAbstract<T>
508592
_id: string[],
509593
options?: IDatabaseManyOptions<ClientSession>
510594
): Promise<boolean> {
511-
const del = this._repository
512-
.deleteMany({
513-
_id: {
514-
$in: _id.map((val) => new Types.ObjectId(val)),
515-
},
516-
})
517-
.where(DATABASE_DELETED_AT_FIELD_NAME)
518-
.exists(false);
595+
const del = this._repository.deleteMany({
596+
_id: {
597+
$in: _id.map((val) => new Types.ObjectId(val)),
598+
},
599+
});
519600

520601
if (options?.session) {
521602
del.session(options.session);
@@ -541,10 +622,7 @@ export abstract class DatabaseMongoObjectIdRepositoryAbstract<T>
541622
find: Record<string, any> | Record<string, any>[],
542623
options?: IDatabaseManyOptions<ClientSession>
543624
): Promise<boolean> {
544-
const del = this._repository
545-
.deleteMany(find)
546-
.where(DATABASE_DELETED_AT_FIELD_NAME)
547-
.exists(false);
625+
const del = this._repository.deleteMany(find);
548626

549627
if (options?.session) {
550628
del.session(options.session);

0 commit comments

Comments
 (0)