Fix: Requested changes

This commit is contained in:
Daniel Hougaard
2024-06-24 19:11:58 +02:00
parent 2d98560255
commit c735beea32
6 changed files with 62 additions and 62 deletions
@@ -1,14 +1,16 @@
import { Knex } from "knex";
import { TDbClient } from "@app/db";
import { TableName, TSuperAdmin } from "@app/db/schemas";
import { TableName, TSuperAdmin, TSuperAdminUpdate } from "@app/db/schemas";
import { ormify } from "@app/lib/knex";
export type TSuperAdminDALFactory = ReturnType<typeof superAdminDALFactory>;
export const superAdminDALFactory = (db: TDbClient) => {
const orm = ormify(db, TableName.SuperAdmin);
const superAdminOrm = ormify(db, TableName.SuperAdmin);
const findById = async (id: string) => {
const config = await db(TableName.SuperAdmin)
const findById = async (id: string, tx?: Knex) => {
const config = await (tx || db)(TableName.SuperAdmin)
.where(`${TableName.SuperAdmin}.id`, id)
.leftJoin(TableName.Organization, `${TableName.SuperAdmin}.defaultAuthOrgId`, `${TableName.Organization}.id`)
.select(
@@ -23,8 +25,20 @@ export const superAdminDALFactory = (db: TDbClient) => {
} as TSuperAdmin & { defaultAuthOrgSlug: string | null };
};
const updateById = async (id: string, data: TSuperAdminUpdate, tx?: Knex) => {
const updatedConfig = await superAdminOrm.transaction(async (trx) => {
await superAdminOrm.updateById(id, data, tx || trx);
const config = await findById(id, tx || trx);
return config;
});
return updatedConfig;
};
return {
...orm,
findById
...superAdminOrm,
findById,
updateById
};
};