mongodb-dynamic-api 2.2.1 → 2.3.1
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/CHANGELOG.md +9 -0
- package/package.json +3 -1
- package/src/decorators/api-endpoint-visibility.decorator.d.ts +3 -0
- package/src/decorators/api-endpoint-visibility.decorator.js +10 -0
- package/src/decorators/index.d.ts +1 -0
- package/src/decorators/index.js +1 -0
- package/src/dynamic-api.module.js +25 -24
- package/src/guards/base-policies.guard.d.ts +28 -3
- package/src/guards/base-policies.guard.js +8 -7
- package/src/helpers/index.d.ts +2 -0
- package/src/helpers/index.js +2 -0
- package/src/interfaces/dynamic-api-ability.interface.d.ts +2 -2
- package/src/interfaces/dynamic-api-global-state.interface.d.ts +1 -0
- package/src/interfaces/dynamic-api-policy-handler.interface.d.ts +27 -3
- package/src/interfaces/dynamic-api-route-response.type.d.ts +7 -0
- package/src/interfaces/dynamic-api-route-response.type.js +2 -0
- package/src/interfaces/dynamic-api-service-callback.interface.d.ts +23 -3
- package/src/interfaces/index.d.ts +1 -0
- package/src/interfaces/index.js +1 -0
- package/src/mixins/create-policies-guard.mixin.d.ts +1 -2
- package/src/mixins/create-policies-guard.mixin.js +8 -5
- package/src/modules/auth/auth.helper.d.ts +3 -3
- package/src/modules/auth/auth.helper.js +4 -3
- package/src/modules/auth/auth.module.d.ts +1 -1
- package/src/modules/auth/auth.module.js +7 -3
- package/src/modules/auth/dtos/change-password.dto.d.ts +4 -0
- package/src/modules/auth/dtos/change-password.dto.js +29 -0
- package/src/modules/auth/dtos/reset-password.dto.d.ts +3 -0
- package/src/modules/auth/dtos/reset-password.dto.js +24 -0
- package/src/modules/auth/interfaces/auth-options.interface.d.ts +15 -9
- package/src/modules/auth/interfaces/auth-service.interface.d.ts +2 -1
- package/src/modules/auth/mixins/auth-controller.mixin.d.ts +2 -2
- package/src/modules/auth/mixins/auth-controller.mixin.js +28 -2
- package/src/modules/auth/mixins/auth-register-policies-guard.mixin.d.ts +2 -2
- package/src/modules/auth/services/base-auth.service.d.ts +6 -3
- package/src/modules/auth/services/base-auth.service.js +67 -9
- package/src/routes/create-many/base-create-many.service.js +1 -1
- package/src/routes/create-many/create-many-controller.mixin.js +1 -2
- package/src/routes/create-many/create-many.helper.js +1 -0
- package/src/routes/create-many/create-many.module.d.ts +1 -1
- package/src/routes/create-many/create-many.module.js +1 -1
- package/src/routes/create-one/base-create-one.service.js +1 -1
- package/src/routes/create-one/create-one-controller.mixin.js +1 -2
- package/src/routes/create-one/create-one.helper.js +1 -0
- package/src/routes/delete-many/delete-many-controller.interface.d.ts +2 -2
- package/src/routes/delete-many/delete-many-controller.mixin.js +1 -2
- package/src/routes/delete-many/delete-many-service.interface.d.ts +2 -2
- package/src/routes/delete-one/delete-one-controller.interface.d.ts +3 -2
- package/src/routes/delete-one/delete-one-controller.mixin.js +1 -2
- package/src/routes/delete-one/delete-one-service.interface.d.ts +3 -5
- package/src/routes/duplicate-many/base-duplicate-many.service.js +1 -1
- package/src/routes/duplicate-many/duplicate-many-controller.mixin.js +1 -2
- package/src/routes/duplicate-one/base-duplicate-one.service.js +1 -1
- package/src/routes/duplicate-one/duplicate-one-controller.mixin.js +1 -2
- package/src/routes/get-many/base-get-many.service.js +1 -1
- package/src/routes/get-many/get-many-controller.mixin.js +1 -2
- package/src/routes/get-one/base-get-one.service.js +1 -1
- package/src/routes/get-one/get-one-controller.mixin.js +1 -2
- package/src/routes/replace-one/base-replace-one.service.js +1 -1
- package/src/routes/replace-one/replace-one-controller.mixin.js +1 -2
- package/src/routes/update-many/base-update-many.service.js +1 -1
- package/src/routes/update-many/update-many-controller.mixin.js +1 -2
- package/src/routes/update-one/base-update-one.service.js +1 -1
- package/src/routes/update-one/update-one-controller.mixin.js +1 -2
- package/src/services/base/base.service.d.ts +17 -5
- package/src/services/base/base.service.js +61 -12
- package/src/services/dynamic-api-global-state/dynamic-api-global-state.service.d.ts +6 -4
- package/src/services/dynamic-api-global-state/dynamic-api-global-state.service.js +18 -12
- package/src/version.json +1 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -7,6 +7,7 @@ interface AuthService<Entity extends BaseEntity> {
|
|
|
7
7
|
login(user: Entity): Promise<LoginResponse>;
|
|
8
8
|
register(userToCreate: Partial<Entity>): Promise<LoginResponse>;
|
|
9
9
|
getAccount(user: Entity): Promise<Entity>;
|
|
10
|
-
|
|
10
|
+
resetPassword(email: string): Promise<void>;
|
|
11
|
+
changePassword(resetPasswordToken: string, newPassword: string): Promise<void>;
|
|
11
12
|
}
|
|
12
13
|
export type { AuthService, LoginResponse };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Type } from '@nestjs/common';
|
|
2
2
|
import { BaseEntity } from '../../../models';
|
|
3
|
-
import { AuthControllerConstructor, DynamicApiRegisterOptions } from '../interfaces';
|
|
4
|
-
declare function AuthControllerMixin<Entity extends BaseEntity>(userEntity: Type<Entity>, loginField: keyof Entity, passwordField: keyof Entity, additionalRequestFields?: (keyof Entity)[], { additionalFields: additionalRegisterFields, protected: registerProtected, abilityPredicate: registerAbilityPredicate, }?: DynamicApiRegisterOptions<Entity>): AuthControllerConstructor<Entity>;
|
|
3
|
+
import { AuthControllerConstructor, DynamicApiRegisterOptions, DynamicApiResetPasswordOptions } from '../interfaces';
|
|
4
|
+
declare function AuthControllerMixin<Entity extends BaseEntity>(userEntity: Type<Entity>, loginField: keyof Entity, passwordField: keyof Entity, additionalRequestFields?: (keyof Entity)[], { additionalFields: additionalRegisterFields, protected: registerProtected, abilityPredicate: registerAbilityPredicate, }?: DynamicApiRegisterOptions<Entity>, resetPasswordOptions?: DynamicApiResetPasswordOptions<Entity>): AuthControllerConstructor<Entity>;
|
|
5
5
|
export { AuthControllerMixin };
|
|
@@ -19,9 +19,11 @@ const class_validator_1 = require("class-validator");
|
|
|
19
19
|
const builders_1 = require("../../../builders");
|
|
20
20
|
const decorators_1 = require("../../../decorators");
|
|
21
21
|
const helpers_1 = require("../../../helpers");
|
|
22
|
+
const change_password_dto_1 = require("../dtos/change-password.dto");
|
|
23
|
+
const reset_password_dto_1 = require("../dtos/reset-password.dto");
|
|
22
24
|
const guards_1 = require("../guards");
|
|
23
25
|
const auth_register_policies_guard_mixin_1 = require("./auth-register-policies-guard.mixin");
|
|
24
|
-
function AuthControllerMixin(userEntity, loginField, passwordField, additionalRequestFields = [], { additionalFields: additionalRegisterFields, protected: registerProtected, abilityPredicate: registerAbilityPredicate, } = {}) {
|
|
26
|
+
function AuthControllerMixin(userEntity, loginField, passwordField, additionalRequestFields = [], { additionalFields: additionalRegisterFields, protected: registerProtected, abilityPredicate: registerAbilityPredicate, } = {}, resetPasswordOptions) {
|
|
25
27
|
var _a;
|
|
26
28
|
if (!loginField || !passwordField) {
|
|
27
29
|
throw new Error('Login and password fields are required');
|
|
@@ -77,12 +79,18 @@ function AuthControllerMixin(userEntity, loginField, passwordField, additionalRe
|
|
|
77
79
|
getAccount(req) {
|
|
78
80
|
return this.service.getAccount(req.user);
|
|
79
81
|
}
|
|
80
|
-
login(req,
|
|
82
|
+
login(req, _) {
|
|
81
83
|
return this.service.login(req.user);
|
|
82
84
|
}
|
|
83
85
|
register(body) {
|
|
84
86
|
return this.service.register(body);
|
|
85
87
|
}
|
|
88
|
+
resetPassword({ email }) {
|
|
89
|
+
return this.service.resetPassword(email);
|
|
90
|
+
}
|
|
91
|
+
changePassword({ resetPasswordToken, newPassword }) {
|
|
92
|
+
return this.service.changePassword(resetPasswordToken, newPassword);
|
|
93
|
+
}
|
|
86
94
|
}
|
|
87
95
|
__decorate([
|
|
88
96
|
(0, swagger_1.ApiBearerAuth)(),
|
|
@@ -118,6 +126,24 @@ function AuthControllerMixin(userEntity, loginField, passwordField, additionalRe
|
|
|
118
126
|
__metadata("design:paramtypes", [AuthRegisterDto]),
|
|
119
127
|
__metadata("design:returntype", void 0)
|
|
120
128
|
], BaseAuthController.prototype, "register", null);
|
|
129
|
+
__decorate([
|
|
130
|
+
(0, decorators_1.ApiEndpointVisibility)(!!resetPasswordOptions, (0, decorators_1.Public)()),
|
|
131
|
+
(0, common_1.HttpCode)(common_1.HttpStatus.NO_CONTENT),
|
|
132
|
+
(0, common_1.Post)('reset-password'),
|
|
133
|
+
__param(0, (0, common_1.Body)()),
|
|
134
|
+
__metadata("design:type", Function),
|
|
135
|
+
__metadata("design:paramtypes", [reset_password_dto_1.ResetPasswordDto]),
|
|
136
|
+
__metadata("design:returntype", void 0)
|
|
137
|
+
], BaseAuthController.prototype, "resetPassword", null);
|
|
138
|
+
__decorate([
|
|
139
|
+
(0, decorators_1.ApiEndpointVisibility)(!!resetPasswordOptions, (0, decorators_1.Public)()),
|
|
140
|
+
(0, common_1.HttpCode)(common_1.HttpStatus.NO_CONTENT),
|
|
141
|
+
(0, common_1.Patch)('change-password'),
|
|
142
|
+
__param(0, (0, common_1.Body)()),
|
|
143
|
+
__metadata("design:type", Function),
|
|
144
|
+
__metadata("design:paramtypes", [change_password_dto_1.ChangePasswordDto]),
|
|
145
|
+
__metadata("design:returntype", void 0)
|
|
146
|
+
], BaseAuthController.prototype, "changePassword", null);
|
|
121
147
|
return BaseAuthController;
|
|
122
148
|
}
|
|
123
149
|
exports.AuthControllerMixin = AuthControllerMixin;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Type } from '@nestjs/common';
|
|
2
|
-
import { PoliciesGuardConstructor,
|
|
2
|
+
import { PoliciesGuardConstructor, AuthAbilityPredicate } from '../../../interfaces';
|
|
3
3
|
import { BaseEntity } from '../../../models';
|
|
4
|
-
declare function AuthRegisterPoliciesGuardMixin<Entity extends BaseEntity>(entity: Type<Entity>, abilityPredicate:
|
|
4
|
+
declare function AuthRegisterPoliciesGuardMixin<Entity extends BaseEntity>(entity: Type<Entity>, abilityPredicate: AuthAbilityPredicate | undefined): PoliciesGuardConstructor<Entity>;
|
|
5
5
|
export { AuthRegisterPoliciesGuardMixin };
|
|
@@ -27,6 +27,7 @@ import { Model } from 'mongoose';
|
|
|
27
27
|
import { DynamicApiServiceCallback } from '../../../interfaces';
|
|
28
28
|
import { BaseEntity } from '../../../models';
|
|
29
29
|
import { BaseService, BcryptService } from '../../../services';
|
|
30
|
+
import { DynamicApiResetPasswordOptions } from '../interfaces';
|
|
30
31
|
export declare abstract class BaseAuthService<Entity extends BaseEntity> extends BaseService<Entity> {
|
|
31
32
|
protected readonly model: Model<Entity>;
|
|
32
33
|
protected readonly jwtService: JwtService;
|
|
@@ -36,6 +37,9 @@ export declare abstract class BaseAuthService<Entity extends BaseEntity> extends
|
|
|
36
37
|
protected additionalRequestFields: (keyof Entity)[];
|
|
37
38
|
protected registerCallback: DynamicApiServiceCallback<Entity> | undefined;
|
|
38
39
|
protected loginCallback: DynamicApiServiceCallback<Entity> | undefined;
|
|
40
|
+
protected resetPasswordOptions: DynamicApiResetPasswordOptions<Entity> | undefined;
|
|
41
|
+
private resetPasswordCallbackMethods;
|
|
42
|
+
private readonly logger;
|
|
39
43
|
protected constructor(model: Model<Entity>, jwtService: JwtService, bcryptService: BcryptService);
|
|
40
44
|
protected validateUser(login: string, pass: string): Promise<Entity>;
|
|
41
45
|
protected login(user: Entity, fromMember?: boolean): Promise<{
|
|
@@ -45,8 +49,7 @@ export declare abstract class BaseAuthService<Entity extends BaseEntity> extends
|
|
|
45
49
|
accessToken: string;
|
|
46
50
|
}>;
|
|
47
51
|
protected getAccount({ id }: Entity): Promise<Entity>;
|
|
48
|
-
protected
|
|
49
|
-
|
|
50
|
-
}>;
|
|
52
|
+
protected resetPassword(email: string): Promise<void>;
|
|
53
|
+
protected changePassword(resetPasswordToken: string, newPassword: string): Promise<void>;
|
|
51
54
|
private buildUserFields;
|
|
52
55
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BaseAuthService = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
4
5
|
const services_1 = require("../../../services");
|
|
5
6
|
class BaseAuthService extends services_1.BaseService {
|
|
6
7
|
constructor(model, jwtService, bcryptService) {
|
|
@@ -11,6 +12,7 @@ class BaseAuthService extends services_1.BaseService {
|
|
|
11
12
|
this.loginField = 'email';
|
|
12
13
|
this.passwordField = 'password';
|
|
13
14
|
this.additionalRequestFields = [];
|
|
15
|
+
this.logger = new common_1.Logger('AuthService');
|
|
14
16
|
}
|
|
15
17
|
async validateUser(login, pass) {
|
|
16
18
|
const user = (await this.model.findOne({ [this.loginField]: login }).lean().exec());
|
|
@@ -33,19 +35,19 @@ class BaseAuthService extends services_1.BaseService {
|
|
|
33
35
|
];
|
|
34
36
|
const payload = this.buildUserFields(user, fieldsToBuild);
|
|
35
37
|
if (!fromMember && this.loginCallback) {
|
|
36
|
-
await this.loginCallback(payload, this.
|
|
38
|
+
await this.loginCallback(payload, this.callbackMethods);
|
|
37
39
|
}
|
|
38
40
|
return {
|
|
39
41
|
accessToken: this.jwtService.sign(payload),
|
|
40
42
|
};
|
|
41
43
|
}
|
|
42
44
|
async register(userToCreate) {
|
|
43
|
-
const hashedPassword = await this.bcryptService.hashPassword(userToCreate[this.passwordField]);
|
|
44
45
|
try {
|
|
46
|
+
const hashedPassword = await this.bcryptService.hashPassword(userToCreate[this.passwordField]);
|
|
45
47
|
const { _id } = await this.model.create({ ...userToCreate, [this.passwordField]: hashedPassword });
|
|
46
|
-
const user = await this.
|
|
48
|
+
const user = await this.findOneDocumentWithAbilityPredicate(_id);
|
|
47
49
|
if (this.registerCallback) {
|
|
48
|
-
await this.registerCallback(user, this.
|
|
50
|
+
await this.registerCallback(user, this.callbackMethods);
|
|
49
51
|
}
|
|
50
52
|
return this.login(user, true);
|
|
51
53
|
}
|
|
@@ -54,7 +56,7 @@ class BaseAuthService extends services_1.BaseService {
|
|
|
54
56
|
}
|
|
55
57
|
}
|
|
56
58
|
async getAccount({ id }) {
|
|
57
|
-
const user = await this.
|
|
59
|
+
const user = await this.findOneDocumentWithAbilityPredicate(id);
|
|
58
60
|
const fieldsToBuild = [
|
|
59
61
|
'_id',
|
|
60
62
|
this.loginField,
|
|
@@ -62,11 +64,67 @@ class BaseAuthService extends services_1.BaseService {
|
|
|
62
64
|
];
|
|
63
65
|
return this.buildUserFields(user, fieldsToBuild);
|
|
64
66
|
}
|
|
65
|
-
async
|
|
67
|
+
async resetPassword(email) {
|
|
68
|
+
if (!this.resetPasswordOptions) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
this.resetPasswordCallbackMethods = {
|
|
72
|
+
findUserByEmail: async (email) => {
|
|
73
|
+
const user = await this.model.findOne({ [this.resetPasswordOptions.emailField]: email })
|
|
74
|
+
.lean()
|
|
75
|
+
.exec();
|
|
76
|
+
if (!user) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
return this.buildInstance(user);
|
|
80
|
+
},
|
|
81
|
+
updateUserByEmail: async (email, data) => {
|
|
82
|
+
const user = await this.model.findOneAndUpdate({ [this.resetPasswordOptions.emailField]: email }, data, { new: true }).lean().exec();
|
|
83
|
+
if (!user) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
return this.buildInstance(user);
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
const { resetPasswordCallback, expirationInMinutes } = this.resetPasswordOptions;
|
|
90
|
+
const resetPasswordToken = this.jwtService.sign({ email }, { expiresIn: expirationInMinutes * 60 });
|
|
91
|
+
await resetPasswordCallback({ resetPasswordToken, email }, this.resetPasswordCallbackMethods);
|
|
92
|
+
}
|
|
93
|
+
async changePassword(resetPasswordToken, newPassword) {
|
|
94
|
+
let email;
|
|
95
|
+
let exp;
|
|
96
|
+
try {
|
|
97
|
+
const decoded = this.jwtService.decode(resetPasswordToken);
|
|
98
|
+
email = decoded.email;
|
|
99
|
+
exp = decoded.exp;
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
this.logger.warn('Invalid reset password token');
|
|
103
|
+
}
|
|
104
|
+
if (!email || !exp) {
|
|
105
|
+
throw new common_1.BadRequestException('Invalid reset password token. Please redo the reset password process.');
|
|
106
|
+
}
|
|
107
|
+
const now = Math.round(Date.now() / 1000);
|
|
108
|
+
if (exp <= now) {
|
|
109
|
+
throw new common_1.UnauthorizedException('Time to reset password has expired. Please redo the reset password process.');
|
|
110
|
+
}
|
|
111
|
+
let userId;
|
|
112
|
+
try {
|
|
113
|
+
const { _id } = await this.findOneDocumentWithAbilityPredicate(undefined, { [this.resetPasswordOptions.emailField]: email });
|
|
114
|
+
userId = _id.toString();
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
this.logger.warn('Invalid email, user not found');
|
|
118
|
+
}
|
|
119
|
+
if (!userId) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
66
122
|
const hashedPassword = await this.bcryptService.hashPassword(newPassword);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
123
|
+
await this.model.updateOne({ _id: userId }, { [this.passwordField]: hashedPassword });
|
|
124
|
+
if (this.resetPasswordOptions?.changePasswordCallback) {
|
|
125
|
+
const user = await this.findOneDocumentWithAbilityPredicate(userId);
|
|
126
|
+
await this.resetPasswordOptions.changePasswordCallback(this.buildInstance(user), this.callbackMethods);
|
|
127
|
+
}
|
|
70
128
|
}
|
|
71
129
|
buildUserFields(user, fieldsToBuild) {
|
|
72
130
|
return this.buildInstance(fieldsToBuild.reduce((acc, field) => (user[field] !== undefined ? { ...acc, [field]: user[field] } : acc), {}));
|
|
@@ -15,7 +15,7 @@ class BaseCreateManyService extends services_1.BaseService {
|
|
|
15
15
|
.lean()
|
|
16
16
|
.exec();
|
|
17
17
|
if (this.callback && documents.length) {
|
|
18
|
-
await Promise.all(documents.map((document) => this.callback(document, this.
|
|
18
|
+
await Promise.all(documents.map((document) => this.callback(document, this.callbackMethods)));
|
|
19
19
|
}
|
|
20
20
|
return documents.map((d) => this.buildInstance(d));
|
|
21
21
|
}
|
|
@@ -16,11 +16,10 @@ exports.CreateManyControllerMixin = void 0;
|
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
17
|
const builders_1 = require("../../builders");
|
|
18
18
|
const helpers_1 = require("../../helpers");
|
|
19
|
-
const controller_mixin_helper_1 = require("../../helpers/controller-mixin.helper");
|
|
20
19
|
const mixins_1 = require("../../mixins");
|
|
21
20
|
function CreateManyControllerMixin(entity, controllerOptions, routeConfig, version) {
|
|
22
21
|
var _a;
|
|
23
|
-
const { routeType, description, isPublic, RouteBody, RoutePresenter, abilityPredicate, } = (0,
|
|
22
|
+
const { routeType, description, isPublic, RouteBody, RoutePresenter, abilityPredicate, } = (0, helpers_1.getControllerMixinData)(entity, controllerOptions, routeConfig, version);
|
|
24
23
|
const routeDecoratorsBuilder = new builders_1.RouteDecoratorsBuilder(routeType, entity, version, description, isPublic, {
|
|
25
24
|
body: RouteBody,
|
|
26
25
|
presenter: RoutePresenter,
|
|
@@ -2,5 +2,5 @@ import { DynamicModule, Type, ValidationPipeOptions } from '@nestjs/common';
|
|
|
2
2
|
import { DynamicApiControllerOptions, DynamicAPIRouteConfig } from '../../interfaces';
|
|
3
3
|
import { BaseEntity } from '../../models';
|
|
4
4
|
export declare class CreateManyModule {
|
|
5
|
-
static forFeature<Entity extends BaseEntity>(databaseModule: DynamicModule, entity: Type<Entity>, controllerOptions: DynamicApiControllerOptions<Entity>, routeConfig: DynamicAPIRouteConfig<Entity>, version?: string, validationPipeOptions?: ValidationPipeOptions):
|
|
5
|
+
static forFeature<Entity extends BaseEntity>(databaseModule: DynamicModule, entity: Type<Entity>, controllerOptions: DynamicApiControllerOptions<Entity>, routeConfig: DynamicAPIRouteConfig<Entity>, version?: string, validationPipeOptions?: ValidationPipeOptions): DynamicModule;
|
|
6
6
|
}
|
|
@@ -11,7 +11,7 @@ exports.CreateManyModule = void 0;
|
|
|
11
11
|
const common_1 = require("@nestjs/common");
|
|
12
12
|
const create_many_helper_1 = require("./create-many.helper");
|
|
13
13
|
let CreateManyModule = CreateManyModule_1 = class CreateManyModule {
|
|
14
|
-
static
|
|
14
|
+
static forFeature(databaseModule, entity, controllerOptions, routeConfig, version, validationPipeOptions) {
|
|
15
15
|
const controller = (0, create_many_helper_1.createCreateManyController)(entity, controllerOptions, routeConfig, version, validationPipeOptions);
|
|
16
16
|
const ServiceProvider = (0, create_many_helper_1.createCreateManyServiceProvider)(entity, version, routeConfig.callback);
|
|
17
17
|
return {
|
|
@@ -12,7 +12,7 @@ class BaseCreateOneService extends services_1.BaseService {
|
|
|
12
12
|
const { _id } = await this.model.create(partial);
|
|
13
13
|
const document = await this.model.findOne({ _id }).lean().exec();
|
|
14
14
|
if (this.callback) {
|
|
15
|
-
await this.callback(document, this.
|
|
15
|
+
await this.callback(document, this.callbackMethods);
|
|
16
16
|
}
|
|
17
17
|
return this.buildInstance(document);
|
|
18
18
|
}
|
|
@@ -16,11 +16,10 @@ exports.CreateOneControllerMixin = void 0;
|
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
17
|
const builders_1 = require("../../builders");
|
|
18
18
|
const helpers_1 = require("../../helpers");
|
|
19
|
-
const controller_mixin_helper_1 = require("../../helpers/controller-mixin.helper");
|
|
20
19
|
const mixins_1 = require("../../mixins");
|
|
21
20
|
function CreateOneControllerMixin(entity, controllerOptions, routeConfig, version) {
|
|
22
21
|
var _a;
|
|
23
|
-
const { routeType, description, isPublic, RouteBody, RoutePresenter, abilityPredicate, } = (0,
|
|
22
|
+
const { routeType, description, isPublic, RouteBody, RoutePresenter, abilityPredicate, } = (0, helpers_1.getControllerMixinData)(entity, controllerOptions, routeConfig, version);
|
|
24
23
|
const routeDecoratorsBuilder = new builders_1.RouteDecoratorsBuilder(routeType, entity, version, description, isPublic, {
|
|
25
24
|
body: RouteBody,
|
|
26
25
|
presenter: RoutePresenter,
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import { DeleteResult } from '../../interfaces';
|
|
1
2
|
import { BaseEntity } from '../../models';
|
|
2
|
-
import { DeletedCount } from '../delete-one';
|
|
3
3
|
import { DeleteManyService } from './delete-many-service.interface';
|
|
4
4
|
interface DeleteManyController<Entity extends BaseEntity> {
|
|
5
|
-
deleteMany(ids: string[]): Promise<
|
|
5
|
+
deleteMany(ids: string[]): Promise<DeleteResult>;
|
|
6
6
|
}
|
|
7
7
|
type DeleteManyControllerConstructor<Entity extends BaseEntity> = new (service: DeleteManyService<Entity>) => DeleteManyController<Entity>;
|
|
8
8
|
export type { DeleteManyController, DeleteManyControllerConstructor };
|
|
@@ -16,10 +16,9 @@ exports.DeleteManyControllerMixin = void 0;
|
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
17
|
const builders_1 = require("../../builders");
|
|
18
18
|
const helpers_1 = require("../../helpers");
|
|
19
|
-
const controller_mixin_helper_1 = require("../../helpers/controller-mixin.helper");
|
|
20
19
|
const mixins_1 = require("../../mixins");
|
|
21
20
|
function DeleteManyControllerMixin(entity, controllerOptions, routeConfig, version) {
|
|
22
|
-
const { routeType, description, isPublic, RoutePresenter, abilityPredicate, } = (0,
|
|
21
|
+
const { routeType, description, isPublic, RoutePresenter, abilityPredicate, } = (0, helpers_1.getControllerMixinData)(entity, controllerOptions, routeConfig, version);
|
|
23
22
|
const routeDecoratorsBuilder = new builders_1.RouteDecoratorsBuilder(routeType, entity, version, description, isPublic, {
|
|
24
23
|
presenter: RoutePresenter,
|
|
25
24
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { DeleteResult } from '../../interfaces';
|
|
1
2
|
import { BaseEntity } from '../../models';
|
|
2
|
-
import { DeletedCount } from '../delete-one';
|
|
3
3
|
interface DeleteManyService<Entity extends BaseEntity> {
|
|
4
|
-
deleteMany(ids: string[]): Promise<
|
|
4
|
+
deleteMany(ids: string[]): Promise<DeleteResult>;
|
|
5
5
|
}
|
|
6
6
|
export type { DeleteManyService };
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { DeleteResult } from '../../interfaces';
|
|
1
2
|
import { BaseEntity } from '../../models';
|
|
2
|
-
import {
|
|
3
|
+
import { DeleteOneService } from './delete-one-service.interface';
|
|
3
4
|
interface DeleteOneController<Entity extends BaseEntity> {
|
|
4
|
-
deleteOne(id: string): Promise<
|
|
5
|
+
deleteOne(id: string): Promise<DeleteResult>;
|
|
5
6
|
}
|
|
6
7
|
type DeleteOneControllerConstructor<Entity extends BaseEntity> = new (service: DeleteOneService<Entity>) => DeleteOneController<Entity>;
|
|
7
8
|
export type { DeleteOneController, DeleteOneControllerConstructor };
|
|
@@ -16,10 +16,9 @@ exports.DeleteOneControllerMixin = void 0;
|
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
17
|
const builders_1 = require("../../builders");
|
|
18
18
|
const helpers_1 = require("../../helpers");
|
|
19
|
-
const controller_mixin_helper_1 = require("../../helpers/controller-mixin.helper");
|
|
20
19
|
const mixins_1 = require("../../mixins");
|
|
21
20
|
function DeleteOneControllerMixin(entity, controllerOptions, routeConfig, version) {
|
|
22
|
-
const { routeType, description, isPublic, EntityParam, RoutePresenter, abilityPredicate, } = (0,
|
|
21
|
+
const { routeType, description, isPublic, EntityParam, RoutePresenter, abilityPredicate, } = (0, helpers_1.getControllerMixinData)(entity, controllerOptions, routeConfig, version);
|
|
23
22
|
const routeDecoratorsBuilder = new builders_1.RouteDecoratorsBuilder(routeType, entity, version, description, isPublic, {
|
|
24
23
|
param: EntityParam,
|
|
25
24
|
presenter: RoutePresenter,
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
+
import { DeleteResult } from '../../interfaces';
|
|
1
2
|
import { BaseEntity } from '../../models';
|
|
2
|
-
type DeletedCount = {
|
|
3
|
-
deletedCount: number;
|
|
4
|
-
};
|
|
5
3
|
interface DeleteOneService<Entity extends BaseEntity> {
|
|
6
|
-
deleteOne(id: string): Promise<
|
|
4
|
+
deleteOne(id: string): Promise<DeleteResult>;
|
|
7
5
|
}
|
|
8
|
-
export type {
|
|
6
|
+
export type { DeleteOneService };
|
|
@@ -33,7 +33,7 @@ class BaseDuplicateManyService extends services_1.BaseService {
|
|
|
33
33
|
.lean()
|
|
34
34
|
.exec();
|
|
35
35
|
if (this.callback && documents.length) {
|
|
36
|
-
await Promise.all(documents.map((document) => this.callback(document, this.
|
|
36
|
+
await Promise.all(documents.map((document) => this.callback(document, this.callbackMethods)));
|
|
37
37
|
}
|
|
38
38
|
return documents.map((d) => this.buildInstance(d));
|
|
39
39
|
}
|
|
@@ -16,11 +16,10 @@ exports.DuplicateManyControllerMixin = void 0;
|
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
17
|
const builders_1 = require("../../builders");
|
|
18
18
|
const helpers_1 = require("../../helpers");
|
|
19
|
-
const controller_mixin_helper_1 = require("../../helpers/controller-mixin.helper");
|
|
20
19
|
const mixins_1 = require("../../mixins");
|
|
21
20
|
function DuplicateManyControllerMixin(entity, controllerOptions, routeConfig, version) {
|
|
22
21
|
var _a;
|
|
23
|
-
const { routeType, description, isPublic, RouteBody, RoutePresenter, abilityPredicate, } = (0,
|
|
22
|
+
const { routeType, description, isPublic, RouteBody, RoutePresenter, abilityPredicate, } = (0, helpers_1.getControllerMixinData)(entity, controllerOptions, routeConfig, version);
|
|
24
23
|
const routeDecoratorsBuilder = new builders_1.RouteDecoratorsBuilder(routeType, entity, version, description, isPublic, {
|
|
25
24
|
body: RouteBody,
|
|
26
25
|
presenter: RoutePresenter,
|
|
@@ -31,7 +31,7 @@ class BaseDuplicateOneService extends services_1.BaseService {
|
|
|
31
31
|
});
|
|
32
32
|
const document = await this.model.findOne({ _id }).lean().exec();
|
|
33
33
|
if (this.callback) {
|
|
34
|
-
await this.callback(document, this.
|
|
34
|
+
await this.callback(document, this.callbackMethods);
|
|
35
35
|
}
|
|
36
36
|
return this.buildInstance(document);
|
|
37
37
|
}
|
|
@@ -17,11 +17,10 @@ const common_1 = require("@nestjs/common");
|
|
|
17
17
|
const builders_1 = require("../../builders");
|
|
18
18
|
const dtos_1 = require("../../dtos");
|
|
19
19
|
const helpers_1 = require("../../helpers");
|
|
20
|
-
const controller_mixin_helper_1 = require("../../helpers/controller-mixin.helper");
|
|
21
20
|
const mixins_1 = require("../../mixins");
|
|
22
21
|
function DuplicateOneControllerMixin(entity, controllerOptions, routeConfig, version) {
|
|
23
22
|
var _a;
|
|
24
|
-
const { routeType, description, isPublic, RouteBody, RoutePresenter, abilityPredicate, } = (0,
|
|
23
|
+
const { routeType, description, isPublic, RouteBody, RoutePresenter, abilityPredicate, } = (0, helpers_1.getControllerMixinData)(entity, controllerOptions, routeConfig, version);
|
|
25
24
|
const routeDecoratorsBuilder = new builders_1.RouteDecoratorsBuilder('DuplicateOne', entity, version, description, isPublic, {
|
|
26
25
|
param: dtos_1.EntityParam,
|
|
27
26
|
body: RouteBody,
|
|
@@ -16,7 +16,7 @@ class BaseGetManyService extends services_1.BaseService {
|
|
|
16
16
|
.lean()
|
|
17
17
|
.exec();
|
|
18
18
|
if (this.callback && documents.length) {
|
|
19
|
-
await Promise.all(documents.map((document) => this.callback(document, this.
|
|
19
|
+
await Promise.all(documents.map((document) => this.callback(document, this.callbackMethods)));
|
|
20
20
|
}
|
|
21
21
|
return documents.map((d) => this.buildInstance(d));
|
|
22
22
|
}
|
|
@@ -17,10 +17,9 @@ const common_1 = require("@nestjs/common");
|
|
|
17
17
|
const builders_1 = require("../../builders");
|
|
18
18
|
const dtos_1 = require("../../dtos");
|
|
19
19
|
const helpers_1 = require("../../helpers");
|
|
20
|
-
const controller_mixin_helper_1 = require("../../helpers/controller-mixin.helper");
|
|
21
20
|
const mixins_1 = require("../../mixins");
|
|
22
21
|
function GetManyControllerMixin(entity, controllerOptions, routeConfig, version) {
|
|
23
|
-
const { routeType, description, isPublic, RoutePresenter, abilityPredicate, } = (0,
|
|
22
|
+
const { routeType, description, isPublic, RoutePresenter, abilityPredicate, } = (0, helpers_1.getControllerMixinData)(entity, controllerOptions, routeConfig, version);
|
|
24
23
|
class RouteQuery extends (routeConfig.dTOs?.query ?? dtos_1.EntityQuery) {
|
|
25
24
|
}
|
|
26
25
|
Object.defineProperty(RouteQuery, 'name', {
|
|
@@ -19,7 +19,7 @@ class BaseGetOneService extends services_1.BaseService {
|
|
|
19
19
|
this.handleDocumentNotFound();
|
|
20
20
|
}
|
|
21
21
|
if (this.callback) {
|
|
22
|
-
await this.callback(document, this.
|
|
22
|
+
await this.callback(document, this.callbackMethods);
|
|
23
23
|
}
|
|
24
24
|
return this.buildInstance(document);
|
|
25
25
|
}
|
|
@@ -17,10 +17,9 @@ const common_1 = require("@nestjs/common");
|
|
|
17
17
|
const builders_1 = require("../../builders");
|
|
18
18
|
const dtos_1 = require("../../dtos");
|
|
19
19
|
const helpers_1 = require("../../helpers");
|
|
20
|
-
const controller_mixin_helper_1 = require("../../helpers/controller-mixin.helper");
|
|
21
20
|
const mixins_1 = require("../../mixins");
|
|
22
21
|
function GetOneControllerMixin(entity, controllerOptions, routeConfig, version) {
|
|
23
|
-
const { routeType, description, isPublic, RoutePresenter, abilityPredicate, } = (0,
|
|
22
|
+
const { routeType, description, isPublic, RoutePresenter, abilityPredicate, } = (0, helpers_1.getControllerMixinData)(entity, controllerOptions, routeConfig, version);
|
|
24
23
|
const routeDecoratorsBuilder = new builders_1.RouteDecoratorsBuilder(routeType, entity, version, description, isPublic, {
|
|
25
24
|
param: dtos_1.EntityParam,
|
|
26
25
|
presenter: RoutePresenter,
|
|
@@ -23,7 +23,7 @@ class BaseReplaceOneService extends services_1.BaseService {
|
|
|
23
23
|
this.handleDocumentNotFound();
|
|
24
24
|
}
|
|
25
25
|
if (this.callback) {
|
|
26
|
-
await this.callback(document, this.
|
|
26
|
+
await this.callback(document, this.callbackMethods);
|
|
27
27
|
}
|
|
28
28
|
return this.buildInstance(document);
|
|
29
29
|
}
|
|
@@ -16,11 +16,10 @@ exports.ReplaceOneControllerMixin = void 0;
|
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
17
|
const builders_1 = require("../../builders");
|
|
18
18
|
const helpers_1 = require("../../helpers");
|
|
19
|
-
const controller_mixin_helper_1 = require("../../helpers/controller-mixin.helper");
|
|
20
19
|
const mixins_1 = require("../../mixins");
|
|
21
20
|
function ReplaceOneControllerMixin(entity, controllerOptions, routeConfig, version) {
|
|
22
21
|
var _a;
|
|
23
|
-
const { routeType, description, isPublic, EntityParam, RouteBody, RoutePresenter, abilityPredicate, } = (0,
|
|
22
|
+
const { routeType, description, isPublic, EntityParam, RouteBody, RoutePresenter, abilityPredicate, } = (0, helpers_1.getControllerMixinData)(entity, controllerOptions, routeConfig, version);
|
|
24
23
|
const routeDecoratorsBuilder = new builders_1.RouteDecoratorsBuilder(routeType, entity, version, description, isPublic, {
|
|
25
24
|
param: EntityParam,
|
|
26
25
|
body: RouteBody,
|
|
@@ -22,7 +22,7 @@ class BaseUpdateManyService extends services_1.BaseService {
|
|
|
22
22
|
.exec();
|
|
23
23
|
const documents = await this.model.find({ _id: { $in: ids } }).lean().exec();
|
|
24
24
|
if (this.callback && documents.length) {
|
|
25
|
-
await Promise.all(documents.map((document) => this.callback(document, this.
|
|
25
|
+
await Promise.all(documents.map((document) => this.callback(document, this.callbackMethods)));
|
|
26
26
|
}
|
|
27
27
|
return documents.map((d) => this.buildInstance(d));
|
|
28
28
|
}
|
|
@@ -16,11 +16,10 @@ exports.UpdateManyControllerMixin = void 0;
|
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
17
|
const builders_1 = require("../../builders");
|
|
18
18
|
const helpers_1 = require("../../helpers");
|
|
19
|
-
const controller_mixin_helper_1 = require("../../helpers/controller-mixin.helper");
|
|
20
19
|
const mixins_1 = require("../../mixins");
|
|
21
20
|
function UpdateManyControllerMixin(entity, controllerOptions, routeConfig, version) {
|
|
22
21
|
var _a;
|
|
23
|
-
const { routeType, description, isPublic, RouteBody, RoutePresenter, abilityPredicate, } = (0,
|
|
22
|
+
const { routeType, description, isPublic, RouteBody, RoutePresenter, abilityPredicate, } = (0, helpers_1.getControllerMixinData)(entity, controllerOptions, routeConfig, version);
|
|
24
23
|
const routeDecoratorsBuilder = new builders_1.RouteDecoratorsBuilder(routeType, entity, version, description, isPublic, {
|
|
25
24
|
body: RouteBody,
|
|
26
25
|
presenter: RoutePresenter,
|
|
@@ -20,7 +20,7 @@ class BaseUpdateOneService extends services_1.BaseService {
|
|
|
20
20
|
this.handleDocumentNotFound();
|
|
21
21
|
}
|
|
22
22
|
if (this.callback) {
|
|
23
|
-
await this.callback(document, this.
|
|
23
|
+
await this.callback(document, this.callbackMethods);
|
|
24
24
|
}
|
|
25
25
|
return this.buildInstance(document);
|
|
26
26
|
}
|
|
@@ -16,11 +16,10 @@ exports.UpdateOneControllerMixin = void 0;
|
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
17
|
const builders_1 = require("../../builders");
|
|
18
18
|
const helpers_1 = require("../../helpers");
|
|
19
|
-
const controller_mixin_helper_1 = require("../../helpers/controller-mixin.helper");
|
|
20
19
|
const mixins_1 = require("../../mixins");
|
|
21
20
|
function UpdateOneControllerMixin(entity, controllerOptions, routeConfig, version) {
|
|
22
21
|
var _a;
|
|
23
|
-
const { routeType, description, isPublic, EntityParam, RouteBody, RoutePresenter, abilityPredicate, } = (0,
|
|
22
|
+
const { routeType, description, isPublic, EntityParam, RouteBody, RoutePresenter, abilityPredicate, } = (0, helpers_1.getControllerMixinData)(entity, controllerOptions, routeConfig, version);
|
|
24
23
|
const routeDecoratorsBuilder = new builders_1.RouteDecoratorsBuilder(routeType, entity, version, description, isPublic, {
|
|
25
24
|
param: EntityParam,
|
|
26
25
|
body: RouteBody,
|
|
@@ -24,17 +24,29 @@
|
|
|
24
24
|
/// <reference types="mongoose/types/inferschematype" />
|
|
25
25
|
import { Type } from '@nestjs/common';
|
|
26
26
|
import { FilterQuery, Model, Schema } from 'mongoose';
|
|
27
|
-
import { AbilityPredicate } from '../../interfaces';
|
|
27
|
+
import { AbilityPredicate, DeleteResult, DynamicApiCallbackMethods, UpdateResult } from '../../interfaces';
|
|
28
28
|
import { BaseEntity } from '../../models';
|
|
29
|
+
import { DynamicApiResetPasswordOptions } from '../../modules';
|
|
29
30
|
export declare abstract class BaseService<Entity extends BaseEntity> {
|
|
30
31
|
protected readonly model: Model<Entity>;
|
|
31
|
-
|
|
32
|
-
user: unknown;
|
|
32
|
+
protected user: unknown;
|
|
33
33
|
protected readonly entity: Type<Entity>;
|
|
34
|
+
protected readonly abilityPredicate: AbilityPredicate<Entity> | undefined;
|
|
35
|
+
protected readonly passwordField: keyof Entity | undefined;
|
|
36
|
+
protected readonly resetPasswordOptions: DynamicApiResetPasswordOptions<Entity> | undefined;
|
|
37
|
+
protected readonly callbackMethods: DynamicApiCallbackMethods;
|
|
34
38
|
protected constructor(model: Model<Entity>);
|
|
35
39
|
get isSoftDeletable(): boolean;
|
|
36
|
-
|
|
37
|
-
|
|
40
|
+
protected findManyDocumentsWithAbilityPredicate(conditions?: FilterQuery<Entity>): Promise<Entity[]>;
|
|
41
|
+
protected findOneDocumentWithAbilityPredicate(_id: string | Schema.Types.ObjectId | undefined, conditions?: FilterQuery<Entity>): Promise<Entity>;
|
|
42
|
+
protected findManyDocuments<T>(entity: Type<T>, query: FilterQuery<T>): Promise<T[]>;
|
|
43
|
+
protected findOneDocument<T>(entity: Type<T>, query: FilterQuery<T>): Promise<T | undefined>;
|
|
44
|
+
protected createManyDocuments<T>(entity: Type<T>, data: Partial<T>[]): Promise<T[]>;
|
|
45
|
+
protected createOneDocument<T>(entity: Type<T>, data: Partial<T>): Promise<T>;
|
|
46
|
+
protected updateManyDocuments<T>(entity: Type<T>, query: FilterQuery<T>, data: Partial<T>): Promise<UpdateResult>;
|
|
47
|
+
protected updateOneDocument<T>(entity: Type<T>, query: FilterQuery<T>, data: Partial<T>): Promise<UpdateResult>;
|
|
48
|
+
protected deleteManyDocuments<T>(entity: Type<T>, ids: string[]): Promise<DeleteResult>;
|
|
49
|
+
protected deleteOneDocument<T>(entity: Type<T>, id: string): Promise<DeleteResult>;
|
|
38
50
|
protected buildInstance(document: Entity): Entity;
|
|
39
51
|
protected handleAbilityPredicate(document: Entity): void;
|
|
40
52
|
protected handleDuplicateKeyError(error: any): void;
|