Skip to content

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -34,6 +35,8 @@ export abstract class DefaultTransactionalUserModifyRepository<
super(entityClass, dataSource);
}

public overridingOptions?: RepositoryOverridingOptions;
Copy link
Contributor

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.

Copy link
Contributor Author

@yeshamavani yeshamavani Oct 28, 2024

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

Copy link
Contributor Author

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


async create(entity: DataObject<T>, options?: Options): Promise<T> {
let currentUser = await this.getCurrentUser();
currentUser = currentUser ?? options?.currentUser;
Expand All @@ -43,6 +46,10 @@ export abstract class DefaultTransactionalUserModifyRepository<
const uid = currentUser?.userTenantId ?? currentUser?.id;
entity.createdBy = uid;
entity.modifiedBy = uid;
if (this.overridingOptions?.restrictDateModification) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
but with this only one change at repo level

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

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

delete entity.createdOn;
delete entity.modifiedOn;
}
return super.create(entity, options);
}

Expand All @@ -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);
}
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {SoftCrudRepository} from 'loopback4-soft-delete';

import {IAuthUserWithPermissions} from '../components';
import {UserModifiableEntity} from '../models';
import {RepositoryOverridingOptions} from '../types';

export class DefaultUserModifyCrudRepository<
T extends UserModifiableEntity,
Expand All @@ -34,6 +35,7 @@ export class DefaultUserModifyCrudRepository<
) {
super(entityClass, dataSource);
}
public overridingOptions?: RepositoryOverridingOptions;

async create(entity: DataObject<T>, options?: Options): Promise<T> {
let currentUser = await this.getCurrentUser();
Expand All @@ -44,6 +46,10 @@ export class DefaultUserModifyCrudRepository<
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);
}

Expand All @@ -57,6 +63,10 @@ export class DefaultUserModifyCrudRepository<
entities.forEach(entity => {
entity.createdBy = uid ?? '';
entity.modifiedBy = uid ?? '';
if (this.overridingOptions?.restrictDateModification) {
delete entity.createdOn;
delete entity.modifiedOn;
}
});
return super.createAll(entities, options);
}
Expand All @@ -68,6 +78,10 @@ export class DefaultUserModifyCrudRepository<
}
const uid = currentUser?.userTenantId ?? currentUser?.id;
entity.modifiedBy = uid;
if (this.overridingOptions?.restrictDateModification) {
delete entity.createdOn;
delete entity.modifiedOn;
}
return super.save(entity, options);
}

Expand All @@ -78,6 +92,11 @@ export class DefaultUserModifyCrudRepository<
}
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);
}

Expand All @@ -93,6 +112,11 @@ export class DefaultUserModifyCrudRepository<
}
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);
}

Expand All @@ -108,6 +132,11 @@ export class DefaultUserModifyCrudRepository<
}
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);
}
async replaceById(
Expand All @@ -121,6 +150,11 @@ export class DefaultUserModifyCrudRepository<
}
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);
}
}
9 changes: 8 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ export interface IServiceConfig {
useCustomSequence: boolean;
useSequelize?: boolean;
}

/**This type is used to override the default behaviour of our repo classes */
export interface RepositoryOverridingOptions {
/**restricts user to pass createdOn and modifiedOn fields in the request body
* and only current date will be set in the database */
restrictDateModification: boolean;
//eslint-disable-next-line @typescript-eslint/no-explicit-any
[property: string]: any; //NOSONAR
}
export type OASPathDefinition = AnyObject;

export interface CoreConfig {
Expand Down
Loading