Add delete house

This commit is contained in:
2026-02-06 11:34:23 +07:00
parent 7b14b30320
commit 42435faa7f
11 changed files with 316 additions and 24 deletions

View File

@@ -6,6 +6,7 @@ import { authMiddleware } from '@lib/middleware';
import { createServerFn } from '@tanstack/react-start';
import { parseError } from '@utils/helper';
import {
baseHouse,
houseCreateBESchema,
houseEditBESchema,
houseListSchema,
@@ -137,3 +138,42 @@ export const updateHouse = createServerFn({ method: 'POST' })
throw { message, code };
}
});
export const deleteHouse = createServerFn({ method: 'POST' })
.middleware([authMiddleware])
.inputValidator(baseHouse)
.handler(async ({ data, context: { user } }) => {
try {
const currentHouse = await prisma.organization.findUnique({
where: { id: data.id },
});
if (!currentHouse) throw Error('House not found');
const result = await Promise.all([
prisma.organization.delete({
where: { id: data.id },
}),
prisma.member.deleteMany({
where: { organizationId: data.id },
}),
prisma.invitation.deleteMany({
where: { organizationId: data.id },
}),
]);
await createAuditLog({
action: LOG_ACTION.DELETE,
tableName: DB_TABLE.ORGANIZATION,
recordId: result[0]?.id,
oldValue: JSON.stringify(currentHouse),
newValue: '',
userId: user.id,
});
return result;
} catch (error) {
console.error(error);
const { message, code } = parseError(error);
throw { message, code };
}
});