From 5a00a42f05b72af163c586fc4b94e6181c05acd0 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Sun, 12 Jul 2026 23:19:42 +0800 Subject: [PATCH] fix: check product_org_auth exists before insert in save_agreement_discount --- wwwroot/api/save_agreement_discount.dspy | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/wwwroot/api/save_agreement_discount.dspy b/wwwroot/api/save_agreement_discount.dspy index 21907b7..10e6e18 100644 --- a/wwwroot/api/save_agreement_discount.dspy +++ b/wwwroot/api/save_agreement_discount.dspy @@ -34,7 +34,7 @@ async with DBPools().sqlorContext(dbname) as sor: 'productid': productid, 'prodtypeid': prodtypeid, 'discount': new_discount, 'created_at': now, }) - # Sync product_org_auth + # Sync product_org_auth (upsert — skip if exists) async with DBPools().sqlorContext(dbname) as s2: ag = await s2.sqlExe( "SELECT sub_reseller_id FROM distribution_agreements WHERE id = ${aid}$", @@ -42,10 +42,15 @@ async with DBPools().sqlorContext(dbname) as sor: ) if ag: async with DBPools().sqlorContext('sage') as s3: - await s3.C('product_org_auth', { - 'id': getID(), 'product_id': productid, - 'org_id': ag[0].sub_reseller_id, - 'auth_source': 'distribution_agreement', 'auth_source_id': new_id, - }) + existing = await s3.sqlExe( + "SELECT id FROM product_org_auth WHERE product_id = ${pid}$ AND org_id = ${oid}$", + {'pid': productid, 'oid': ag[0].sub_reseller_id} + ) + if not existing: + await s3.C('product_org_auth', { + 'id': getID(), 'product_id': productid, + 'org_id': ag[0].sub_reseller_id, + 'auth_source': 'distribution_agreement', 'auth_source_id': new_id, + }) return {'status': 'ok'}