ddd-ts-kit 0.101.0 → 0.102.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (293) hide show
  1. package/CHANGELOG.md +90 -0
  2. package/README.fr.md +4 -4
  3. package/README.md +3 -3
  4. package/dist/build/emitted-place.d.ts +1 -2
  5. package/dist/build/emitted-place.d.ts.map +1 -1
  6. package/dist/build/emitted-specifier.d.ts +7 -1
  7. package/dist/build/emitted-specifier.d.ts.map +1 -1
  8. package/dist/build/emitted-specifier.js +5 -6
  9. package/dist/build/emitted-specifier.js.map +1 -1
  10. package/dist/build/emitted-target.d.ts +6 -5
  11. package/dist/build/emitted-target.d.ts.map +1 -1
  12. package/dist/build/emitted-target.js +5 -9
  13. package/dist/build/emitted-target.js.map +1 -1
  14. package/dist/build/rewrite-aliases.d.ts.map +1 -1
  15. package/dist/build/rewrite-aliases.js +4 -5
  16. package/dist/build/rewrite-aliases.js.map +1 -1
  17. package/dist/build/rewrite-specifiers.d.ts +7 -6
  18. package/dist/build/rewrite-specifiers.d.ts.map +1 -1
  19. package/dist/build/rewrite-specifiers.js +6 -4
  20. package/dist/build/rewrite-specifiers.js.map +1 -1
  21. package/dist/build/unresolved-problem.d.ts +2 -1
  22. package/dist/build/unresolved-problem.d.ts.map +1 -1
  23. package/dist/build/unresolved-problem.js +2 -2
  24. package/dist/build/unresolved-problem.js.map +1 -1
  25. package/dist/check/business-names/parameter-lists.js +1 -3
  26. package/dist/check/business-names/parameter-lists.js.map +1 -1
  27. package/dist/check/engine/run-check.d.ts +3 -0
  28. package/dist/check/engine/run-check.d.ts.map +1 -1
  29. package/dist/check/engine/run-check.js +1 -1
  30. package/dist/check/engine/run-check.js.map +1 -1
  31. package/dist/check/explain/tenancy-docs.d.ts.map +1 -1
  32. package/dist/check/explain/tenancy-docs.js +13 -3
  33. package/dist/check/explain/tenancy-docs.js.map +1 -1
  34. package/dist/check/fixtures/conjunct-cases.d.ts +11 -0
  35. package/dist/check/fixtures/conjunct-cases.d.ts.map +1 -0
  36. package/dist/check/fixtures/conjunct-cases.js +19 -0
  37. package/dist/check/fixtures/conjunct-cases.js.map +1 -0
  38. package/dist/check/fixtures/run-fixture.d.ts +3 -2
  39. package/dist/check/fixtures/run-fixture.d.ts.map +1 -1
  40. package/dist/check/fixtures/run-fixture.js +3 -3
  41. package/dist/check/fixtures/run-fixture.js.map +1 -1
  42. package/dist/check/fixtures/run-self-check.js +1 -1
  43. package/dist/check/fixtures/run-self-check.js.map +1 -1
  44. package/dist/check/fixtures/tenancy-conjunct-drizzle-fixtures.d.ts +3 -0
  45. package/dist/check/fixtures/tenancy-conjunct-drizzle-fixtures.d.ts.map +1 -0
  46. package/dist/check/fixtures/tenancy-conjunct-drizzle-fixtures.js +33 -0
  47. package/dist/check/fixtures/tenancy-conjunct-drizzle-fixtures.js.map +1 -0
  48. package/dist/check/fixtures/tenancy-conjunct-fixtures.d.ts +4 -0
  49. package/dist/check/fixtures/tenancy-conjunct-fixtures.d.ts.map +1 -0
  50. package/dist/check/fixtures/tenancy-conjunct-fixtures.js +12 -0
  51. package/dist/check/fixtures/tenancy-conjunct-fixtures.js.map +1 -0
  52. package/dist/check/fixtures/tenancy-conjunct-knex-fixtures.d.ts +3 -0
  53. package/dist/check/fixtures/tenancy-conjunct-knex-fixtures.d.ts.map +1 -0
  54. package/dist/check/fixtures/tenancy-conjunct-knex-fixtures.js +49 -0
  55. package/dist/check/fixtures/tenancy-conjunct-knex-fixtures.js.map +1 -0
  56. package/dist/check/fixtures/tenancy-conjunct-prisma-fixtures.d.ts +3 -0
  57. package/dist/check/fixtures/tenancy-conjunct-prisma-fixtures.d.ts.map +1 -0
  58. package/dist/check/fixtures/tenancy-conjunct-prisma-fixtures.js +32 -0
  59. package/dist/check/fixtures/tenancy-conjunct-prisma-fixtures.js.map +1 -0
  60. package/dist/check/fixtures/tenancy-conjunct-typeorm-fixtures.d.ts +3 -0
  61. package/dist/check/fixtures/tenancy-conjunct-typeorm-fixtures.d.ts.map +1 -0
  62. package/dist/check/fixtures/tenancy-conjunct-typeorm-fixtures.js +34 -0
  63. package/dist/check/fixtures/tenancy-conjunct-typeorm-fixtures.js.map +1 -0
  64. package/dist/check/fixtures/tenancy-fixtures.d.ts.map +1 -1
  65. package/dist/check/fixtures/tenancy-fixtures.js +2 -0
  66. package/dist/check/fixtures/tenancy-fixtures.js.map +1 -1
  67. package/dist/check/rules/clean-one-export-per-file.js +1 -0
  68. package/dist/check/rules/clean-one-export-per-file.js.map +1 -1
  69. package/dist/check/rules/schema-no-duplicate-shape.js +1 -3
  70. package/dist/check/rules/schema-no-duplicate-shape.js.map +1 -1
  71. package/dist/check/rules/tenancy-scoped-statement.js +2 -2
  72. package/dist/check/rules/tenancy-scoped-statement.js.map +1 -1
  73. package/dist/check/tenancy/arguments-of-call.d.ts +3 -0
  74. package/dist/check/tenancy/arguments-of-call.d.ts.map +1 -0
  75. package/dist/check/tenancy/arguments-of-call.js +11 -0
  76. package/dist/check/tenancy/arguments-of-call.js.map +1 -0
  77. package/dist/check/tenancy/bare-expression.d.ts +3 -0
  78. package/dist/check/tenancy/bare-expression.d.ts.map +1 -0
  79. package/dist/check/tenancy/bare-expression.js +9 -0
  80. package/dist/check/tenancy/bare-expression.js.map +1 -0
  81. package/dist/check/tenancy/bracket-depth.d.ts +3 -0
  82. package/dist/check/tenancy/bracket-depth.d.ts.map +1 -0
  83. package/dist/check/tenancy/bracket-depth.js +10 -0
  84. package/dist/check/tenancy/bracket-depth.js.map +1 -0
  85. package/dist/check/tenancy/chain-links.d.ts +17 -0
  86. package/dist/check/tenancy/chain-links.d.ts.map +1 -0
  87. package/dist/check/tenancy/chain-links.js +24 -0
  88. package/dist/check/tenancy/chain-links.js.map +1 -0
  89. package/dist/check/tenancy/choice-branches.d.ts +3 -0
  90. package/dist/check/tenancy/choice-branches.d.ts.map +1 -0
  91. package/dist/check/tenancy/choice-branches.js +39 -0
  92. package/dist/check/tenancy/choice-branches.js.map +1 -0
  93. package/dist/check/tenancy/declaration-before.d.ts +12 -0
  94. package/dist/check/tenancy/declaration-before.d.ts.map +1 -0
  95. package/dist/check/tenancy/declaration-before.js +11 -0
  96. package/dist/check/tenancy/declaration-before.js.map +1 -0
  97. package/dist/check/tenancy/declaration-of.d.ts +1 -5
  98. package/dist/check/tenancy/declaration-of.d.ts.map +1 -1
  99. package/dist/check/tenancy/declaration-of.js +3 -8
  100. package/dist/check/tenancy/declaration-of.js.map +1 -1
  101. package/dist/check/tenancy/drizzle-unscoped.d.ts.map +1 -1
  102. package/dist/check/tenancy/drizzle-unscoped.js +16 -3
  103. package/dist/check/tenancy/drizzle-unscoped.js.map +1 -1
  104. package/dist/check/tenancy/enclosed-by.d.ts +3 -0
  105. package/dist/check/tenancy/enclosed-by.d.ts.map +1 -0
  106. package/dist/check/tenancy/enclosed-by.js +6 -0
  107. package/dist/check/tenancy/enclosed-by.js.map +1 -0
  108. package/dist/check/tenancy/end-of-enclosing.d.ts +7 -0
  109. package/dist/check/tenancy/end-of-enclosing.d.ts.map +1 -0
  110. package/dist/check/tenancy/end-of-enclosing.js +18 -0
  111. package/dist/check/tenancy/end-of-enclosing.js.map +1 -0
  112. package/dist/check/tenancy/holds-condition.d.ts +14 -0
  113. package/dist/check/tenancy/holds-condition.d.ts.map +1 -0
  114. package/dist/check/tenancy/holds-condition.js +26 -0
  115. package/dist/check/tenancy/holds-condition.js.map +1 -0
  116. package/dist/check/tenancy/is-conjunct.d.ts +8 -0
  117. package/dist/check/tenancy/is-conjunct.d.ts.map +1 -0
  118. package/dist/check/tenancy/is-conjunct.js +17 -0
  119. package/dist/check/tenancy/is-conjunct.js.map +1 -0
  120. package/dist/check/tenancy/is-or-link.d.ts +4 -0
  121. package/dist/check/tenancy/is-or-link.d.ts.map +1 -0
  122. package/dist/check/tenancy/is-or-link.js +7 -0
  123. package/dist/check/tenancy/is-or-link.js.map +1 -0
  124. package/dist/check/tenancy/knex-links-outside-condition.d.ts +11 -0
  125. package/dist/check/tenancy/knex-links-outside-condition.d.ts.map +1 -0
  126. package/dist/check/tenancy/knex-links-outside-condition.js +108 -0
  127. package/dist/check/tenancy/knex-links-outside-condition.js.map +1 -0
  128. package/dist/check/tenancy/knex-unscoped.d.ts +3 -1
  129. package/dist/check/tenancy/knex-unscoped.d.ts.map +1 -1
  130. package/dist/check/tenancy/knex-unscoped.js +12 -2
  131. package/dist/check/tenancy/knex-unscoped.js.map +1 -1
  132. package/dist/check/tenancy/prisma-unscoped.d.ts.map +1 -1
  133. package/dist/check/tenancy/prisma-unscoped.js +15 -10
  134. package/dist/check/tenancy/prisma-unscoped.js.map +1 -1
  135. package/dist/check/tenancy/spreads-at-top.d.ts +9 -0
  136. package/dist/check/tenancy/spreads-at-top.d.ts.map +1 -0
  137. package/dist/check/tenancy/spreads-at-top.js +49 -0
  138. package/dist/check/tenancy/spreads-at-top.js.map +1 -0
  139. package/dist/check/tenancy/top-level-parts.d.ts +3 -0
  140. package/dist/check/tenancy/top-level-parts.d.ts.map +1 -0
  141. package/dist/check/tenancy/top-level-parts.js +13 -0
  142. package/dist/check/tenancy/top-level-parts.js.map +1 -0
  143. package/dist/check/tenancy/typeorm-unscoped.d.ts.map +1 -1
  144. package/dist/check/tenancy/typeorm-unscoped.js +30 -16
  145. package/dist/check/tenancy/typeorm-unscoped.js.map +1 -1
  146. package/dist/check/tenancy/unconfined-where.d.ts +11 -0
  147. package/dist/check/tenancy/unconfined-where.d.ts.map +1 -0
  148. package/dist/check/tenancy/unconfined-where.js +18 -0
  149. package/dist/check/tenancy/unconfined-where.js.map +1 -0
  150. package/dist/config/read-config.d.ts.map +1 -1
  151. package/dist/config/read-config.js +20 -0
  152. package/dist/config/read-config.js.map +1 -1
  153. package/dist/declared/render-declarations.js +1 -3
  154. package/dist/declared/render-declarations.js.map +1 -1
  155. package/dist/describe/read-controllers.d.ts.map +1 -1
  156. package/dist/describe/read-controllers.js +18 -2
  157. package/dist/describe/read-controllers.js.map +1 -1
  158. package/dist/describe/schema-class-of.js +8 -1
  159. package/dist/describe/schema-class-of.js.map +1 -1
  160. package/dist/digest/tree-at.d.ts.map +1 -1
  161. package/dist/digest/tree-at.js +2 -0
  162. package/dist/digest/tree-at.js.map +1 -1
  163. package/dist/docs/context-sections.js +3 -7
  164. package/dist/docs/context-sections.js.map +1 -1
  165. package/dist/docs/personal-event-lines.d.ts +7 -0
  166. package/dist/docs/personal-event-lines.d.ts.map +1 -0
  167. package/dist/docs/personal-event-lines.js +34 -0
  168. package/dist/docs/personal-event-lines.js.map +1 -0
  169. package/dist/docs/write-docs.d.ts.map +1 -1
  170. package/dist/docs/write-docs.js +2 -0
  171. package/dist/docs/write-docs.js.map +1 -1
  172. package/dist/docs/write-personal-events.d.ts +11 -0
  173. package/dist/docs/write-personal-events.d.ts.map +1 -0
  174. package/dist/docs/write-personal-events.js +28 -0
  175. package/dist/docs/write-personal-events.js.map +1 -0
  176. package/dist/generate/kinds/assert-event-store.d.ts +5 -1
  177. package/dist/generate/kinds/assert-event-store.d.ts.map +1 -1
  178. package/dist/generate/kinds/assert-event-store.js +11 -5
  179. package/dist/generate/kinds/assert-event-store.js.map +1 -1
  180. package/dist/generate/kinds/background/background-job-vars.js +4 -2
  181. package/dist/generate/kinds/background/background-job-vars.js.map +1 -1
  182. package/dist/generate/kinds/generate-event-sourced-repository.js +8 -1
  183. package/dist/generate/kinds/generate-event-sourced-repository.js.map +1 -1
  184. package/dist/generate/kinds/route/controller-vars.js +1 -1
  185. package/dist/generate/kinds/route/controller-vars.js.map +1 -1
  186. package/dist/generate/kinds/route/message-call.d.ts +6 -2
  187. package/dist/generate/kinds/route/message-call.d.ts.map +1 -1
  188. package/dist/generate/kinds/route/message-call.js +2 -1
  189. package/dist/generate/kinds/route/message-call.js.map +1 -1
  190. package/dist/generate/kinds/route/standard-schema-of.d.ts +2 -1
  191. package/dist/generate/kinds/route/standard-schema-of.d.ts.map +1 -1
  192. package/dist/generate/kinds/route/standard-schema-of.js +2 -4
  193. package/dist/generate/kinds/route/standard-schema-of.js.map +1 -1
  194. package/dist/generate/kinds/service-kind.js +1 -1
  195. package/dist/generate/kinds/service-kind.js.map +1 -1
  196. package/dist/generator-lock/unreadable-lock-refusal.d.ts +4 -0
  197. package/dist/generator-lock/unreadable-lock-refusal.d.ts.map +1 -0
  198. package/dist/generator-lock/unreadable-lock-refusal.js +7 -0
  199. package/dist/generator-lock/unreadable-lock-refusal.js.map +1 -0
  200. package/dist/generator-lock/with-generator-lock.d.ts.map +1 -1
  201. package/dist/generator-lock/with-generator-lock.js +8 -7
  202. package/dist/generator-lock/with-generator-lock.js.map +1 -1
  203. package/dist/init/files/platform-files.d.ts.map +1 -1
  204. package/dist/init/files/platform-files.js +14 -0
  205. package/dist/init/files/platform-files.js.map +1 -1
  206. package/dist/init/files/privacy-files.d.ts +2 -2
  207. package/dist/init/files/privacy-files.d.ts.map +1 -1
  208. package/dist/init/files/privacy-files.js +3 -2
  209. package/dist/init/files/privacy-files.js.map +1 -1
  210. package/dist/init/manifest/versions.js +24 -24
  211. package/dist/measures/timeline-of.js +1 -0
  212. package/dist/measures/timeline-of.js.map +1 -1
  213. package/dist/progress/format-progress.js +1 -0
  214. package/dist/progress/format-progress.js.map +1 -1
  215. package/dist/sdk/planned-clients.d.ts +2 -2
  216. package/dist/sdk/planned-clients.d.ts.map +1 -1
  217. package/dist/sdk/planned-clients.js +2 -6
  218. package/dist/sdk/planned-clients.js.map +1 -1
  219. package/dist/sdk/set-up-sdk.d.ts.map +1 -1
  220. package/dist/sdk/set-up-sdk.js +3 -2
  221. package/dist/sdk/set-up-sdk.js.map +1 -1
  222. package/dist/stack/stack-vars.js +12 -3
  223. package/dist/stack/stack-vars.js.map +1 -1
  224. package/dist/stories/forecast-merge.js +1 -0
  225. package/dist/stories/forecast-merge.js.map +1 -1
  226. package/dist/version.d.ts +1 -1
  227. package/dist/version.js +1 -1
  228. package/dist/worker/format-resume.d.ts.map +1 -1
  229. package/dist/worker/format-resume.js +5 -0
  230. package/dist/worker/format-resume.js.map +1 -1
  231. package/dist/worker/format-workers.js +5 -0
  232. package/dist/worker/format-workers.js.map +1 -1
  233. package/dist/worker/progress-of.d.ts +8 -3
  234. package/dist/worker/progress-of.d.ts.map +1 -1
  235. package/dist/worker/progress-of.js +2 -16
  236. package/dist/worker/progress-of.js.map +1 -1
  237. package/dist/worker/read-agent-record.d.ts.map +1 -1
  238. package/dist/worker/read-agent-record.js +15 -3
  239. package/dist/worker/read-agent-record.js.map +1 -1
  240. package/dist/worker/read-workers.js +2 -2
  241. package/dist/worker/read-workers.js.map +1 -1
  242. package/dist/worker/readable-progress.d.ts +5 -0
  243. package/dist/worker/readable-progress.d.ts.map +1 -0
  244. package/dist/worker/readable-progress.js +16 -0
  245. package/dist/worker/readable-progress.js.map +1 -0
  246. package/dist/worker/resume-worker.d.ts.map +1 -1
  247. package/dist/worker/resume-worker.js +4 -5
  248. package/dist/worker/resume-worker.js.map +1 -1
  249. package/dist/worker/worker-progress.d.ts +2 -1
  250. package/dist/worker/worker-progress.d.ts.map +1 -1
  251. package/dist/workspace/real-path-of.d.ts +2 -2
  252. package/dist/workspace/real-path-of.d.ts.map +1 -1
  253. package/dist/workspace/real-path-of.js +15 -10
  254. package/dist/workspace/real-path-of.js.map +1 -1
  255. package/package.json +5 -5
  256. package/templates/docs/privacy-register.md.tpl +2 -0
  257. package/templates/init/README.md.tpl +13 -4
  258. package/templates/init/docker/env.example.tpl +8 -0
  259. package/templates/init/main.ts.tpl +3 -0
  260. package/templates/init/platform/audit/create-audit.ts.tpl +12 -6
  261. package/templates/init/platform/composition/compose-application.manual.ts.tpl +36 -0
  262. package/templates/init/platform/composition/compose-application.tsyringe.ts.tpl +7 -0
  263. package/templates/init/platform/composition/lay-foundations.manual.ts.tpl +1 -1
  264. package/templates/init/platform/composition/lay-foundations.tsyringe.ts.tpl +1 -1
  265. package/templates/init/platform/composition/platform-options.manual.ts.tpl +7 -0
  266. package/templates/init/platform/composition/platform.manual.ts.tpl +7 -0
  267. package/templates/init/platform/composition/platform.module.tsyringe.ts.tpl +7 -0
  268. package/templates/init/platform/config/app-config.ts.tpl +7 -0
  269. package/templates/init/platform/config/load-config.ts.tpl +22 -0
  270. package/templates/init/platform/database/persistence.contract.spec.ts.tpl +14 -3
  271. package/templates/init/platform/event-sourcing/create-event-sourcing.ts.tpl +5 -1
  272. package/templates/init/platform/jobs/create-background-work.ts.tpl +12 -0
  273. package/templates/init/platform/jobs/days-before.ts.tpl +8 -0
  274. package/templates/init/platform/jobs/housekeeping.module.tsyringe.ts.tpl +46 -0
  275. package/templates/init/platform/jobs/housekeeping.ts.tpl +24 -0
  276. package/templates/init/platform/jobs/job-settings.ts.tpl +6 -0
  277. package/templates/init/platform/privacy/personal-events.ts.tpl +11 -0
  278. package/templates/init/platform/privacy/run-erasure.manual.ts.tpl +69 -19
  279. package/templates/init/platform/privacy/run-erasure.tsyringe.ts.tpl +69 -8
  280. package/templates/init/platform/privacy/run-export.manual.ts.tpl +2 -2
  281. package/templates/init/platform/privacy/run-export.tsyringe.ts.tpl +4 -2
  282. package/templates/init/platform/privacy/run-purge.manual.ts.tpl +3 -2
  283. package/templates/init/platform/privacy/run-purge.tsyringe.ts.tpl +4 -2
  284. package/templates/init/rules/infrastructure.md.tpl +1 -1
  285. package/templates/init/scripts/privacy-erase.manual.ts.tpl +52 -7
  286. package/templates/init/scripts/privacy-erase.tsyringe.ts.tpl +52 -7
  287. package/templates/init/scripts/privacy-export.manual.ts.tpl +4 -0
  288. package/templates/init/scripts/privacy-export.tsyringe.ts.tpl +4 -0
  289. package/templates/init/scripts/privacy-purge.manual.ts.tpl +4 -0
  290. package/templates/init/scripts/privacy-purge.tsyringe.ts.tpl +4 -0
  291. package/templates/pipeline/package-scripts.json +1 -1
  292. package/templates/pipeline/stryker.config.mjs.tpl +4 -0
  293. package/templates/worker/main.ts.tpl +3 -0
