-
Notifications
You must be signed in to change notification settings - Fork 77
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 2 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why cant we use the second parameter of the method options?: Options. That ways its consistent and easy to implement. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the method's option parameter user will have to make changes in all the method calls There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But this introduces a new param whereas we already have such options in method param. The second param is for that purpose only. Moreover, using the method options param gives more flexibility at runtime. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now using the coreConfig for decision making |
||
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.
I dont like this approach. This will mean they will have to do it for all the repo files. Why ? We need to support both mechanism. One using options and another using a global config.
Uh oh!
There was an error while loading. Please reload this page.
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.
Tried passing an optional config to the repository but it still requires it from the derived class no mater what
making it a Breaking change - hence used this approach
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.
Now using the coreConfig for decision making