Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
fix(eval): revamped sentiment analysis logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jhdcruz committed Nov 14, 2024
1 parent bc64f98 commit af8ae1f
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 176 deletions.
24 changes: 4 additions & 20 deletions src/app/eval/_components/Forms/BeneficiariesForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { ThemeSwitcher } from '@/components/Buttons/ThemeSwitcher';
import { RatingField } from './RatingFields';

export interface BeneficiariesFeedbackProps {
idempotencyKey?: string;
id?: string;
type?: Enums<'feedback_type'>;
respondent: {
Expand Down Expand Up @@ -74,6 +75,7 @@ const BeneficiariesForm = ({
validateInputOnChange: true,

initialValues: {
idempotencyKey: uuid,
id: activity.id!,
type: 'beneficiaries',
respondent: {
Expand Down Expand Up @@ -117,9 +119,9 @@ const BeneficiariesForm = ({
},
});

const handleSubmit = async () => {
const handleSubmit = async (values: typeof form.values) => {
setLoading(true);
const result = await submitFeedback(form);
const result = await submitFeedback(values);
setLoading(false);

if (result?.status === 0) {
Expand Down Expand Up @@ -180,24 +182,6 @@ const BeneficiariesForm = ({
</Affix>

<form onSubmit={form.onSubmit(handleSubmit)}>
<TextInput
key={form.key('idempotencyKey')}
type="hidden"
value={uuid}
{...form.getInputProps('idempotencyKey')}
/>

<TextInput
key={form.key('id')}
type="hidden"
{...form.getInputProps('id')}
/>
<TextInput
key={form.key('type')}
type="hidden"
{...form.getInputProps('type')}
/>

<Fieldset legend="Beneficiary Information (Optional)" my="md">
<TextInput
description="First Name, Middle Initial, Last Name, Suffix (if any)"
Expand Down
24 changes: 4 additions & 20 deletions src/app/eval/_components/Forms/ImplementersForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ThemeSwitcher } from '@/components/Buttons/ThemeSwitcher';
import { RatingField } from './RatingFields';

export interface ImplementerFeedbackProps {
idempotencyKey?: string;
id?: string;
type?: Enums<'feedback_type'>;
respondent: {
Expand Down Expand Up @@ -93,6 +94,7 @@ const ImplementersForm = ({
validateInputOnChange: true,

initialValues: {
idempotencyKey: uuid,
id: activity.id!,
type: 'implementers',
respondent: {
Expand Down Expand Up @@ -171,9 +173,9 @@ const ImplementersForm = ({
},
});

const handleSubmit = async () => {
const handleSubmit = async (values: typeof form.values) => {
setLoading(true);
const result = await submitFeedback(form);
const result = await submitFeedback(values);
setLoading(false);

if (result?.status === 0) {
Expand Down Expand Up @@ -238,24 +240,6 @@ const ImplementersForm = ({
</Affix>

<form onSubmit={form.onSubmit(handleSubmit)}>
<TextInput
key={form.key('idempotencyKey')}
type="hidden"
value={uuid}
{...form.getInputProps('idempotencyKey')}
/>

<TextInput
key={form.key('id')}
type="hidden"
{...form.getInputProps('id')}
/>
<TextInput
key={form.key('type')}
type="hidden"
{...form.getInputProps('type')}
/>

<Fieldset legend="Implementer's Information" my="md">
<TextInput
description="First Name, Middle Initial, Last Name, Suffix (if any)"
Expand Down
24 changes: 4 additions & 20 deletions src/app/eval/_components/Forms/PartnersForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ThemeSwitcher } from '@/components/Buttons/ThemeSwitcher';
import { RatingField } from './RatingFields';

export interface PartnersFeedbackProps {
idempotencyKey?: string;
id?: string;
type?: Enums<'feedback_type'>;
respondent: {
Expand Down Expand Up @@ -67,6 +68,7 @@ const PartnersForm = ({
validateInputOnChange: true,

initialValues: {
idempotencyKey: uuid,
id: activity.id!,
type: 'partners',
respondent: {
Expand Down Expand Up @@ -122,9 +124,9 @@ const PartnersForm = ({
},
});

const handleSubmit = async () => {
const handleSubmit = async (values: typeof form.values) => {
setLoading(true);
const result = await submitFeedback(form);
const result = await submitFeedback(values);
setLoading(false);

if (result?.status === 0) {
Expand Down Expand Up @@ -184,24 +186,6 @@ const PartnersForm = ({
</Affix>

<form onSubmit={form.onSubmit(handleSubmit)}>
<TextInput
key={form.key('idempotencyKey')}
type="hidden"
value={uuid}
{...form.getInputProps('idempotencyKey')}
/>

<TextInput
key={form.key('id')}
type="hidden"
{...form.getInputProps('id')}
/>
<TextInput
key={form.key('type')}
type="hidden"
{...form.getInputProps('type')}
/>

<Fieldset legend="Partner's Information" my="md">
<TextInput
description="First Name, Middle Initial, Last Name, Suffix (if any)"
Expand Down
34 changes: 20 additions & 14 deletions src/app/eval/actions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'use server';

import { cookies } from 'next/headers';
import { UseFormReturnType } from '@mantine/form';
import { createServerClient } from '@/libs/supabase/server';
import {
analyzePartner,
analyzeBeneficiary,
analyzeImplementer,
} from '@/trigger/analyze-feedback';
import type { ImplementerFeedbackProps } from './_components/Forms/ImplementersForm';
import type { PartnersFeedbackProps } from './_components/Forms/PartnersForm';
import type { BeneficiariesFeedbackProps } from './_components/Forms/BeneficiariesForm';
import type ApiResponse from '@/utils/response';

/**
Expand All @@ -16,14 +18,16 @@ import type ApiResponse from '@/utils/response';
* @param form - Form data using PartnersFeedbackProps type.
*/
export async function submitFeedback(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
form: UseFormReturnType<any>,
form:
| ImplementerFeedbackProps
| PartnersFeedbackProps
| BeneficiariesFeedbackProps,
): Promise<ApiResponse> {
const cookieStore = cookies();
const supabase = await createServerClient(cookieStore);

// separate id and type from the rest
const { id, idempotencyKey, type, ...feedback } = form.values;
const { id, idempotencyKey, type, ...feedback } = form;

const { data, error } = await supabase
.from('activity_feedback')
Expand All @@ -32,7 +36,9 @@ export async function submitFeedback(
type: type!,
response: feedback,
})
.select();
.select('id')
.limit(1)
.single();

if (error) {
return {
Expand All @@ -45,33 +51,33 @@ export async function submitFeedback(
switch (type) {
case 'beneficiaries':
// analyze beneficiary feedback
analyzeBeneficiary.trigger(
{ id: id!, form: feedback },
await analyzeBeneficiary.trigger(
{ id: data.id, form: feedback as BeneficiariesFeedbackProps },
{
idempotencyKey: idempotencyKey,
tags: [`activity_${id}`, 'type_beneficiary'],
tags: [`activity_${id}`, `feedback_${data.id}`, 'type_beneficiary'],
},
);
break;

case 'partners':
// analyze partner feedback
analyzePartner.trigger(
{ id: id!, form: feedback },
await analyzePartner.trigger(
{ id: data.id, form: feedback as PartnersFeedbackProps },
{
idempotencyKey: idempotencyKey,
tags: [`activity_${id}`, 'type_partner'],
tags: [`activity_${id}`, `feedback_${data.id}`, 'type_partner'],
},
);
break;

case 'implementers':
// analyze implementers feedback
analyzeImplementer.trigger(
{ id: id!, form: feedback },
await analyzeImplementer.trigger(
{ id: data.id, form: feedback as ImplementerFeedbackProps },
{
idempotencyKey: idempotencyKey,
tags: [`activity_${id}`, 'type_implementer'],
tags: [`activity_${id}`, `feedback_${data.id}`, 'type_implementer'],
},
);
break;
Expand Down
Loading

0 comments on commit af8ae1f

Please sign in to comment.