@@ -79,7 +79,7 @@ export function layFoundations(persistence: Persistence, options: PlatformOption
79
79
  const lock = new InMemoryLock(clock);
80
80
  {{/unless}}
81
81
  {{#if audit.on}}
82
- const audit = createAudit(persistence, { scope, clock, ids });
82
+ const audit = createAudit(persistence, { scope, clock, ids }, logger);
83
83
  {{/if}}
84
84
 
85
85
  {{#if tenancy.any}}
@@ -64,7 +64,7 @@ export function layFoundations(persistence: Persistence, options: Observed): Fou
64
64
  const ids = new UuidGenerator();
65
65
  const logger = createLogger(scope, options.log);
66
66
  {{#if audit.on}}
67
- const audit = createAudit(persistence, { scope, clock, ids });
67
+ const audit = createAudit(persistence, { scope, clock, ids }, logger);
68
68
  {{/if}}
69
69
 
70
70
  return {
@@ -17,6 +17,9 @@ import type { JobSettings } from '@ddd-ts-kit/kernel';
17
17
  import type { QueueWorkerOptions } from '@ddd-ts-kit/scheduling';
18
18
  import type { ConsumerTrace } from '@ddd-ts-kit/tracing';
19
19
  import type { EventProvider } from '{{alias.platform}}/events/event-provider{{alias.ext}}';
20
+ {{#if housekeeping.any}}
21
+ import type { Housekeeping } from '{{alias.platform}}/jobs/housekeeping{{alias.ext}}';
22
+ {{/if}}
20
23
  import type { LogSettings } from '{{alias.platform}}/observability/create-logger{{alias.ext}}';
21
24
  {{#if cache.redis}}
22
25
  import type { RedisServices } from '{{alias.platform}}/redis/open-redis{{alias.ext}}';
@@ -35,6 +38,10 @@ export interface PlatformOptions {
35
38
  readonly events: EventProvider;
36
39
  /** Where log entries go; left out, the application writes nothing. */
37
40
  readonly log?: LogSettings;
41
+ {{#if housekeeping.any}}
42
+ /** How long the platform keeps what it is done with, in days: the configuration in `main.ts`; left out (the tools, the tests), thirty. */
43
+ readonly housekeeping?: Housekeeping;
44
+ {{/if}}
38
45
  {{#if errors.sentry}}
39
46
  /** Where unexpected failures are reported: the reporter of `errors.ts` in `main.ts`; left out (the tools, the tests), nowhere. */
40
47
  readonly errors?: ErrorReporter;
@@ -26,6 +26,9 @@ import type {
26
26
  RateLimiter,
27
27
  {{/if}}
28
28
  } from '@ddd-ts-kit/kernel';
29
+ {{#if queue.stored}}
30
+ import type { JobStore } from '@ddd-ts-kit/scheduling';
31
+ {{/if}}
29
32
  {{#if audit.on}}
30
33
  import type { Audit } from '{{alias.platform}}/audit/create-audit{{alias.ext}}';
31
34
  {{/if}}
@@ -89,6 +92,10 @@ export interface Platform {
89
92
  readonly jobs: JobScheduler;
90
93
  /** What a use case enqueues through: a background job is written with its command and run later by the worker, as the actor that enqueued it. */
91
94
  readonly jobQueue: JobQueue;
95
+ {{#if queue.stored}}
96
+ /** Where the jobs wait: what the purge of what finished and the erasure of a person reach. */
97
+ readonly jobStore: JobStore;
98
+ {{/if}}
92
99
  /** Where a context registers its background jobs; the application starts it when this process runs the jobs. */
93
100
  readonly backgroundJobs: BackgroundJobs;
94
101
  /** Runs the steps of the process managers, in the unit of work of the step, and delivers their timeouts through the queue. */
@@ -83,6 +83,9 @@ import type { Persistence } from '{{alias.platform}}/database/persistence{{alias
83
83
  import { platformErrorMapper } from '{{alias.platform}}/composition/platform-error-mapper{{alias.ext}}';
84
84
  {{/if}}
85
85
  import type { EventProvider } from '{{alias.platform}}/events/event-provider{{alias.ext}}';
86
+ {{#if housekeeping.any}}
87
+ import type { Housekeeping } from '{{alias.platform}}/jobs/housekeeping{{alias.ext}}';
88
+ {{/if}}
86
89
  import type { LogSettings } from '{{alias.platform}}/observability/create-logger{{alias.ext}}';
87
90
  {{#if cache.redis}}
88
91
  import type { RedisServices } from '{{alias.platform}}/redis/open-redis{{alias.ext}}';
@@ -105,6 +108,10 @@ export interface PlatformOptions {
105
108
  {{/if}}
106
109
  /** Where log entries go; left out, the application writes nothing. */
107
110
  readonly log?: LogSettings;
111
+ {{#if housekeeping.any}}
112
+ /** How long the platform keeps what it is done with, in days: the configuration in `main.ts`; left out (the tools, the tests), thirty. */
113
+ readonly housekeeping?: Housekeeping;
114
+ {{/if}}
108
115
  /** The policy around every adapter that leaves the process; left out, `resilience/default-resilience-settings.ts`. */
109
116
  readonly resilience?: ResilienceSettings;
110
117
  /** Whether a consumed event continues the trace of its publication (the default) or starts one linked to it. */
@@ -9,6 +9,9 @@ import type { ResilienceSettings } from '{{alias.platform}}/resilience/resilienc
9
9
  import type { FilesConfig } from '{{alias.platform}}/config/read-files{{alias.ext}}';
10
10
  {{/if}}
11
11
  import type { TracingConfig } from '{{alias.platform}}/config/read-tracing{{alias.ext}}';
12
+ {{#if housekeeping.any}}
13
+ import type { Housekeeping } from '{{alias.platform}}/jobs/housekeeping{{alias.ext}}';
14
+ {{/if}}
12
15
 
13
16
  /** The validated configuration of the process: `loadConfig` is the only reader of the environment. */
14
17
  export interface AppConfig {
@@ -36,6 +39,10 @@ export interface AppConfig {
36
39
  {{#if events.outbox}}
37
40
  readonly outboxRelayIntervalMs: number;
38
41
  {{/if}}
42
+ {{#if housekeeping.any}}
43
+ /** How long the platform keeps what it is done with, in days (`OUTBOX_RETENTION_DAYS`, `BACKGROUND_JOBS_RETENTION_DAYS`). */
44
+ readonly housekeeping: Housekeeping;
45
+ {{/if}}
39
46
  {{#if docs.any}}
40
47
  readonly docsEnabled: boolean;
41
48
  {{/if}}
@@ -15,6 +15,9 @@ import { Environment, type Variables } from '{{alias.platform}}/config/environme
15
15
  import { readFiles } from '{{alias.platform}}/config/read-files{{alias.ext}}';
16
16
  {{/if}}
17
17
  import { readTracing } from '{{alias.platform}}/config/read-tracing{{alias.ext}}';
18
+ {{#if housekeeping.any}}
19
+ import { DEFAULT_HOUSEKEEPING, type Housekeeping } from '{{alias.platform}}/jobs/housekeeping{{alias.ext}}';
20
+ {{/if}}
18
21
 
19
22
  const environments = ['development', 'test', 'production'] as const;
20
23
  {{#if http.any}}
@@ -62,6 +65,9 @@ export function loadConfig(variables: Variables): AppConfig {
62
65
  {{#if events.outbox}}
63
66
  outboxRelayIntervalMs: env.integer('OUTBOX_RELAY_INTERVAL_MS', { fallback: 500, min: 10, max: 60000 }),
64
67
  {{/if}}
68
+ {{#if housekeeping.any}}
69
+ housekeeping: readHousekeeping(env),
70
+ {{/if}}
65
71
  {{#if docs.any}}
66
72
  docsEnabled: env.flag('API_DOCS', environment !== 'production'),
67
73
  {{/if}}
@@ -110,6 +116,22 @@ export function loadConfig(variables: Variables): AppConfig {
110
116
  return config;
111
117
  }
112
118
 
119
+ {{#if housekeeping.any}}
120
+ /** How long the platform keeps what it is done with, in days; past that, the nightly purges drop it. */
121
+ function readHousekeeping(env: Environment): Housekeeping {
122
+ const days = (name: string, fallback: number): number => env.integer(name, { fallback, min: 1, max: 3650 });
123
+
124
+ return {
125
+ {{#if events.outbox}}
126
+ publishedEventsDays: days('OUTBOX_RETENTION_DAYS', DEFAULT_HOUSEKEEPING.publishedEventsDays),
127
+ {{/if}}
128
+ {{#if queue.stored}}
129
+ finishedJobsDays: days('BACKGROUND_JOBS_RETENTION_DAYS', DEFAULT_HOUSEKEEPING.finishedJobsDays),
130
+ {{/if}}
131
+ };
132
+ }
133
+
134
+ {{/if}}
113
135
  {{#if tenancy.any}}
114
136
  /** `TENANTS=acme,globex`: each id in the grammar of a tenant id. */
115
137
  function readTenants(env: Environment): readonly string[] {
@@ -5,7 +5,7 @@ import { FixedExecutionScope, executionContext, systemActor } from '@ddd-ts-kit/
5
5
  {{#unless queue.processTable}}
6
6
  {{#if tenancy.any}}
7
7
  {{#if eventSourcing.on}}
8
- import { FixedExecutionScope } from '@ddd-ts-kit/kernel';
8
+ import { FixedExecutionScope, executionContext, systemActor } from '@ddd-ts-kit/kernel';
9
9
  {{/if}}
10
10
  {{/if}}
11
11
  {{/unless}}
@@ -74,8 +74,8 @@ processedEventStoreContract(runner, {
74
74
  eventStoreContract(runner, {
75
75
  name: 'The event store of the platform',
76
76
  {{#if tenancy.any}}
77
- createStore: async () => (await emptied()).eventSourcing(new FixedExecutionScope()).events,
78
- createCompanions: async () => (await emptied()).eventSourcing(new FixedExecutionScope()),
77
+ createStore: async () => (await emptied()).eventSourcing(inOneTenant()).events,
78
+ createCompanions: async () => (await emptied()).eventSourcing(inOneTenant()),
79
79
  createTenantStore: async () => {
80
80
  const scope = new FixedExecutionScope();
81
81
 
@@ -129,3 +129,14 @@ async function emptied(): Promise<Persistence> {
129
129
 
130
130
  return database.persistence;
131
131
  }
132
+ {{#if tenancy.any}}
133
+ {{#if eventSourcing.on}}
134
+
135
+ /** A scope in one tenant: the event store refuses work that names none, and its stream suites run in this one. */
136
+ function inOneTenant(): FixedExecutionScope {
137
+ return new FixedExecutionScope(
138
+ executionContext({ correlationId: 'contract', actor: systemActor('contract'), origin: 'test', tenantId: 'acme' }),
139
+ );
140
+ }
141
+ {{/if}}
142
+ {{/if}}
@@ -28,7 +28,11 @@ export function createEventSourcing(persistence: Persistence, audit: Audit, sour
28
28
  {{#unless tenancy.any}}
29
29
  const stores = persistence.eventSourcing;
30
30
  {{/unless}}
31
- const subjectKeys = auditedSubjectKeys(stores.subjectKeys, audit.trail, source);
31
+
32
+ const subjectKeys = auditedSubjectKeys(stores.subjectKeys, audit.trail, {
33
+ ...source,
34
+ onLostRecord: audit.onLostRecord,
35
+ });
32
36
 
33
37
  return { ...stores, subjectKeys, cipher: new PersonalDataCipher(subjectKeys) };
34
38
  }
@@ -40,6 +40,10 @@ import type { BackgroundJobs } from '{{alias.platform}}/jobs/background-jobs{{al
40
40
  export interface BackgroundWork {
41
41
  /** What a use case enqueues through, in the transaction of its command when the store shares it. */
42
42
  readonly jobQueue: JobQueue;
43
+ {{#if queue.stored}}
44
+ /** Where the jobs wait: what the purge of what finished and the erasure of a person reach. */
45
+ readonly jobStore: JobStore;
46
+ {{/if}}
43
47
  readonly backgroundJobs: BackgroundJobs;
44
48
  /** Runs the steps of the process managers and delivers their timeouts. */
45
49
  readonly processes: ProcessRunner;
@@ -82,10 +86,18 @@ export function createBackgroundWork({
82
86
  {{#if queue.memory}}
83
87
  const store = new InMemoryJobStore();
84
88
  {{/if}}
89
+ {{#if tenancy.any}}
90
+ const jobQueue = new StoredJobQueue({ store, clock, ids, scope, tenant: true, traceparent: traced.traceparent });
91
+ {{/if}}
92
+ {{#unless tenancy.any}}
85
93
  const jobQueue = new StoredJobQueue({ store, clock, ids, scope, traceparent: traced.traceparent });
94
+ {{/unless}}
86
95
 
87
96
  return {
88
97
  jobQueue,
98
+ {{#if queue.stored}}
99
+ jobStore: store,
100
+ {{/if}}
89
101
  backgroundJobs: backgroundJobsOn(workerOn(store, options, foundations), traced),
90
102
  processes: new StoredProcessRunner({
91
103
  {{#if queue.processTable}}
@@ -0,0 +1,8 @@
1
+ import type { Clock } from '@ddd-ts-kit/kernel';
2
+
3
+ const DAY_MS = 86_400_000;
4
+
5
+ /** The instant before which a purge reaches what finished. */
6
+ export function daysBefore(clock: Clock, days: number): Date {
7
+ return new Date(clock.now().getTime() - days * DAY_MS);
8
+ }
@@ -0,0 +1,46 @@
1
+ import { defineContextModule, type ContextModule } from '@ddd-ts-kit/di';
2
+ {{#if events.outbox}}
3
+ import { OutboxStore } from '@ddd-ts-kit/events';
4
+ {{/if}}
5
+ import { Clock } from '@ddd-ts-kit/kernel';
6
+ {{#if queue.stored}}
7
+ import { JobStore } from '@ddd-ts-kit/scheduling';
8
+ {{/if}}
9
+ import { daysBefore } from '{{alias.platform}}/jobs/days-before{{alias.ext}}';
10
+ import type { Housekeeping } from '{{alias.platform}}/jobs/housekeeping{{alias.ext}}';
11
+
12
+ /**
13
+ * What the platform is done with, purged every night{{#if events.outbox}}: `events.purge-published` at 03:30 UTC (switched off in
14
+ * one environment with `JOB_EVENTS_PURGE_PUBLISHED_ENABLED=false`){{/if}}{{#if queue.stored}}{{#if events.outbox}},{{/if}}{{#unless events.outbox}}:{{/unless}} `jobs.purge-finished` at 03:45 UTC
15
+ * (`JOB_JOBS_PURGE_FINISHED_ENABLED=false`){{/if}}. A record and a job row keep their payload until then.
16
+ */
17
+ export function housekeepingModule(
18
+ retention: Housekeeping,
19
+ { jobs = true }: { readonly jobs?: boolean } = {},
20
+ ): ContextModule {
21
+ return defineContextModule({
22
+ name: 'housekeeping',
23
+ register(builder) {
24
+ // Registered by the process that runs the jobs of the platform; a service of the deployment passes `jobs: false`.
25
+ if (!jobs) {
26
+ return;
27
+ }
28
+ {{#if events.outbox}}
29
+
30
+ builder.job('events.purge-published', { cron: '30 3 * * *', tenancy: 'global' }).handledByFactory((resolve) => ({
31
+ run: async () => {
32
+ await resolve(OutboxStore).purgePublished(daysBefore(resolve(Clock), retention.publishedEventsDays));
33
+ },
34
+ }));
35
+ {{/if}}
36
+ {{#if queue.stored}}
37
+
38
+ builder.job('jobs.purge-finished', { cron: '45 3 * * *', tenancy: 'global' }).handledByFactory((resolve) => ({
39
+ run: async () => {
40
+ await resolve(JobStore).purgeFinished(daysBefore(resolve(Clock), retention.finishedJobsDays));
41
+ },
42
+ }));
43
+ {{/if}}
44
+ },
45
+ });
46
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * How long the platform keeps what it is done with, in days, read from the environment by
3
+ * `load-config.ts`. A row holds what its payload held: {{#if events.outbox}}`events.purge-published` drops the records of the
4
+ * outbox past that age{{/if}}{{#if events.outbox}}{{#if queue.stored}}, {{/if}}{{/if}}{{#if queue.stored}}`jobs.purge-finished` the jobs that succeeded or were cancelled{{/if}}, every night. What failed for
5
+ * good stays until an operator decides.
6
+ */
7
+ export interface Housekeeping {
8
+ {{#if events.outbox}}
9
+ readonly publishedEventsDays: number;
10
+ {{/if}}
11
+ {{#if queue.stored}}
12
+ readonly finishedJobsDays: number;
13
+ {{/if}}
14
+ }
15
+
16
+ /** Left out (the tools, the tests), thirty days of each. */
17
+ export const DEFAULT_HOUSEKEEPING: Housekeeping = {
18
+ {{#if events.outbox}}
19
+ publishedEventsDays: 30,
20
+ {{/if}}
21
+ {{#if queue.stored}}
22
+ finishedJobsDays: 30,
23
+ {{/if}}
24
+ };
@@ -29,6 +29,12 @@ export function jobSettings(environment: Environment, context?: string): Readonl
29
29
  {{/if}}
30
30
  {{#if privacy.on}}
31
31
  'privacy.purge-expired': { enabled: environment['JOB_PRIVACY_PURGE_EXPIRED_ENABLED'] !== 'false' },
32
+ {{/if}}
33
+ {{#if events.outbox}}
34
+ 'events.purge-published': { enabled: environment['JOB_EVENTS_PURGE_PUBLISHED_ENABLED'] !== 'false' },
35
+ {{/if}}
36
+ {{#if queue.stored}}
37
+ 'jobs.purge-finished': { enabled: environment['JOB_JOBS_PURGE_FINISHED_ENABLED'] !== 'false' },
32
38
  {{/if}}
33
39
  // ddd-ts-kit:jobs
34
40
  // /ddd-ts-kit:jobs
@@ -0,0 +1,11 @@
1
+ import type { PersonalEvent } from '@ddd-ts-kit/events';
2
+
3
+ /**
4
+ * The integration events that carry personal data, as the `personal` option of each one declares it:
5
+ * what an erasure looks for in the records of the outbox. Written by `ddd-ts-kit docs`, like the
6
+ * register, and held to the declarations by `docs --check`.
7
+ */
8
+ export const personalEvents: readonly PersonalEvent[] = [
9
+ // ddd-ts-kit:personal-events
10
+ // /ddd-ts-kit:personal-events
11
+ ];
@@ -1,33 +1,48 @@
1
- {{#if tenancy.any}}
1
+ {{#if audit.on}}
2
+ import { auditedPersonalDataErasure } from '@ddd-ts-kit/audit';
3
+ {{/if}}
4
+ {{#if events.outbox}}
5
+ import { personalEventsInOutbox } from '@ddd-ts-kit/events';
6
+ {{/if}}
2
7
  import {
3
8
  dataSubjectErasedV1,
9
+ {{#if tenancy.any}}
4
10
  eachTenant,
11
+ {{/if}}
5
12
  eraseDataSubject,
6
13
  executionContext,
14
+ {{#if tenancy.any}}
7
15
  inEachTenant,
8
- type Actor,
9
- type DataSubjectErasureReport,
10
- } from '@ddd-ts-kit/kernel';
11
16
  {{/if}}
12
- {{#unless tenancy.any}}
13
- import {
14
- dataSubjectErasedV1,
15
- eraseDataSubject,
16
- executionContext,
17
17
  type Actor,
18
18
  type DataSubjectErasureReport,
19
- } from '@ddd-ts-kit/kernel';
19
+ {{#if housekeeping.any}}
20
+ type PersonalDataCopy,
21
+ {{/if}}
22
+ {{#unless housekeeping.any}}
23
+ {{#if audit.on}}
24
+ type PersonalDataCopy,
25
+ {{/if}}
20
26
  {{/unless}}
27
+ } from '@ddd-ts-kit/kernel';
28
+ {{#if queue.stored}}
29
+ import { jobsOfSubject } from '@ddd-ts-kit/scheduling';
30
+ {{/if}}
21
31
  import type { Platform } from '{{alias.platform}}/composition/platform{{alias.ext}}';
22
32
  import { dataSubjectCopies } from '{{alias.platform}}/privacy/data-subject-copies{{alias.ext}}';
33
+ {{#if events.outbox}}
34
+ import { personalEvents } from '{{alias.platform}}/privacy/personal-events{{alias.ext}}';
35
+ {{/if}}
23
36
 
24
37
  export interface ErasureRequest {
25
- /** The assembled platform: the {{#if eventSourcing.on}}keys, the {{/if}}scope and the copies come from it. */
38
+ /** The assembled platform: the {{#if eventSourcing.on}}keys, the {{/if}}scope{{#if audit.on}}, the trail{{/if}} and the copies come from it. */
26
39
  readonly platform: Platform;
27
- /** Who asks for the erasure, as typed: the actor of the work{{#if eventSourcing.on}}{{#if audit.on}}, named by the entry the audited keys write{{/if}}{{/if}}. */
40
+ /** Who asks for the erasure, as typed: the actor of the work{{#if audit.on}}, named by the entry each copy writes{{/if}}. */
28
41
  readonly operator: string;
29
42
  /** The correlation of the work: one per erasure. */
30
43
  readonly correlationId: string;
44
+ /** This subject was erased before: reaching nothing is expected, and the erasure is announced again, which completes one that failed before its announcement. */
45
+ readonly rerun?: boolean;
31
46
  {{#if tenancy.any}}
32
47
  /** The tenants the deployment serves: the copies are erased in each one, in an execution context of that tenant, since a write of tenant data is never widened. */
33
48
  readonly tenants: readonly string[];
@@ -37,8 +52,9 @@ export interface ErasureRequest {
37
52
  {{#if eventSourcing.on}}
38
53
  /**
39
54
  * The right to erasure of one data subject, in an execution context of its own (origin `cli`): the
40
- * key first{{#if audit.on}}, with its audit entry{{/if}}, then every copy a context declared in `data-subject-copies.ts`{{#if tenancy.any}}, in
41
- * every tenant{{/if}}. Running it again completes an erasure whose entry or copy failed; a subject that is
55
+ * key first, then every copy a context declared in `data-subject-copies.ts`{{#if tenancy.any}}, in every tenant{{/if}}{{#if audit.on}}; the key and every
56
+ * copy write their entry to the audit trail, the operator, the subject and the number of rows, never
57
+ * the values{{/if}}. Running it again completes an erasure whose entry or copy failed; a subject that is
42
58
  * not an opaque identifier is refused before any key is touched.
43
59
  */
44
60
  {{/if}}
@@ -47,13 +63,19 @@ export interface ErasureRequest {
47
63
  * The right to erasure of one data subject, in an execution context of its own (origin `cli`):
48
64
  * every copy a context declared in `data-subject-copies.ts`{{#if tenancy.any}}, in every tenant{{/if}}. The aggregates
49
65
  * of this project are rows, so no key is destroyed: a copy is erased or pseudonymised where it
50
- * lies. Running it again completes an erasure whose copy failed; a subject that is not an opaque
51
- * identifier is refused before anything is touched.
66
+ * lies{{#if audit.on}}, and writes its entry to the audit trail, the operator, the subject and the number of
67
+ * rows, never the values{{/if}}. Running it again completes an erasure whose copy failed; a subject that is not an
68
+ * opaque identifier is refused before anything is touched.
52
69
  */
53
70
  {{/unless}}
54
71
  export function runErasure(subject: string, request: ErasureRequest): Promise<DataSubjectErasureReport> {
55
72
  const { platform, operator, correlationId } = request;
56
73
  const context = executionContext({ correlationId, actor: operatorActor(operator), origin: 'cli' });
74
+ {{#if audit.on}}
75
+ const { trail, onLostRecord } = platform.audit;
76
+ const source = { scope: platform.scope, clock: platform.clock, ids: platform.ids, onLostRecord };
77
+ const audited = (copy: PersonalDataCopy): PersonalDataCopy => auditedPersonalDataErasure(copy, trail, source);
78
+ {{/if}}
57
79
 
58
80
  return platform.scope.run(context, async () => {
59
81
  const report = await eraseDataSubject(subject, {
@@ -61,7 +83,19 @@ export function runErasure(subject: string, request: ErasureRequest): Promise<Da
61
83
  // TODO(platform): once a projection is fed by the event store, give `rebuilds` the rebuild of each one: a projection keeps a clear copy of the person until it is rebuilt.
62
84
  keys: platform.eventSourcing.subjectKeys,
63
85
  {{/if}}
86
+ {{#if audit.on}}
87
+ copies: dataSubjectCopies.map((copy) => audited(copy(platform))),
88
+ {{#if housekeeping.any}}
89
+ shared: platformCopies(platform).map(audited),
90
+ {{/if}}
91
+ {{/if}}
92
+ {{#unless audit.on}}
64
93
  copies: dataSubjectCopies.map((copy) => copy(platform)),
94
+ {{#if housekeeping.any}}
95
+ shared: platformCopies(platform),
96
+ {{/if}}
97
+ {{/unless}}
98
+ isKnown: () => Promise.resolve(request.rerun === true),
65
99
  {{#if tenancy.any}}
66
100
  inEveryTenant: inEachTenant(platform.scope, request.tenants),
67
101
  {{/if}}
@@ -73,12 +107,28 @@ export function runErasure(subject: string, request: ErasureRequest): Promise<Da
73
107
  });
74
108
  }
75
109
 
110
+ {{#if housekeeping.any}}
111
+ /** What the platform keeps of a person in tables no tenant confines{{#if events.outbox}}: the records of the outbox, by what `personal-events.ts` lists{{/if}}{{#if queue.stored}}{{#if events.outbox}},{{/if}}{{#unless events.outbox}}:{{/unless}} the jobs enqueued with the person as `subject`{{/if}}. */
112
+ function platformCopies(platform: Platform): readonly PersonalDataCopy[] {
113
+ {{#if events.outbox}}
114
+ const outbox = personalEventsInOutbox(platform.persistence.outbox, personalEvents);
115
+ {{/if}}
116
+ {{#if queue.stored}}
117
+ const jobs = jobsOfSubject(platform.jobStore);
118
+ {{/if}}
119
+
120
+ return [{{#if events.outbox}}outbox{{/if}}{{#if events.outbox}}{{#if queue.stored}}, {{/if}}{{/if}}{{#if queue.stored}}jobs{{/if}}];
121
+ }
122
+
123
+ {{/if}}
76
124
  /**
77
125
  * Once every copy is erased, `privacy.data-subject.erased` goes to the outbox{{#if tenancy.any}}, once per tenant{{/if}}: a context that
78
126
  * runs as a service of its own, or a system that copied the person from an integration event, erases
79
- * its copy on its side (GDPR article 19). It names the person by id and nobody else: who asked is in
80
- * the audit trail, under the correlation id the event carries. The relay of the application delivers it; an erasure that
81
- * failed publishes nothing, and running it again does.
127
+ * its copy on its side (GDPR article 19). It names the person by id and nobody else{{#if audit.on}}: who asked is in
128
+ * the audit trail, under the correlation id the event carries{{/if}}. The relay of the application delivers it; an erasure that
129
+ * failed publishes nothing, and running it again does. Neither does an erasure that reached nothing:
130
+ * `eraseDataSubject` refuses it (`UnknownDataSubjectError`), a mistyped id or a tenant left out of the
131
+ * list must not tell the others that a person is gone; `rerun` says it is expected.
82
132
  */
83
133
  async function announceErasure(subject: string, { platform{{#if tenancy.any}}, tenants{{/if}} }: ErasureRequest): Promise<void> {
84
134
  const erasedAt = platform.clock.now();
@@ -1,9 +1,18 @@
1
+ {{#if audit.on}}
2
+ import { AuditTrail, auditedPersonalDataErasure, logLostRecords } from '@ddd-ts-kit/audit';
3
+ {{/if}}
1
4
  import type { Application as Container } from '@ddd-ts-kit/di';
5
+ {{#if events.outbox}}
6
+ import { OutboxStore, personalEventsInOutbox } from '@ddd-ts-kit/events';
7
+ {{/if}}
2
8
  import {
3
9
  Clock,
4
10
  ExecutionScope,
5
11
  IdGenerator,
6
12
  IntegrationEventPublisher,
13
+ {{#if audit.on}}
14
+ Logger,
15
+ {{/if}}
7
16
  {{#if eventSourcing.on}}
8
17
  SubjectKeyStore,
9
18
  {{/if}}
@@ -18,16 +27,32 @@ import {
18
27
  {{/if}}
19
28
  type Actor,
20
29
  type DataSubjectErasureReport,
30
+ {{#if housekeeping.any}}
31
+ type PersonalDataCopy,
32
+ {{/if}}
33
+ {{#unless housekeeping.any}}
34
+ {{#if audit.on}}
35
+ type PersonalDataCopy,
36
+ {{/if}}
37
+ {{/unless}}
21
38
  } from '@ddd-ts-kit/kernel';
39
+ {{#if queue.stored}}
40
+ import { JobStore, jobsOfSubject } from '@ddd-ts-kit/scheduling';
41
+ {{/if}}
22
42
  import { dataSubjectCopies } from '{{alias.platform}}/privacy/data-subject-copies{{alias.ext}}';
43
+ {{#if events.outbox}}
44
+ import { personalEvents } from '{{alias.platform}}/privacy/personal-events{{alias.ext}}';
45
+ {{/if}}
23
46
 
24
47
  export interface ErasureRequest {
25
48
  /** The composed application: the {{#if eventSourcing.on}}keys and the {{/if}}copies are resolved from it. */
26
49
  readonly container: Container;
27
- /** Who asks for the erasure, as typed: the actor of the work{{#if eventSourcing.on}}{{#if audit.on}}, named by the entry the audited keys write{{/if}}{{/if}}. */
50
+ /** Who asks for the erasure, as typed: the actor of the work{{#if audit.on}}, named by the entry each copy writes{{/if}}. */
28
51
  readonly operator: string;
29
52
  /** The correlation of the work: one per erasure. */
30
53
  readonly correlationId: string;
54
+ /** This subject was erased before: reaching nothing is expected, and the erasure is announced again, which completes one that failed before its announcement. */
55
+ readonly rerun?: boolean;
31
56
  {{#if tenancy.any}}
32
57
  /** The tenants the deployment serves: the copies are erased in each one, in an execution context of that tenant, since a write of tenant data is never widened. */
33
58
  readonly tenants: readonly string[];
@@ -37,8 +62,9 @@ export interface ErasureRequest {
37
62
  {{#if eventSourcing.on}}
38
63
  /**
39
64
  * The right to erasure of one data subject, in an execution context of its own (origin `cli`): the
40
- * key first{{#if audit.on}}, with its audit entry{{/if}}, then every copy a context declared in `data-subject-copies.ts`{{#if tenancy.any}}, in
41
- * every tenant{{/if}}. Running it again completes an erasure whose entry or copy failed; a subject that is
65
+ * key first, then every copy a context declared in `data-subject-copies.ts`{{#if tenancy.any}}, in every tenant{{/if}}{{#if audit.on}}; the key and every
66
+ * copy write their entry to the audit trail, the operator, the subject and the number of rows, never
67
+ * the values{{/if}}. Running it again completes an erasure whose entry or copy failed; a subject that is
42
68
  * not an opaque identifier is refused before any key is touched.
43
69
  */
44
70
  {{/if}}
@@ -47,14 +73,21 @@ export interface ErasureRequest {
47
73
  * The right to erasure of one data subject, in an execution context of its own (origin `cli`):
48
74
  * every copy a context declared in `data-subject-copies.ts`{{#if tenancy.any}}, in every tenant{{/if}}. The aggregates
49
75
  * of this project are rows, so no key is destroyed: a copy is erased or pseudonymised where it
50
- * lies. Running it again completes an erasure whose copy failed; a subject that is not an opaque
51
- * identifier is refused before anything is touched.
76
+ * lies{{#if audit.on}}, and writes its entry to the audit trail, the operator, the subject and the number of
77
+ * rows, never the values{{/if}}. Running it again completes an erasure whose copy failed; a subject that is not an
78
+ * opaque identifier is refused before anything is touched.
52
79
  */
53
80
  {{/unless}}
54
81
  export function runErasure(subject: string, request: ErasureRequest): Promise<DataSubjectErasureReport> {
55
82
  const { container, operator, correlationId } = request;
56
83
  const scope = container.resolve(ExecutionScope);
57
84
  const context = executionContext({ correlationId, actor: operatorActor(operator), origin: 'cli' });
85
+ {{#if audit.on}}
86
+ const trail = container.resolve(AuditTrail);
87
+ const onLostRecord = logLostRecords(container.resolve(Logger));
88
+ const source = { scope, clock: container.resolve(Clock), ids: container.resolve(IdGenerator), onLostRecord };
89
+ const audited = (copy: PersonalDataCopy): PersonalDataCopy => auditedPersonalDataErasure(copy, trail, source);
90
+ {{/if}}
58
91
 
59
92
  return scope.run(context, async () => {
60
93
  const report = await eraseDataSubject(subject, {
@@ -62,7 +95,19 @@ export function runErasure(subject: string, request: ErasureRequest): Promise<Da
62
95
  // TODO(platform): once a projection is fed by the event store, give `rebuilds` the rebuild of each one: a projection keeps a clear copy of the person until it is rebuilt.
63
96
  keys: container.resolve(SubjectKeyStore),
64
97
  {{/if}}
98
+ {{#if audit.on}}
99
+ copies: dataSubjectCopies.map((copy) => audited(container.resolve(copy))),
100
+ {{#if housekeeping.any}}
101
+ shared: platformCopies(container).map(audited),
102
+ {{/if}}
103
+ {{/if}}
104
+ {{#unless audit.on}}
65
105
  copies: dataSubjectCopies.map((copy) => container.resolve(copy)),
106
+ {{#if housekeeping.any}}
107
+ shared: platformCopies(container),
108
+ {{/if}}
109
+ {{/unless}}
110
+ isKnown: () => Promise.resolve(request.rerun === true),
66
111
  {{#if tenancy.any}}
67
112
  inEveryTenant: inEachTenant(scope, request.tenants),
68
113
  {{/if}}
@@ -74,12 +119,28 @@ export function runErasure(subject: string, request: ErasureRequest): Promise<Da
74
119
  });
75
120
  }
76
121
 
122
+ {{#if housekeeping.any}}
123
+ /** What the platform keeps of a person in tables no tenant confines{{#if events.outbox}}: the records of the outbox, by what `personal-events.ts` lists{{/if}}{{#if queue.stored}}{{#if events.outbox}},{{/if}}{{#unless events.outbox}}:{{/unless}} the jobs enqueued with the person as `subject`{{/if}}. */
124
+ function platformCopies(container: Container): readonly PersonalDataCopy[] {
125
+ {{#if events.outbox}}
126
+ const outbox = personalEventsInOutbox(container.resolve(OutboxStore), personalEvents);
127
+ {{/if}}
128
+ {{#if queue.stored}}
129
+ const jobs = jobsOfSubject(container.resolve(JobStore));
130
+ {{/if}}
131
+
132
+ return [{{#if events.outbox}}outbox{{/if}}{{#if events.outbox}}{{#if queue.stored}}, {{/if}}{{/if}}{{#if queue.stored}}jobs{{/if}}];
133
+ }
134
+
135
+ {{/if}}
77
136
  /**
78
137
  * Once every copy is erased, `privacy.data-subject.erased` goes to the outbox{{#if tenancy.any}}, once per tenant{{/if}}: a context that
79
138
  * runs as a service of its own, or a system that copied the person from an integration event, erases
80
- * its copy on its side (GDPR article 19). It names the person by id and nobody else: who asked is in
81
- * the audit trail, under the correlation id the event carries. The relay of the application delivers it; an erasure that
82
- * failed publishes nothing, and running it again does.
139
+ * its copy on its side (GDPR article 19). It names the person by id and nobody else{{#if audit.on}}: who asked is in
140
+ * the audit trail, under the correlation id the event carries{{/if}}. The relay of the application delivers it; an erasure that
141
+ * failed publishes nothing, and running it again does. Neither does an erasure that reached nothing:
142
+ * `eraseDataSubject` refuses it (`UnknownDataSubjectError`), a mistyped id or a tenant left out of the
143
+ * list must not tell the others that a person is gone; `rerun` says it is expected.
83
144
  */
84
145
  async function announceErasure(subject: string, request: ErasureRequest): Promise<void> {
85
146
  const { container } = request;
@@ -39,8 +39,8 @@ export function runExport(subject: string, request: ExportRequest): Promise<Pers
39
39
  const { platform, operator, correlationId } = request;
40
40
  const context = executionContext({ correlationId, actor: operatorActor(operator), origin: 'cli' });
41
41
  {{#if audit.on}}
42
- const { trail } = platform.audit;
43
- const source = { scope: platform.scope, clock: platform.clock, ids: platform.ids };
42
+ const { trail, onLostRecord } = platform.audit;
43
+ const source = { scope: platform.scope, clock: platform.clock, ids: platform.ids, onLostRecord };
44
44
  const audited = (port: PersonalDataExport): PersonalDataExport => auditedPersonalDataExport(port, trail, source);
45
45
  const exports = dataSubjectExports.map((exported) => audited(exported(platform)));
46
46
  {{/if}}