import { Count , DataObject , Getter , juggler , Where , Filter , AndClause , OrClause , Condition , AnyObject , DefaultTransactionalRepository , Entity , } from '@loopback/repository' ; import { HttpErrors } from '@loopback/rest' ; import { Options } from 'loopback-datasource-juggler' ; import { AuthErrorKeys } from 'loopback4-authentication' ; import { IAuthUserWithPermissions } from '../components' ; import { UserModifiableEntity } from '../models' ; export abstract class DefaultTransactionalUserModifyRepository < T extends UserModifiableEntity , ID , Relations extends object = { } > extends DefaultTransactionalRepository < T , ID , Relations > { constructor ( entityClass , dataSource , protected readonly getCurrentUser ) { super ( entityClass , dataSource ) ; } find ( filter ? , options ? ) { if ( filter ?. where && ( filter ?. where as AndClause < T > ) . and && ( filter ?. where as AndClause < T > ) . and . length > 0 ) { ( filter ?. where as AndClause < T > ) . and . push ( { deleted : false , } as Condition < T > ) ; } else if ( filter ?. where && ( filter ?. where as OrClause < T > ) . or && ( filter ?. where as OrClause < T > ) . or . length > 0 ) { ( filter ?. where as AndClause < T > ) . and = [ ] ; ( filter ?. where as AndClause < T > ) . and . push ( { deleted : false , } as Condition < T > , { or : ( filter ?. where as OrClause < T > ) . or , } ) ; } else { } return super . find ( filter , options ) ; } findOne ( filter ? , options ? ) { if ( filter ?. where && ( filter ?. where as AndClause < T > ) . and && ( filter ?. where as AndClause < T > ) . and . length > 0 ) { ( filter ?. where as AndClause < T > ) . and . push ( { deleted : false , } as Condition < T > ) ; } else if ( filter ?. where && ( filter ?. where as OrClause < T > ) . or && ( filter ?. where as OrClause < T > ) . or . length > 0 ) { ( filter ?. where as AndClause < T > ) . and = [ ] ; ( filter ?. where as AndClause < T > ) . and . push ( { deleted : false , } as Condition < T > , { or : ( filter ?. where as OrClause < T > ) . or , } ) ; } else { } return super . findOne ( filter , options ) ; } findById ( id , filter ? , options ? ) { if ( filter ?. where && ( filter ?. where as AndClause < T > ) . and && ( filter ?. where as AndClause < T > ) . and . length > 0 ) { ( filter ?. where as AndClause < T > ) . and . push ( { deleted : false , } as Condition < T > ) ; } else if ( filter ?. where && ( filter ?. where as OrClause < T > ) . or && ( filter ?. where as OrClause < T > ) . or . length > 0 ) { ( filter ?. where as AndClause < T > ) . and = [ ] ; ( filter ?. where as AndClause < T > ) . and . push ( { deleted : false , } as Condition < T > , { or : ( filter ?. where as OrClause < T > ) . or , } ) ; } else { } return super . findById ( id , filter , options ) ; } async create ( entity , options ? ) { let currentUser = await this . getCurrentUser ( ) ; currentUser = currentUser ?? options ?. currentUser ; if ( ! currentUser ) { throw new HttpErrors . Forbidden ( AuthErrorKeys . InvalidCredentials ) ; } const uid = currentUser ?. userTenantId ?? currentUser ?. id ; entity . createdBy = uid ; entity . modifiedBy = uid ; return super . create ( entity , options ) ; } async createAll ( entities , options ? ) { let currentUser = await this . getCurrentUser ( ) ; currentUser = currentUser ?? options ?. currentUser ; if ( ! currentUser ) { throw new HttpErrors . Forbidden ( AuthErrorKeys . InvalidCredentials ) ; } const uid = currentUser ?. userTenantId ?? currentUser ?. id ; entities . forEach ( entity => { entity . createdBy = uid ?? '' ; entity . modifiedBy = uid ?? '' ; } ) ; return super . createAll ( entities , options ) ; } async save ( entity , options ? ) { const currentUser = await this . getCurrentUser ( ) ; if ( ! currentUser ) { throw new HttpErrors . Forbidden ( AuthErrorKeys . InvalidCredentials ) ; } const uid = currentUser ?. userTenantId ?? currentUser ?. id ; entity . modifiedBy = uid ; return super . save ( entity , options ) ; } async update ( entity , options ? ) { const currentUser = await this . getCurrentUser ( ) ; if ( ! currentUser ) { throw new HttpErrors . Forbidden ( AuthErrorKeys . InvalidCredentials ) ; } const uid = currentUser ?. userTenantId ?? currentUser ?. id ; entity . modifiedBy = uid ; return super . update ( entity , options ) ; } async updateAll ( data , where ? , options ? ) { let currentUser = await this . getCurrentUser ( ) ; currentUser = currentUser ?? options ?. currentUser ; if ( ! currentUser ) { throw new HttpErrors . Forbidden ( AuthErrorKeys . InvalidCredentials ) ; } const uid = currentUser ?. userTenantId ?? currentUser ?. id ; data . modifiedBy = uid ; return super . updateAll ( data , where , options ) ; } async updateById ( id , data , options ? ) { let currentUser = await this . getCurrentUser ( ) ; currentUser = currentUser ?? options ?. currentUser ; if ( ! currentUser ) { throw new HttpErrors . Forbidden ( AuthErrorKeys . InvalidCredentials ) ; } const uid = currentUser ?. userTenantId ?? currentUser ?. id ; data . modifiedBy = uid ; return super . updateById ( id , data , options ) ; } async replaceById ( id , data , options ? ) { const currentUser = await this . getCurrentUser ( ) ; if ( ! currentUser ) { throw new HttpErrors . Forbidden ( AuthErrorKeys . InvalidCredentials ) ; } const uid = currentUser ?. userTenantId ?? currentUser ?. id ; data . modifiedBy = uid ; return super . replaceById ( id , data , options ) ; } async deleteAllHard ( where ? , options ? ) { return super . deleteAll ( where , options ) ; } async deleteByIdHard ( id , options ? ) { return super . deleteById ( id , options ) ; } async deleteById ( id , options ? ) { return super . updateById ( id , { deleted : true , } as DataObject < T > , options ) ; } async deleteAll ( where ? , options ? ) { return super . updateAll ( { deleted : true , } as DataObject < T > , where , options ) ; } }