-
Notifications
You must be signed in to change notification settings - Fork 81
feat(core): restrict modification of createdon and modifiedon #2188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
397baec
0efe67c
48af493
c93bf79
9845d70
091897b
248d670
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ import {AuthErrorKeys} from 'loopback4-authentication'; | |
| import {DefaultTransactionSoftCrudRepository} from 'loopback4-soft-delete'; | ||
| import {IAuthUserWithPermissions} from '../components'; | ||
| import {UserModifiableEntity} from '../models'; | ||
| import {RepositoryOverridingOptions} from '../types'; | ||
|
|
||
| export abstract class DefaultTransactionalUserModifyRepository< | ||
| T extends UserModifiableEntity, | ||
|
|
@@ -34,6 +35,8 @@ export abstract class DefaultTransactionalUserModifyRepository< | |
| super(entityClass, dataSource); | ||
| } | ||
|
|
||
| public overridingOptions?: RepositoryOverridingOptions; | ||
|
||
|
|
||
| async create(entity: DataObject<T>, options?: Options): Promise<T> { | ||
| let currentUser = await this.getCurrentUser(); | ||
| currentUser = currentUser ?? options?.currentUser; | ||
|
|
@@ -43,6 +46,10 @@ export abstract class DefaultTransactionalUserModifyRepository< | |
| const uid = currentUser?.userTenantId ?? currentUser?.id; | ||
| entity.createdBy = uid; | ||
| entity.modifiedBy = uid; | ||
| if (this.overridingOptions?.restrictDateModification) { | ||
|
||
| delete entity.createdOn; | ||
| delete entity.modifiedOn; | ||
| } | ||
| return super.create(entity, options); | ||
| } | ||
|
|
||
|
|
@@ -56,6 +63,10 @@ export abstract class DefaultTransactionalUserModifyRepository< | |
| entities.forEach(entity => { | ||
| entity.createdBy = uid ?? ''; | ||
| entity.modifiedBy = uid ?? ''; | ||
| if (this.overridingOptions?.restrictDateModification) { | ||
| delete entity.createdOn; | ||
| delete entity.modifiedOn; | ||
| } | ||
| }); | ||
| return super.createAll(entities, options); | ||
| } | ||
|
|
@@ -67,6 +78,10 @@ export abstract class DefaultTransactionalUserModifyRepository< | |
| } | ||
| const uid = currentUser?.userTenantId ?? currentUser?.id; | ||
| entity.modifiedBy = uid; | ||
| if (this.overridingOptions?.restrictDateModification) { | ||
| delete entity.createdOn; | ||
| delete entity.modifiedOn; | ||
| } | ||
| return super.save(entity, options); | ||
| } | ||
|
|
||
|
|
@@ -77,6 +92,11 @@ export abstract class DefaultTransactionalUserModifyRepository< | |
| } | ||
| const uid = currentUser?.userTenantId ?? currentUser?.id; | ||
| entity.modifiedBy = uid; | ||
| if (this.overridingOptions?.restrictDateModification) { | ||
| /**not deleting the createdOn as it can be a use case where | ||
| * we want to update the modifiedOn but not the createdOn */ | ||
| delete entity.modifiedOn; | ||
| } | ||
| return super.update(entity, options); | ||
| } | ||
|
|
||
|
|
@@ -92,6 +112,11 @@ export abstract class DefaultTransactionalUserModifyRepository< | |
| } | ||
| const uid = currentUser?.userTenantId ?? currentUser?.id; | ||
| data.modifiedBy = uid; | ||
| if (this.overridingOptions?.restrictDateModification) { | ||
| /**not deleting the createdOn as it can be a use case where | ||
| * we want to update the modifiedOn but not the createdOn */ | ||
| delete data.modifiedOn; | ||
| } | ||
| return super.updateAll(data, where, options); | ||
| } | ||
|
|
||
|
|
@@ -107,6 +132,11 @@ export abstract class DefaultTransactionalUserModifyRepository< | |
| } | ||
| const uid = currentUser?.userTenantId ?? currentUser?.id; | ||
| data.modifiedBy = uid; | ||
| if (this.overridingOptions?.restrictDateModification) { | ||
| /**not deleting the createdOn as it can be a use case where | ||
| * we want to update the modifiedOn but not the createdOn */ | ||
| delete data.modifiedOn; | ||
| } | ||
| return super.updateById(id, data, options); | ||
| } | ||
|
|
||
|
|
@@ -121,6 +151,11 @@ export abstract class DefaultTransactionalUserModifyRepository< | |
| } | ||
| const uid = currentUser?.userTenantId ?? currentUser?.id; | ||
| data.modifiedBy = uid; | ||
| if (this.overridingOptions?.restrictDateModification) { | ||
| /**not deleting the createdOn as it can be a use case where | ||
| * we want to update the modifiedOn but not the createdOn */ | ||
| delete data.modifiedOn; | ||
| } | ||
| return super.replaceById(id, data, options); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done