AccountMappingHelper.test.mjs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import { expect } from 'vitest'
  2. import mongodb from 'mongodb-legacy'
  3. import path from 'node:path'
  4. const { ObjectId } = mongodb
  5. const MODULE_PATH = path.join(
  6. import.meta.dirname,
  7. '../../../../app/src/Features/Analytics/AccountMappingHelper'
  8. )
  9. describe('AccountMappingHelper', function () {
  10. beforeEach(async function (ctx) {
  11. ctx.AccountMappingHelper = (await import(MODULE_PATH)).default
  12. })
  13. describe('extractAccountMappingsFromSubscription', function () {
  14. describe('when the v1 id is the same in the updated subscription and the subscription', function () {
  15. describe('when the salesforce id is the same in the updated subscription and the subscription', function () {
  16. beforeEach(function (ctx) {
  17. ctx.subscription = {
  18. id: new ObjectId('abc123abc123abc123abc123'),
  19. salesforce_id: 'def456def456def456',
  20. }
  21. ctx.updatedSubscription = { salesforce_id: 'def456def456def456' }
  22. ctx.result =
  23. ctx.AccountMappingHelper.extractAccountMappingsFromSubscription(
  24. ctx.subscription,
  25. ctx.updatedSubscription
  26. )
  27. })
  28. it('returns an empty array', function (ctx) {
  29. expect(ctx.result).to.be.an('array')
  30. expect(ctx.result).to.have.length(0)
  31. })
  32. })
  33. describe('when the salesforce id has changed between the subscription and the updated subscription', function () {
  34. beforeEach(function (ctx) {
  35. ctx.subscription = {
  36. id: new ObjectId('abc123abc123abc123abc123'),
  37. salesforce_id: 'def456def456def456',
  38. }
  39. ctx.updatedSubscription = { salesforce_id: 'ghi789ghi789ghi789' }
  40. ctx.result =
  41. ctx.AccountMappingHelper.extractAccountMappingsFromSubscription(
  42. ctx.subscription,
  43. ctx.updatedSubscription
  44. )
  45. })
  46. it('returns an array with a single item', function (ctx) {
  47. expect(ctx.result).to.be.an('array')
  48. expect(ctx.result).to.have.length(1)
  49. })
  50. it('uses "account" as sourceEntity', function (ctx) {
  51. expect(ctx.result[0]).to.haveOwnProperty('sourceEntity', 'account')
  52. })
  53. it('uses the salesforceId from the updated subscription as sourceEntityId', function (ctx) {
  54. expect(ctx.result[0]).to.haveOwnProperty(
  55. 'sourceEntityId',
  56. ctx.updatedSubscription.salesforce_id
  57. )
  58. })
  59. it('uses "subscription" as targetEntity', function (ctx) {
  60. expect(ctx.result[0]).to.haveOwnProperty(
  61. 'targetEntity',
  62. 'subscription'
  63. )
  64. })
  65. it('uses the subscriptionId as targetEntityId', function (ctx) {
  66. expect(ctx.result[0]).to.haveOwnProperty(
  67. 'targetEntityId',
  68. ctx.subscription.id
  69. )
  70. })
  71. })
  72. describe('when the update subscription has a salesforce id and the subscription has no salesforce_id', function () {
  73. beforeEach(function (ctx) {
  74. ctx.subscription = { id: new ObjectId('abc123abc123abc123abc123') }
  75. ctx.updatedSubscription = { salesforce_id: 'def456def456def456' }
  76. ctx.result =
  77. ctx.AccountMappingHelper.extractAccountMappingsFromSubscription(
  78. ctx.subscription,
  79. ctx.updatedSubscription
  80. )
  81. })
  82. it('returns an array with a single item', function (ctx) {
  83. expect(ctx.result).to.be.an('array')
  84. expect(ctx.result).to.have.length(1)
  85. })
  86. it('uses "account" as sourceEntity', function (ctx) {
  87. expect(ctx.result[0]).to.haveOwnProperty('sourceEntity', 'account')
  88. })
  89. it('uses the salesforceId from the updated subscription as sourceEntityId', function (ctx) {
  90. expect(ctx.result[0]).to.haveOwnProperty(
  91. 'sourceEntityId',
  92. ctx.updatedSubscription.salesforce_id
  93. )
  94. })
  95. it('uses "subscription" as targetEntity', function (ctx) {
  96. expect(ctx.result[0]).to.haveOwnProperty(
  97. 'targetEntity',
  98. 'subscription'
  99. )
  100. })
  101. it('uses the subscriptionId as targetEntityId', function (ctx) {
  102. expect(ctx.result[0]).to.haveOwnProperty(
  103. 'targetEntityId',
  104. ctx.subscription.id
  105. )
  106. })
  107. })
  108. })
  109. describe('when the v1 id has changed between the subscription and the updated subscription', function () {
  110. describe('when the salesforce id has not changed between the subscription and the updated subscription', function () {
  111. beforeEach(function (ctx) {
  112. ctx.subscription = {
  113. id: new ObjectId('abc123abc123abc123abc123'),
  114. v1_id: '1',
  115. salesforce_id: '',
  116. }
  117. ctx.updatedSubscription = { v1_id: '2', salesforce_id: '' }
  118. ctx.result =
  119. ctx.AccountMappingHelper.extractAccountMappingsFromSubscription(
  120. ctx.subscription,
  121. ctx.updatedSubscription
  122. )
  123. })
  124. it('returns an array with a single item', function (ctx) {
  125. expect(ctx.result).to.be.an('array')
  126. expect(ctx.result).to.have.length(1)
  127. })
  128. it('uses "university" as the sourceEntity', function (ctx) {
  129. expect(ctx.result[0]).to.haveOwnProperty('sourceEntity', 'university')
  130. })
  131. it('uses the v1_id from the updated subscription as the sourceEntityId', function (ctx) {
  132. expect(ctx.result[0]).to.haveOwnProperty(
  133. 'sourceEntityId',
  134. ctx.updatedSubscription.v1_id
  135. )
  136. })
  137. it('uses "subscription" as the targetEntity', function (ctx) {
  138. expect(ctx.result[0]).to.haveOwnProperty(
  139. 'targetEntity',
  140. 'subscription'
  141. )
  142. })
  143. it('uses the subscription id as the targetEntityId', function (ctx) {
  144. expect(ctx.result[0]).to.haveOwnProperty(
  145. 'targetEntityId',
  146. ctx.subscription.id
  147. )
  148. })
  149. })
  150. describe('when the salesforce id has changed between the subscription and the updated subscription', function () {
  151. beforeEach(function (ctx) {
  152. ctx.subscription = {
  153. id: new ObjectId('abc123abc123abc123abc123'),
  154. v1_id: '',
  155. salesforce_id: 'def456def456def456',
  156. }
  157. ctx.updatedSubscription = {
  158. v1_id: '2',
  159. salesforce_id: '',
  160. }
  161. ctx.result =
  162. ctx.AccountMappingHelper.extractAccountMappingsFromSubscription(
  163. ctx.subscription,
  164. ctx.updatedSubscription
  165. )
  166. })
  167. it('returns an array with two items', function (ctx) {
  168. expect(ctx.result).to.be.an('array')
  169. expect(ctx.result).to.have.length(2)
  170. })
  171. it('uses the salesforce_id from the updated subscription as the sourceEntityId for the first item', function (ctx) {
  172. expect(ctx.result[0]).to.haveOwnProperty(
  173. 'sourceEntityId',
  174. ctx.updatedSubscription.salesforce_id
  175. )
  176. })
  177. it('uses the subscription id as the targetEntityId for the first item', function (ctx) {
  178. expect(ctx.result[0]).to.haveOwnProperty(
  179. 'targetEntityId',
  180. ctx.subscription.id
  181. )
  182. })
  183. it('uses the v1_id from the updated subscription as the sourceEntityId for the second item', function (ctx) {
  184. expect(ctx.result[1]).to.haveOwnProperty(
  185. 'sourceEntityId',
  186. ctx.updatedSubscription.v1_id
  187. )
  188. })
  189. it('uses the subscription id as the targetEntityId for the second item', function (ctx) {
  190. expect(ctx.result[1]).to.haveOwnProperty(
  191. 'targetEntityId',
  192. ctx.subscription.id
  193. )
  194. })
  195. })
  196. })
  197. })
  198. describe('when the recurlySubscription_id has changed between the subscription and the updated subscription', function () {
  199. beforeEach(function (ctx) {
  200. ctx.subscription = {
  201. id: new ObjectId('abc123abc123abc123abc123'),
  202. recurlySubscription_id: '',
  203. }
  204. ctx.updatedSubscription = {
  205. recurlySubscription_id: '1234a5678b90123cd4567e8f901a2b34',
  206. }
  207. ctx.result =
  208. ctx.AccountMappingHelper.extractAccountMappingsFromSubscription(
  209. ctx.subscription,
  210. ctx.updatedSubscription
  211. )
  212. })
  213. it('returns an array with one item', function (ctx) {
  214. expect(ctx.result).to.be.an('array')
  215. expect(ctx.result).to.have.length(1)
  216. })
  217. it('uses "recurly" as the source', function (ctx) {
  218. expect(ctx.result[0]).to.haveOwnProperty('source', 'recurly')
  219. })
  220. it('uses "subscription" as the sourceEntity', function (ctx) {
  221. expect(ctx.result[0]).to.haveOwnProperty('sourceEntity', 'subscription')
  222. })
  223. it('uses the recurlySubscription_id as the sourceEntityId', function (ctx) {
  224. expect(ctx.result[0]).to.haveOwnProperty(
  225. 'sourceEntityId',
  226. ctx.updatedSubscription.recurlySubscription_id
  227. )
  228. })
  229. it('uses "v2" as the target', function (ctx) {
  230. expect(ctx.result[0]).to.haveOwnProperty('target', 'v2')
  231. })
  232. it('uses "subscription" as the targetEntity', function (ctx) {
  233. expect(ctx.result[0]).to.haveOwnProperty('targetEntity', 'subscription')
  234. })
  235. it('uses the subscription id as the targetEntityId', function (ctx) {
  236. expect(ctx.result[0]).to.haveOwnProperty(
  237. 'targetEntityId',
  238. ctx.subscription.id
  239. )
  240. })
  241. })
  242. })