Add delete house
This commit is contained in:
@@ -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 };
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user