Guest User

Untitled

a guest
Jan 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. /*
  2. // returns updated object but also saves audit to db
  3. fooWithAuditSavedToDbAfterExec({ x: 8 }, 'restuta')
  4. */
  5.  
  6.  
  7.  
  8. /* goals:
  9. We want to abstract audit records collection from "foo" function. We don't want it to know
  10. anything about audit log, it should just be able to call "applyUpdatesWithAudit" anytime it wants and
  11. later we would like to collect all audit logs and save to db.
  12.  
  13. "updateWithAuditLog" is a function that updates an object and returns updated object and
  14. corresponding audit record.
  15.  
  16. Generic definition:
  17.  
  18. Design a pattern that allows "foo" function to be agnostic of the update fuction passed to it,
  19. it should know or deal with any audt logs collection and saving them to db.
  20.  
  21.  
  22. Specific definition:
  23. 1) Design a funciton that has signature similar to "updateWithAuditLog" but returns updates only
  24. (and not the audit log record)
  25. see: "applyUpdatesWithAudit" as an example of such a function
  26. 2) Create new function based on "foo" that is able to collect audit records and save them to db
  27. 3) We should save audit records to db all at once after all updates are done.
  28. 4) Solution should be generic enough so we can "attach" it to any function that expects
  29. "audit producing function".
  30.  
  31. */
  32.  
  33. const foo = applyUpdatesWithAudit => (obj, userName) => {
  34. let updatedObj = applyUpdatesWithAudit(obj, { y: 1 }, userName);
  35. updatedObj = applyUpdatesWithAudit(updatedObj, { z: 2 }, userName)
  36. updatedObj = applyUpdatesWithAudit(updatedObj, { x: 3 }, userName)
  37.  
  38. return {
  39. updatedObj,
  40. };
  41. };
  42.  
  43. const updateWithAuditLog = (orignalObj, updates, userName) => {
  44. const updatedObj = {
  45. ...orignalObj,
  46. ...updates,
  47. };
  48.  
  49. const auditRecord = `${userName} updated "${Object.keys(updates).join(',')}".`;
  50.  
  51. return {
  52. updatedObj,
  53. auditRecord,
  54. };
  55. };
  56.  
  57. const saveAuditRecordsToDb = auditRecords => console.log(`Saved ${auditRecords.length} to db`);
  58.  
  59. // implementation stub
  60. const applyUpdatesWithAudit = (...args) => updateWithAuditLog(...args).updatedObj;
  61.  
  62. updateWithAuditLog({ x: 8 }, { a: 9, x: 9 }, 'anton'); //?
  63.  
  64.  
  65.  
  66.  
  67. foo(applyUpdatesWithAudit)({ x: 8 }, 'restuta'); //?
  68.  
  69.  
  70. const createAuditLogCollector = auditLogProducingFunc => func => {/* your implementation */ }
  71. const saveAuditLog = saveAuditToDbFunc => () => { /* your implementation */ }
  72.  
  73. const saveAuditLogAfterExecution = saveAuditLog(saveAuditRecordsToDb)
  74.  
  75. const withAuditLogCollector = createAuditLogCollector(updateWithAuditLog)
  76. const fooWithAudit = withAuditLogCollector(foo)
  77. const fooWithAuditSavedToDbAfterExec = saveAuditLogAfterExecution(fooWithAudit)
  78.  
  79.  
  80. const R = require('ramda')
  81. const fooWithAuditSavedToDbAfterExec2 = R.pipe(
  82. withAuditLogCollector,
  83. saveAuditLogAfterExecution
  84. )
Add Comment
Please, Sign In to add comment