import { Knex } from 'knex' import { db } from '../../db/db.client' import { IDbQueryOptions, IUpdate, TableName, TablePK, } from '../models/db.model' import { IUserEntity, IUserQueryParams } from '../models/user.model' import { BaseRepository } from './base.repository' class UserRepository extends BaseRepository { constructor() { super({ db: db, pk: TablePK.Users, tableName: TableName.Users, }) } $findQueryContext( query: Knex.QueryBuilder, filter: IUserQueryParams, options: IDbQueryOptions, ) { if (filter?.search) { const { xid, first_name, last_name } = filter.search if (xid) query.andWhere(`${this.tableName}.xid`, xid) if (last_name) query.andWhere(`${this.tableName}.last_name`, 'ilike', `%${last_name}%`) if (first_name) query.andWhere( `${this.tableName}.first_name`, 'ilike', `%${first_name}%`, ) } return super.$findQueryContext(query, filter, options) } async create(item: Partial, options?: IDbQueryOptions) { if (item.password) item.password = await this.hashPassword(item.password) return super.create(item, options) } async update({ id, item }: IUpdate, options?: IDbQueryOptions) { if (item.password) item.password = await this.hashPassword(item.password) return super.update({ id, item }, options) } } export const userRepository = new UserRepository()