MCPcopy Create free account
hub / github.com/cap-js/attachments / waitForScanStatus

Function waitForScanStatus

tests/utils/testUtils.js:12–77  ·  view source on GitHub ↗
(status, attachmentID)

Source from the content-addressed store, hash-verified

10}
11
12async function waitForScanStatus(status, attachmentID) {
13 const db = await cds.connect.to("db")
14 let latestStatus = null
15 return Promise.race([
16 new Promise((resolve) => {
17 let resolved = false
18 const handler = (_res, req) => {
19 // Skip if already resolved to prevent memory buildup
20 if (resolved) return
21
22 if (req.event !== "UPDATE") return
23
24 const data = req.query?.UPDATE?.data
25 if (!data) return
26
27 // Find the status field: either 'status' (composition attachments)
28 // or '<prefix>_status' (inline single attachments)
29 const statusKey = Object.keys(data).find(
30 (k) => k === "status" || k.endsWith("_status"),
31 )
32 if (!statusKey) return
33
34 // Match target: either a composition attachment entity or any entity
35 // with inline attachment fields (identified by having a prefixed _status field)
36 const isAttachmentsTarget =
37 req.target.name.includes(".attachments") ||
38 statusKey.includes("_status")
39 if (!isAttachmentsTarget) return
40
41 // Filter by attachmentID if provided
42 if (
43 attachmentID &&
44 !(
45 (req.query.UPDATE.entity?.ref?.at(-1)?.where &&
46 req.query.UPDATE.entity.ref
47 .at(-1)
48 .where.some((e) => e.val && e.val === attachmentID)) ||
49 (req.query.UPDATE.where &&
50 req.query.UPDATE.where.some(
51 (e) =>
52 (e.val && e.val === attachmentID) ||
53 (e.xpr && e.xpr.some((e) => e.val && e.val === attachmentID)),
54 ))
55 )
56 )
57 return
58
59 latestStatus = data[statusKey]
60
61 if (data[statusKey] === status) {
62 resolved = true
63 resolve(req.query.UPDATE.where || req.query.UPDATE.entity?.ref)
64 }
65 }
66 db.after("*", handler)
67 }),
68 delay(40000).then(async () => {
69 const { messagesAmount } = await SELECT.one

Callers 2

Calls 1

delayFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…