You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
3.3 KiB
TypeScript
129 lines
3.3 KiB
TypeScript
'use server';
|
|
|
|
import { signIn } from '@/auth';
|
|
import { AuthError } from 'next-auth';
|
|
|
|
export async function authenticate(
|
|
prevState: string | undefined,
|
|
formData: FormData,
|
|
) {
|
|
try {
|
|
await signIn('credentials', Object.fromEntries(formData));
|
|
} catch (error) {
|
|
if (error instanceof AuthError) {
|
|
switch (error.type) {
|
|
case 'CredentialsSignin':
|
|
return 'Invalid credentials.';
|
|
default:
|
|
return 'Something went wrong.';
|
|
}
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
import { prisma } from '@/lib/prisma';
|
|
import { revalidatePath } from 'next/cache';
|
|
import { auth } from '@/auth';
|
|
|
|
export async function submitReimbursement(formData: FormData) {
|
|
const session = await auth();
|
|
if (!session?.user) throw new Error('Unauthorized');
|
|
|
|
const amount = formData.get('amount') as string;
|
|
const description = formData.get('description') as string;
|
|
const category = formData.get('category') as string;
|
|
const dateStr = formData.get('date') as string;
|
|
|
|
await prisma.reimbursement.create({
|
|
data: {
|
|
amount: parseFloat(amount),
|
|
description,
|
|
category,
|
|
createdAt: new Date(dateStr),
|
|
userId: (session.user as any).id,
|
|
},
|
|
});
|
|
|
|
revalidatePath('/me/reimbursements');
|
|
revalidatePath('/me/dashboard');
|
|
}
|
|
|
|
export async function submitOvertime(formData: FormData) {
|
|
const session = await auth();
|
|
if (!session?.user) throw new Error('Unauthorized');
|
|
|
|
const date = formData.get('date') as string;
|
|
const hours = formData.get('hours') as string;
|
|
const reason = formData.get('reason') as string;
|
|
|
|
await prisma.overtime.create({
|
|
data: {
|
|
date: new Date(date),
|
|
hours: parseFloat(hours),
|
|
reason,
|
|
userId: (session.user as any).id,
|
|
},
|
|
});
|
|
|
|
revalidatePath('/me/overtime');
|
|
revalidatePath('/me/dashboard');
|
|
}
|
|
|
|
export async function updateRequestStatus(id: string, type: 'reimbursement' | 'overtime', status: 'APPROVED' | 'REJECTED' | 'PAID') {
|
|
const session = await auth();
|
|
// Simple role check
|
|
if ((session?.user as any)?.role !== 'ADMIN') throw new Error('Admin only');
|
|
|
|
if (type === 'reimbursement') {
|
|
await prisma.reimbursement.update({
|
|
where: { id },
|
|
data: { status },
|
|
});
|
|
} else {
|
|
await prisma.overtime.update({
|
|
where: { id },
|
|
data: { status },
|
|
});
|
|
}
|
|
|
|
revalidatePath('/admin/approvals');
|
|
revalidatePath('/admin/dashboard');
|
|
}
|
|
|
|
export async function createBudget(formData: FormData) {
|
|
const department = formData.get('department') as string;
|
|
const amount = formData.get('amount') as string;
|
|
const period = formData.get('period') as string;
|
|
|
|
await prisma.budget.create({
|
|
data: {
|
|
department,
|
|
amount: parseFloat(amount),
|
|
period,
|
|
},
|
|
});
|
|
|
|
revalidatePath('/admin/budgeting');
|
|
}
|
|
|
|
export async function createLedgerEntry(formData: FormData) {
|
|
const amount = formData.get('amount') as string;
|
|
const type = formData.get('type') as 'CREDIT' | 'DEBIT';
|
|
const description = formData.get('description') as string;
|
|
const categoryId = formData.get('categoryId') as string;
|
|
const accountId = formData.get('accountId') as string;
|
|
|
|
await prisma.companyTransaction.create({
|
|
data: {
|
|
amount: parseFloat(amount),
|
|
type,
|
|
description,
|
|
categoryId,
|
|
accountId,
|
|
},
|
|
});
|
|
|
|
revalidatePath('/admin/ledger');
|
|
revalidatePath('/admin/dashboard');
|
|
}
|