{"record":{"id":"01e0585ede2e11a7","repo":"hcengineering/platform","slug":"txapplyif-failed","errorCode":null,"errorMessage":"TxApplyIf failed","messagePattern":"TxApplyIf failed","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"foundations/server/packages/middleware/src/applyTx.ts","lineNumber":66,"sourceCode":"        // Wait for scope promise if found\n        const passed =\n          applyIf.scope != null ? await this.verifyApplyIf(ctx, applyIf) : { passed: true, onEnd: () => {} }\n        try {\n          if (passed.passed) {\n            const applyResult: TxApplyResult = {\n              success: true,\n              serverTime: 0\n            }\n            result.push(applyResult)\n\n            const st = Date.now()\n            const r = await this.provideTx(ctx, applyIf.txes)\n            if (Object.keys(r).length > 0) {\n              result.push(r)\n            }\n            applyResult.serverTime = Date.now() - st\n          } else {\n            ctx.warn('TxApplyIf failed', {\n              scope: applyIf.scope,\n              reason: passed.reason,\n              measureName: applyIf.measureName,\n              matchCount: applyIf.match?.length ?? 0,\n              notMatchCount: applyIf.notMatch?.length ?? 0,\n              txCount: applyIf.txes.length\n            })\n            result.push({\n              success: false\n            })\n          }\n        } finally {\n          passed.onEnd()\n        }\n      } else {\n        part.push(tx)\n      }\n    }","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/foundations/server/packages/middleware/src/applyTx.ts#L48-L84","documentation":"TxApplyIf evaluates match/notMatch measure scopes and only applies the wrapped transactions when the condition passes. When the condition does not pass, the server logs this warning with the scope, failure reason, measure names, and match/notMatch/tx counts. It is not an exception — the transactions are simply skipped.","triggerScenarios":"Calling applyTx (or txIf) with an ApplyIf operation whose measure conditions (match/notMatch against the given scope) evaluate to false, with the reason field explaining which check failed.","commonSituations":"Client logic building conditional transactions with wrong measure names; counters already at the expected value so match arrays don't align; measure data not yet committed by a prior op; typos in scope or measure ids.","solutions":["Inspect the logged reason/scope/measureName to see which condition failed","Verify the measure names and scope ids in the applyIf operation match actual model definitions","Check current measure values (matchCount/notMatchCount) to confirm expected state before sending the conditional tx","Split the operation: query first, then send plain tx only when the condition truly holds"],"exampleFix":"// before\nawait client.applyTx(ctx, { txes, applyIf: { scope, measureName: 'members', txes } })\n// after — verify measure state first\nconst res = await client.findAll(ctx, space, query)\nif (res.length === expected) {\n  await client.tx(ctx, txes)\n}","handlingStrategy":"type-guard","validationCode":"const measureRes = await client.findAll(ctx, targetClass, measureQuery)\nif (measureRes.length !== expectedCount) {\n  console.warn('applyIf condition would fail; measure state:', measureRes.length)\n}","typeGuard":"function txWillPass(passed: { reason?: string } | true): passed is true {\n  return passed === true\n}","tryCatchPattern":"const result = await client.applyTx(ctx, ops)\nif (result.some(r => Object.keys(r).length > 0)) {\n  console.warn('applyIf condition failed; tx skipped', result)\n  // re-check state and retry with corrected measures\n}","preventionTips":["Verify measure values before sending conditional transactions","Keep measure names/scope ids in a typed constant module","Log and inspect the `reason` field when conditions fail","Add integration tests covering both match and notMatch paths"],"tags":["transactions","middleware","conditions"],"backgroundTag":"tx-condition-not-met","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}