Added house function: add, update, view (admin function)

update npm package
This commit is contained in:
2026-02-05 21:10:45 +07:00
parent 018f693998
commit 7b14b30320
104 changed files with 3447 additions and 2518 deletions

View File

@@ -1,9 +1,16 @@
import { prisma } from '@/db';
import { OrganizationWhereInput } from '@/generated/prisma/models';
import { authMiddleware } from '@/lib/middleware';
import { parseError } from '@/utils/helper';
import { DB_TABLE, LOG_ACTION } from '@/types/enum';
import { auth } from '@lib/auth';
import { authMiddleware } from '@lib/middleware';
import { createServerFn } from '@tanstack/react-start';
import { houseListSchema } from './house.schema';
import { parseError } from '@utils/helper';
import {
houseCreateBESchema,
houseEditBESchema,
houseListSchema,
} from './house.schema';
import { createAuditLog } from './repository';
export const getAllHouse = createServerFn({ method: 'GET' })
.middleware([authMiddleware])
@@ -65,3 +72,68 @@ export const getAllHouse = createServerFn({ method: 'GET' })
throw { message, code };
}
});
export const createHouse = createServerFn({ method: 'POST' })
.middleware([authMiddleware])
.inputValidator(houseCreateBESchema)
.handler(async ({ data, context: { user } }) => {
try {
const result = await auth.api.createOrganization({
body: data,
});
if (!result) throw Error('Failed to create house');
await createAuditLog({
action: LOG_ACTION.CREATE,
tableName: DB_TABLE.ORGANIZATION,
recordId: result.id,
oldValue: '',
newValue: JSON.stringify(result),
userId: user.id,
});
return result;
} catch (error) {
console.error(error);
const { message, code } = parseError(error);
throw { message, code };
}
});
export const updateHouse = createServerFn({ method: 'POST' })
.middleware([authMiddleware])
.inputValidator(houseEditBESchema)
.handler(async ({ data, context: { user } }) => {
try {
const currentHouse = await prisma.organization.findUnique({
where: { id: data.id },
});
if (!currentHouse) throw Error('House not found');
const { id, slug, name, color } = data;
const result = await prisma.organization.update({
where: { id },
data: {
name,
slug,
color,
},
});
await createAuditLog({
action: LOG_ACTION.UPDATE,
tableName: DB_TABLE.ORGANIZATION,
recordId: id,
oldValue: JSON.stringify(currentHouse),
newValue: JSON.stringify(result),
userId: user.id,
});
return result;
} catch (error) {
console.error(error);
const { message, code } = parseError(error);
throw { message, code };
}
});