tasko/lib/create-audit-log.ts

38 lines
858 B
TypeScript
Raw Normal View History

2024-03-22 20:52:51 +00:00
import { auth, currentUser } from '@clerk/nextjs/server';
2024-02-16 01:49:19 +00:00
import { ACTION, ENTITY_TYPE } from '@prisma/client';
2024-02-15 02:30:10 +00:00
2024-02-16 01:49:19 +00:00
import { db } from '@/lib/db';
2024-02-15 02:30:10 +00:00
interface Props {
entityId: string;
entityType: ENTITY_TYPE;
entityTitle: string;
action: ACTION;
}
export const createAuditLog = async (props: Props) => {
try {
const { orgId } = auth();
const user = await currentUser();
2024-02-16 01:49:19 +00:00
if (!orgId || !user) throw new Error('User not found');
2024-02-15 02:30:10 +00:00
const { entityId, entityType, entityTitle, action } = props;
await db.auditLog.create({
data: {
orgId,
entityId,
entityType,
entityTitle,
action,
userId: user.id,
userImage: user?.imageUrl,
2024-02-16 01:49:19 +00:00
userName: user?.firstName + ' ' + user?.lastName,
2024-02-15 02:30:10 +00:00
},
});
} catch (error) {
2024-02-16 01:49:19 +00:00
console.error('[AUDIT_LOG_ERROR]', error);
2024-02-15 02:30:10 +00:00
}
};