support-requests-app.js 2.1 KB

1234567891011121314151617181920212223242526
  1. // Ownership always comes from the authenticated account, never a selected student/input uid.
  2. async function appSupportRequests(input,current) {
  3. const company=current.get('company')?.id || current.get('company')?.objectId;
  4. const uid=ownLegacyId(current);
  5. if(!company || !uid)fail(403,'账号缺少反馈记录归属');
  6. if(input.action==='app_support_request_read') {
  7. const rows=await Psql.query(`UPDATE "SupportRequestResolution" r SET "readAt"=COALESCE(r."readAt",now())
  8. FROM "Guestbook" g WHERE r.company=$1 AND r."requestId"=$2 AND r."ownerId"=$3
  9. AND g.company=r.company AND g."objectId"=r."requestId" AND g.userid=$3 AND COALESCE(g.status,0)<>-2 RETURNING r."requestId"`,[company,String(input.id||''),uid]);
  10. if(!rows.length)fail(404,'回复不存在或不属于当前账号');
  11. return {read:true};
  12. }
  13. const page=Math.max(1,Math.floor(number(input.page,1))),size=Math.min(50,Math.max(1,Math.floor(number(input.pageSize,10))));
  14. return await Psql.one(`WITH owned AS (
  15. SELECT g."objectId" AS id,g.title,COALESCE(g.tcontent,'') AS content,COALESCE(g.gdate,g."createdAt") AS "submittedAt",
  16. CASE WHEN g.title LIKE '【休假申请】%' THEN 'leave' WHEN g.title LIKE '【意见反馈】%' THEN 'feedback' ELSE 'legacy' END AS kind,
  17. COALESCE(r.status,'pending') AS status,COALESCE(r.comment,'') AS response,r."handledAt",r."readAt",
  18. (r."requestId" IS NOT NULL AND r."readAt" IS NULL) AS unread
  19. FROM "Guestbook" g LEFT JOIN "SupportRequestResolution" r ON r.company=g.company AND r."requestId"=g."objectId" AND r."ownerId"=$2
  20. WHERE g.company=$1 AND g.userid=$2 AND COALESCE(g.parentid,0)=0 AND COALESCE(g.status,0)<>-2
  21. AND (g.cateid=22 OR g."sourceKey" LIKE 'cloud:guestbook_add:%')
  22. ), paged AS (SELECT * FROM owned ORDER BY unread DESC,"submittedAt" DESC NULLS LAST,id DESC LIMIT $3 OFFSET $4)
  23. SELECT COUNT(*)::int AS total,COUNT(*) FILTER(WHERE unread)::int AS unread,
  24. COALESCE((SELECT jsonb_agg(p ORDER BY p.unread DESC,p."submittedAt" DESC NULLS LAST,p.id DESC) FROM paged p),'[]'::jsonb) AS items
  25. FROM owned`,[company,uid,size,(page-1)*size]);
  26. }