added leave house, remove member

This commit is contained in:
2026-02-23 21:40:32 +07:00
parent 12de48d19d
commit b821260fe7
28 changed files with 861 additions and 391 deletions

View File

@@ -13,6 +13,7 @@ import {
houseEditBESchema,
houseListSchema,
invitationCreateBESchema,
removeMemberSchema,
} from './house.schema';
import { createAuditLog, createNotification } from './repository';
@@ -347,7 +348,7 @@ export const cancelInvitation = createServerFn({ method: 'POST' })
export const acceptInvitation = createServerFn({ method: 'POST' })
.middleware([authMiddleware])
.inputValidator(actionInvitationSchema)
.handler(async ({ data }) => {
.handler(async ({ data, context: { user } }) => {
try {
const result = await prisma.invitation.update({
where: { id: data.id },
@@ -360,13 +361,33 @@ export const acceptInvitation = createServerFn({ method: 'POST' })
data: { link: null },
});
await auth.api.addMember({
const member = await auth.api.addMember({
body: {
userId: notify.userId,
organizationId: result.organizationId,
role: (result.role as 'admin' | 'owner' | 'member') || 'member',
},
});
await createAuditLog({
action: LOG_ACTION.UPDATE,
tableName: DB_TABLE.INVITATION,
recordId: result.id,
oldValue: JSON.stringify(result),
newValue: 'Accept Invitation / Đồng ý mời',
userId: user.id,
});
if (member) {
await createAuditLog({
action: LOG_ACTION.CREATE,
tableName: DB_TABLE.MEMBER,
recordId: member.id,
oldValue: '',
newValue: JSON.stringify(member),
userId: user.id,
});
}
}
return result;
@@ -404,3 +425,72 @@ export const rejectInvitation = createServerFn({ method: 'POST' })
throw { message, code };
}
});
export const leaveHouse = createServerFn({ method: 'POST' })
.middleware([authMiddleware])
.inputValidator(baseHouse)
.handler(async ({ data }) => {
try {
const headers = getRequestHeaders();
const result = await auth.api.leaveOrganization({
body: {
organizationId: data.id,
},
headers,
});
if (result) {
await createAuditLog({
action: LOG_ACTION.UPDATE,
tableName: DB_TABLE.ORGANIZATION,
recordId: result.id,
oldValue: JSON.stringify(result),
newValue: `${result.user.name} đã rời khỏi nhà (left the house)`,
userId: result.user.id,
});
}
return result;
} catch (error) {
console.error(error);
const { message, code } = parseError(error);
throw { message, code };
}
});
export const removeMember = createServerFn({ method: 'POST' })
.middleware([authMiddleware])
.inputValidator(removeMemberSchema)
.handler(async ({ data, context: { user } }) => {
try {
const headers = getRequestHeaders();
const result = await auth.api.removeMember({
body: {
memberIdOrEmail: data.memberId,
organizationId: data.houseId,
},
headers,
});
if (result) {
const oldMember = await prisma.user.findUnique({
where: { id: result.member.userId },
});
await createAuditLog({
action: LOG_ACTION.UPDATE,
tableName: DB_TABLE.MEMBER,
recordId: result.member.id,
oldValue: JSON.stringify(result),
newValue: `${oldMember?.name} đã bị "mời" rời khỏi nhà (was "asked" to leave the house.)`,
userId: user.id,
});
}
return result;
} catch (error) {
console.error(error);
const { message, code } = parseError(error);
throw { message, code };
}
});