corrigir erro na edição do beneficiário

This commit is contained in:
Duarte
2026-06-04 20:20:06 +01:00
parent 1d88b4cb1a
commit 1133775c85
4 changed files with 17 additions and 10 deletions
+3 -3
View File
@@ -267,8 +267,8 @@ sudo -u refood DATABASE_URL=local.db npm run db:push
# 5. Compilar o projeto novamente # 5. Compilar o projeto novamente
sudo -u refood npm run build sudo -u refood npm run build
# 6. Recarregar o PM2 de forma segura (Zero Downtime) # 6. Recarregar o PM2 de forma segura (Zero Downtime) e atualizar variáveis de ambiente
sudo -u refood pm2 reload refood-one 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`.*
+3 -2
View File
@@ -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 parentOdooId = Array.isArray(result.parent_id) ? result.parent_id[0] : null;
const isParent = parentOdooId === null; const isParent = parentOdooId === null;
const activeStatus = result.active !== false ? 'ativo' : 'inativo'; const activeStatus = result.active !== false ? 'ativo' : 'inativo';
const beneficiaryNumber = isParent ? parsedNumber : null;
// Check if beneficiary with odooId already exists // Check if beneficiary with odooId already exists
const existingById = db const existingById = db
@@ -162,7 +163,7 @@ export async function syncBeneficiaries(): Promise<{ success: boolean; count: nu
.set({ .set({
name, name,
contact, contact,
number: parsedNumber !== null ? parsedNumber : existingById.number, number: beneficiaryNumber,
odooNumber, odooNumber,
parentOdooId, parentOdooId,
isParent, isParent,
@@ -175,7 +176,7 @@ export async function syncBeneficiaries(): Promise<{ success: boolean; count: nu
// Insert new // Insert new
db.insert(schema.beneficiaries) db.insert(schema.beneficiaries)
.values({ .values({
number: parsedNumber, number: beneficiaryNumber,
name, name,
contact, contact,
odooId, odooId,
@@ -74,14 +74,15 @@ export const actions: Actions = {
} }
try { 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 const existingConflict = db
.select() .select()
.from(schema.beneficiaries) .from(schema.beneficiaries)
.where( .where(
and( and(
eq(schema.beneficiaries.number, number), eq(schema.beneficiaries.number, number),
ne(schema.beneficiaries.id, id) ne(schema.beneficiaries.id, id),
eq(schema.beneficiaries.isParent, true)
) )
) )
.get(); .get();
@@ -1,6 +1,6 @@
import { db } from '$lib/server/db'; import { db } from '$lib/server/db';
import * as schema from '$lib/server/db/schema'; 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 { fail, redirect } from '@sveltejs/kit';
import type { Actions } from './$types'; import type { Actions } from './$types';
@@ -39,11 +39,16 @@ export const actions: Actions = {
} }
try { try {
// Check if number is unique // Check if number is unique among parent/main beneficiaries
const existing = db const existing = db
.select() .select()
.from(schema.beneficiaries) .from(schema.beneficiaries)
.where(eq(schema.beneficiaries.number, number)) .where(
and(
eq(schema.beneficiaries.number, number),
eq(schema.beneficiaries.isParent, true)
)
)
.get(); .get();
if (existing) { if (existing) {