Implement invitation renewal and optimize revalidation function
This commit adds a new function to renew team invitations and a central function for revalidating member page. It also removes redundant revalidations across different actions. The renew invitation function and UI elements are introduced including a new dialog for confirming the renewal action. Furthermore, function revalidateMemberPage() is added to abstract the revalidation path used multiple times in different functions. The code readability and maintainability have thus been improved.
This commit is contained in:
@@ -36,7 +36,7 @@ export async function createInvitationsAction(params: {
|
||||
|
||||
await service.sendInvitations({ invitations, account: params.account });
|
||||
|
||||
revalidatePath('/home/[account]/members', 'page');
|
||||
revalidateMemberPage();
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
@@ -65,6 +65,8 @@ export async function deleteInvitationAction(
|
||||
|
||||
await service.deleteInvitation(invitation);
|
||||
|
||||
revalidateMemberPage();
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
@@ -80,6 +82,8 @@ export async function updateInvitationAction(
|
||||
|
||||
await service.updateInvitation(invitation);
|
||||
|
||||
revalidateMemberPage();
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
@@ -103,6 +107,21 @@ export async function acceptInvitationAction(data: FormData) {
|
||||
return redirect(nextPath);
|
||||
}
|
||||
|
||||
export async function renewInvitationAction(params: { invitationId: number }) {
|
||||
const client = getSupabaseServerActionClient();
|
||||
const { invitationId } = params;
|
||||
|
||||
await assertSession(client);
|
||||
|
||||
const service = new AccountInvitationsService(client);
|
||||
|
||||
await service.renewInvitation(invitationId);
|
||||
|
||||
revalidateMemberPage();
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
async function assertSession(client: SupabaseClient<Database>) {
|
||||
const { error, data } = await requireAuth(client);
|
||||
|
||||
@@ -112,3 +131,7 @@ async function assertSession(client: SupabaseClient<Database>) {
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
function revalidateMemberPage() {
|
||||
revalidatePath('/home/[account]/members', 'page');
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
import { addDays, formatISO } from 'date-fns';
|
||||
import 'server-only';
|
||||
import { z } from 'zod';
|
||||
|
||||
@@ -227,6 +228,35 @@ export class AccountInvitationsService {
|
||||
return data;
|
||||
}
|
||||
|
||||
async renewInvitation(invitationId: number) {
|
||||
Logger.info('Renewing invitation', {
|
||||
invitationId,
|
||||
name: this.namespace,
|
||||
});
|
||||
|
||||
const sevenDaysFromNow = formatISO(addDays(new Date(), 7));
|
||||
|
||||
const { data, error } = await this.client
|
||||
.from('invitations')
|
||||
.update({
|
||||
expires_at: sevenDaysFromNow,
|
||||
})
|
||||
.match({
|
||||
id: invitationId,
|
||||
});
|
||||
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
Logger.info('Invitation successfully renewed', {
|
||||
invitationId,
|
||||
name: this.namespace,
|
||||
});
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
private async getUser() {
|
||||
const { data, error } = await requireAuth(this.client);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user