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 -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 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,
@@ -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();
@@ -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) {