Skip to content

Commit

Permalink
Merge pull request #277 from lenneTech/develop
Browse files Browse the repository at this point in the history
Release 9.0.30
  • Loading branch information
kaihaase authored Jan 24, 2023
2 parents 644454f + c2a2fb0 commit 7907fe7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lenne.tech/nest-server",
"version": "9.0.29",
"version": "9.0.30",
"description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).",
"keywords": [
"node",
Expand Down
10 changes: 0 additions & 10 deletions src/core/common/helpers/service.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,6 @@ export async function prepareInput<T = any>(
(input as any).password = await bcrypt.hash((input as any).password, 10);
}

// Set creator
if (config.create && currentUser) {
(input as Record<string, any>).createdBy = currentUser.id;
}

// Set updater
if (currentUser) {
(input as Record<string, any>).updatedBy = currentUser.id;
}

// Return prepared input
return input;
}
Expand Down
10 changes: 10 additions & 0 deletions src/core/common/interfaces/service-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export interface ServiceOptions {
// See https://www.mongodb.com/docs/manual/reference/collation/
collation?: CollationOptions;

// Create mode activated (see e.g. setCreateOrUpdateUserId)
// If falsy (default): create mode is deactivated
// If truly: create mode is activated
create?: boolean;

// Current user to set ownership, check rights and other things
currentUser?: {
[key: string]: any;
Expand Down Expand Up @@ -63,6 +68,11 @@ export interface ServiceOptions {
// Whether to publish action via GraphQL subscription
pubSub?: boolean;

// Add updateBy and/or createBy user ID into input after check
// If falsy: input data will not be changed
// If truly (default): updatedBy and/or createdBy (when create mode is activated) will be set if current user is available
setCreateOrUpdateUserId?: boolean;

// Roles (as string) to check
roles?: string | string[];
}
11 changes: 11 additions & 0 deletions src/core/common/services/module.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export abstract class ModuleService<T extends CoreModel = any> {
prepareInput: {},
prepareOutput: {},
pubSub: true,
setCreateOrUpdateUserId: true,
...options?.serviceOptions,
};

Expand Down Expand Up @@ -138,6 +139,16 @@ export abstract class ModuleService<T extends CoreModel = any> {
await this.checkRights(undefined, config.currentUser as any, config);
}

if (config.input && config.currentUser && config.setCreateOrUpdateUserId) {
// Set creator
if (config.create) {
(config.input as Record<string, any>).createdBy = config.currentUser.id;
}

// Set updater
(config.input as Record<string, any>).updatedBy = config.currentUser.id;
}

// Run service function
let result = await serviceFunc(config);

Expand Down

0 comments on commit 7907fe7

Please sign in to comment.