From 1133775c85f42dbaa7d08a6c622d379dbcf33fb8 Mon Sep 17 00:00:00 2001 From: Duarte Date: Thu, 4 Jun 2026 20:20:06 +0100 Subject: [PATCH] =?UTF-8?q?corrigir=20erro=20na=20edi=C3=A7=C3=A3o=20do=20?= =?UTF-8?q?benefici=C3=A1rio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SETUP_SERVER.md | 6 +++--- src/lib/server/odoo.ts | 5 +++-- src/routes/admin/beneficiarios/[id]/+page.server.ts | 5 +++-- src/routes/admin/beneficiarios/novo/+page.server.ts | 11 ++++++++--- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/SETUP_SERVER.md b/SETUP_SERVER.md index 3de62c5..a7d76c3 100644 --- a/SETUP_SERVER.md +++ b/SETUP_SERVER.md @@ -267,8 +267,8 @@ sudo -u refood DATABASE_URL=local.db npm run db:push # 5. Compilar o projeto novamente sudo -u refood npm run build -# 6. Recarregar o PM2 de forma segura (Zero Downtime) -sudo -u refood pm2 reload refood-one +# 6. Recarregar o PM2 de forma segura (Zero Downtime) e atualizar variáveis de ambiente +sudo -u refood pm2 reload refood-one --update-env ``` -*Dica: O comando `pm2 reload` reinicia os processos de forma faseada, garantindo que o seu site não fica fora do ar (zero downtime) enquanto a atualização é aplicada.* +*Dica: O comando `pm2 reload` com a flag `--update-env` reinicia os processos de forma faseada, garantindo que o seu site não fica fora do ar (zero downtime) ao mesmo tempo que carrega as novas alterações feitas no ficheiro `.env`.* diff --git a/src/lib/server/odoo.ts b/src/lib/server/odoo.ts index d1ec181..7653483 100644 --- a/src/lib/server/odoo.ts +++ b/src/lib/server/odoo.ts @@ -148,6 +148,7 @@ export async function syncBeneficiaries(): Promise<{ success: boolean; count: nu const parentOdooId = Array.isArray(result.parent_id) ? result.parent_id[0] : null; const isParent = parentOdooId === null; const activeStatus = result.active !== false ? 'ativo' : 'inativo'; + const beneficiaryNumber = isParent ? parsedNumber : null; // Check if beneficiary with odooId already exists const existingById = db @@ -162,7 +163,7 @@ export async function syncBeneficiaries(): Promise<{ success: boolean; count: nu .set({ name, contact, - number: parsedNumber !== null ? parsedNumber : existingById.number, + number: beneficiaryNumber, odooNumber, parentOdooId, isParent, @@ -175,7 +176,7 @@ export async function syncBeneficiaries(): Promise<{ success: boolean; count: nu // Insert new db.insert(schema.beneficiaries) .values({ - number: parsedNumber, + number: beneficiaryNumber, name, contact, odooId, diff --git a/src/routes/admin/beneficiarios/[id]/+page.server.ts b/src/routes/admin/beneficiarios/[id]/+page.server.ts index f94ad85..6a6bb6f 100644 --- a/src/routes/admin/beneficiarios/[id]/+page.server.ts +++ b/src/routes/admin/beneficiarios/[id]/+page.server.ts @@ -74,14 +74,15 @@ export const actions: Actions = { } try { - // Check if new number conflicts with another beneficiary (excluding self) + // Check if new number conflicts with another beneficiary (excluding self and dependents) const existingConflict = db .select() .from(schema.beneficiaries) .where( and( eq(schema.beneficiaries.number, number), - ne(schema.beneficiaries.id, id) + ne(schema.beneficiaries.id, id), + eq(schema.beneficiaries.isParent, true) ) ) .get(); diff --git a/src/routes/admin/beneficiarios/novo/+page.server.ts b/src/routes/admin/beneficiarios/novo/+page.server.ts index c555067..685c61b 100644 --- a/src/routes/admin/beneficiarios/novo/+page.server.ts +++ b/src/routes/admin/beneficiarios/novo/+page.server.ts @@ -1,6 +1,6 @@ import { db } from '$lib/server/db'; import * as schema from '$lib/server/db/schema'; -import { eq } from 'drizzle-orm'; +import { eq, and } from 'drizzle-orm'; import { fail, redirect } from '@sveltejs/kit'; import type { Actions } from './$types'; @@ -39,11 +39,16 @@ export const actions: Actions = { } try { - // Check if number is unique + // Check if number is unique among parent/main beneficiaries const existing = db .select() .from(schema.beneficiaries) - .where(eq(schema.beneficiaries.number, number)) + .where( + and( + eq(schema.beneficiaries.number, number), + eq(schema.beneficiaries.isParent, true) + ) + ) .get(); if (existing) {