From 2fc205a2617f33cb6cb11b91c55f8cede46a2e2c Mon Sep 17 00:00:00 2001 From: Joan Mon Date: Sun, 10 Nov 2024 23:34:11 +0100 Subject: [PATCH 1/2] feat(crm-status): Edit crm status with moderator role --- src/components/ChangeCRMStatus.tsx | 71 ++++++++++++++++++++++++++++++ src/components/SolicitudCard.tsx | 15 ++++++- src/helpers/constants.ts | 19 ++++++++ src/types/database.ts | 1 + 4 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 src/components/ChangeCRMStatus.tsx diff --git a/src/components/ChangeCRMStatus.tsx b/src/components/ChangeCRMStatus.tsx new file mode 100644 index 0000000..5061cef --- /dev/null +++ b/src/components/ChangeCRMStatus.tsx @@ -0,0 +1,71 @@ +'use client'; + +import { supabase } from '@/lib/supabase/client'; +import { useState } from 'react'; +import { CRMStatus, CrmStatusActive, CrmStatusFinished } from '@/helpers/constants'; + +type ChangeCRMStatusRequestButtonProps = { + helpRequestId: number; + onStatusUpdate: (status: string) => void; + currentStatus: string | null; + currentCrmStatus: string | null; +}; + +export default function ChangeCRMStatus({ + helpRequestId, + onStatusUpdate, + currentStatus, + currentCrmStatus, +}: ChangeCRMStatusRequestButtonProps) { + const [crmStatus, setCrmStatus] = useState(currentCrmStatus || CrmStatusActive); + const [error, setError] = useState({}); + + const updateStatusRequest = async (newCrmStatus: string) => { + var status = currentStatus || 'active'; + if (newCrmStatus === CrmStatusFinished) { + status = 'finished'; + } else if (newCrmStatus !== CrmStatusFinished && status == 'finished') { + status = 'active'; + } + const { data, error } = await supabase + .from('help_requests') + .update({ status, crm_status: newCrmStatus }) + .eq('id', helpRequestId) + .select(); + + onStatusUpdate(status); + + return { data, error }; + }; + + async function handleUpdateSubmit(newStatus: string) { + const { data, error } = await updateStatusRequest(newStatus); + if (error) { + setError(error); + return; + } + } + + if (error === undefined) return <>; + + return ( + <> + + + ); +} diff --git a/src/components/SolicitudCard.tsx b/src/components/SolicitudCard.tsx index 7e6ab24..f7a9630 100644 --- a/src/components/SolicitudCard.tsx +++ b/src/components/SolicitudCard.tsx @@ -13,6 +13,8 @@ import { useRole } from '@/context/RoleProvider'; import { useState } from 'react'; import ChangeUrgencyHelpRequest from './ChangeUrgencyHelpRequest'; import ChangeStatusButton from './ChangeStatusButton'; +import ChangeCRMStatus from './ChangeCRMStatus'; +import { UserRoles } from '@/helpers/constants'; type SolicitudCardProps = { caso: HelpRequestData; @@ -32,7 +34,8 @@ export default function SolicitudCard({ const { getTownById } = useTowns(); const additionalInfo = caso.additional_info as HelpRequestAdditionalInfo; const special_situations = 'special_situations' in additionalInfo ? additionalInfo.special_situations : undefined; - const isAdmin = role === 'admin'; + const isAdmin = role === UserRoles.admin; + const isCrmUser = role === UserRoles.moderator; const [deleted, setDeleted] = useState(false); const isMyRequest = session.user?.id && session.user.id === caso.user_id; const [updateUrgency, setUpdateUrgency] = useState(caso.urgency); @@ -194,7 +197,7 @@ export default function SolicitudCard({ Ver solicitud )} - + {!isCrmUser && } {isAdmin && ( )} + {isCrmUser && ( + + )} {isAdmin && setDeleted(true)} />} diff --git a/src/helpers/constants.ts b/src/helpers/constants.ts index 37a254e..17b7591 100644 --- a/src/helpers/constants.ts +++ b/src/helpers/constants.ts @@ -24,3 +24,22 @@ export const tiposAyudaArray: TipoAyudaOption[] = Object.entries(tiposAyudaOptio id: id as TipoAyudaId, label, })); + +export const CrmStatusActive = 'active'; +export const CrmStatusFollowUp = 'followup'; +export const CrmStatusAssigned = 'assigned'; +export const CrmStatusProgress = 'progress'; +export const CrmStatusFinished = 'finished'; + +export const CRMStatus = { + [CrmStatusActive]: 'Activo', + [CrmStatusFollowUp]: 'Volver a llamar', + [CrmStatusAssigned]: 'Asignado', + [CrmStatusProgress]: 'En Progreso', + [CrmStatusFinished]: 'Hecho', +}; + +export const UserRoles = { + admin: 'admin', + moderator: 'moderator', +}; diff --git a/src/types/database.ts b/src/types/database.ts index 93d05ab..0e8c342 100644 --- a/src/types/database.ts +++ b/src/types/database.ts @@ -190,6 +190,7 @@ export type Database = { town_id: number | null; type: string | null; urgency: string | null; + crm_status: string | null; user_id: string | null; }; Insert: { From 9d681840944b276cf620ad4636a8a9944f86b209 Mon Sep 17 00:00:00 2001 From: patrickwebsdev Date: Sun, 10 Nov 2024 19:41:01 -0300 Subject: [PATCH 2/2] feat: add supabase migration --- ...us_to_help_request_and_policy_for_mod_update.sql | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 supabase/migrations/20241110223829_add_crm_status_to_help_request_and_policy_for_mod_update.sql diff --git a/supabase/migrations/20241110223829_add_crm_status_to_help_request_and_policy_for_mod_update.sql b/supabase/migrations/20241110223829_add_crm_status_to_help_request_and_policy_for_mod_update.sql new file mode 100644 index 0000000..cf02192 --- /dev/null +++ b/supabase/migrations/20241110223829_add_crm_status_to_help_request_and_policy_for_mod_update.sql @@ -0,0 +1,13 @@ +alter table "public"."help_requests" add column "crm_status" text; + +create policy "Mod update" +on "public"."help_requests" +as permissive +for update +to authenticated +using ((EXISTS ( SELECT 1 + FROM user_roles + WHERE ((user_roles.user_id = auth.uid()) AND (user_roles.role = 'moderator'::roles))))); + + +