Skip to content

Commit

Permalink
feat: move count increase to a trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
Pinx0 committed Nov 10, 2024
1 parent af70f21 commit fe2220f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 60 deletions.
14 changes: 7 additions & 7 deletions src/app/casos-activos/solicitudes/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ function Solicitudes() {
))}
</select>
</div>
{/*<div className="flex flex-row flex-1 justify-end">
<Toggle
handleChange={handleToggleChange}
checked={isStringTrue(filtroData.soloSinAsignar)}
label="Sólo ofertas sin voluntarios"
/>
</div>*/}
<div className="flex flex-row flex-1 justify-end">
<Toggle
handleChange={handleToggleChange}
checked={isStringTrue(filtroData.soloSinAsignar)}
label="Sólo ofertas sin voluntarios"
/>
</div>
</div>
</div>
<div className="grid gap-4">
Expand Down
31 changes: 16 additions & 15 deletions src/components/Toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,27 @@ export const Toggle = ({ checked, handleChange, label }: ToggleProps) => {
<label htmlFor="toggle" className="font-medium">
{label}
</label>
<div
className={`relative inline-block w-10 h-6 rounded-full transition-colors duration-300 ${
checked ? 'bg-green-500' : 'bg-gray-300'
}`}
>
<input
type="checkbox"
id="toggle"
className="absolute w-0 h-0 opacity-0"
checked={checked}
onChange={handleChange}
/>
<label htmlFor="toggle">
<label htmlFor="toggle">
<div
className={`relative inline-block w-10 h-6 rounded-full transition-colors duration-300 ${
checked ? 'bg-green-500' : 'bg-gray-300'
}`}
>
<input
type="checkbox"
id="toggle"
className="absolute w-0 h-0 opacity-0"
checked={checked}
onChange={handleChange}
/>

<span
className={`absolute top-0.5 left-0.5 w-5 h-5 rounded-full bg-white transition-transform duration-300 transform ${
checked ? 'translate-x-4' : ''
}`}
></span>
</label>
</div>
</div>
</label>
</div>
);
};
38 changes: 0 additions & 38 deletions src/lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,49 +70,11 @@ export const helpRequestService = {
async assign(requestData: HelpRequestAssignmentInsert) {
const { data, error } = await supabase.from('help_request_assignments').insert([requestData]).select();
if (error) throw error;

const { data: linkedRequestData, error: errorGettingLinkedData } = await supabase
.from('help_requests')
.select('*')
.eq('id', requestData.help_request_id);
if (errorGettingLinkedData) throw errorGettingLinkedData;
if (!linkedRequestData) throw new Error('No se puede encontrar esta tarea');

const { error: errorUpdatingAssigneesCount } = await supabase
.from('help_requests')
.update({ asignees_count: linkedRequestData[0].asignees_count + 1 })
.eq('id', requestData.help_request_id);
if (errorUpdatingAssigneesCount) throw errorUpdatingAssigneesCount;

return data[0];
},
async unassign(id: number) {
const { data, error: errorFindingRow } = await supabase.from('help_request_assignments').select('*').eq('id', id);
if (errorFindingRow || !data) {
throw new Error('No se puede encontrar la tarea');
}

const requestId = data[0].help_request_id;

const { error: errorDeletingAssignment } = await supabase.from('help_request_assignments').delete().eq('id', id);
if (errorDeletingAssignment) throw errorDeletingAssignment;

const { data: linkedRequestData, error: errorGettingLinkedData } = await supabase
.from('help_requests')
.select('*')
.eq('id', requestId);

if (errorGettingLinkedData) throw errorGettingLinkedData;
if (!linkedRequestData) throw new Error('No se puede encontrar esta tarea');

const { asignees_count } = linkedRequestData[0];
const newNumberAssignees = asignees_count <= 0 ? 0 : asignees_count - 1;

const { error: errorUpdatingAssigneesCount } = await supabase
.from('help_requests')
.update({ asignees_count: newNumberAssignees })
.eq('id', requestId);
if (errorUpdatingAssigneesCount) throw errorUpdatingAssigneesCount;
},

async getByType(type: any) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
CREATE OR REPLACE FUNCTION update_asignees_count()
RETURNS TRIGGER AS $$
BEGIN
RAISE NOTICE 'Trigger executed on operation: %, help_request_id: %', TG_OP, COALESCE(NEW.help_request_id, OLD.help_request_id);

IF TG_OP = 'INSERT' THEN
-- Increment assignments_count on assignment add
UPDATE public.help_requests
SET asignees_count = asignees_count + 1
WHERE id = NEW.help_request_id;
ELSIF TG_OP = 'DELETE' THEN
-- Decrement assignments_count on assignment removal
UPDATE public.help_requests
SET asignees_count = asignees_count - 1
WHERE id = OLD.help_request_id;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trigger_update_assignments_count
AFTER INSERT OR DELETE ON public.help_request_assignments
FOR EACH ROW EXECUTE FUNCTION update_asignees_count();

0 comments on commit fe2220f

Please sign in to comment.