Fix optional parameters in reportAdminPortalEvent

function
This commit is contained in:
Kiran K 2023-11-28 16:51:09 +05:30
parent e498c9aa4c
commit 5d1c5cdf51
2 changed files with 38 additions and 14 deletions

View File

@ -51,7 +51,8 @@ type ReportAdminEventParams = {
action: AuditEventType;
crud: Retraced.CRUD;
target?: Retraced.Target;
req: NextApiRequest;
req?: NextApiRequest;
actor?: Retraced.Actor;
};
interface ReportEventParams extends Event {
@ -129,7 +130,13 @@ const reportEvent = async ({ action, crud, group, actor, description, productId
};
// Report Admin portal events to Retraced
export const reportAdminPortalEvent = async ({ action, crud, target, req }: ReportAdminEventParams) => {
export const reportAdminPortalEvent = async ({
action,
crud,
target,
actor,
req,
}: ReportAdminEventParams) => {
try {
const retracedClient = await getClient();
@ -137,27 +144,31 @@ export const reportAdminPortalEvent = async ({ action, crud, target, req }: Repo
return;
}
// Get actor info
const user = await getNextAuthToken({
req,
cookieName: sessionName,
});
// If no actor is provided, try to get it from the request
if (req && !actor) {
const user = await getNextAuthToken({
req,
cookieName: sessionName,
});
if (!user || !user.email || !user.name) {
console.error(`Can't find actor info for Retraced event.`);
return;
if (!user || !user.email || !user.name) {
console.error(`Can't find actor info for Retraced event.`);
return;
}
actor = {
id: user.email,
name: user.name,
};
}
const retracedEvent: Event = {
action,
crud,
target,
actor,
group: adminPortalGroup,
created: new Date(),
actor: {
id: user.email,
name: user.name,
},
//source_ip: process.env.NODE_ENV === 'development' ? null : requestIp.getClientIp(req),
};

View File

@ -7,6 +7,7 @@ import jackson from '@lib/jackson';
import { validateEmailWithACL } from '@lib/utils';
import { jacksonOptions as env } from '@lib/env';
import { sessionName } from '@lib/constants';
import retraced from '@ee/retraced';
export default NextAuth({
theme: {
@ -168,4 +169,16 @@ export default NextAuth({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
adapter: Adapter(),
events: {
async signIn({ user }): Promise<void> {
retraced.reportAdminPortalEvent({
action: 'portal.user.login',
crud: 'c',
actor: {
id: `${user.email}`,
name: `${user.name}`,
},
});
},
},
});