Ver código fonte

PROD-1221: Store opts apart from registration

Cancelling registration shouldn't clear opts.
Blake Schneider 5 anos atrás
pai
commit
e25697a603

+ 5 - 0
__tests__/unit/processes/event.spec.js

@@ -26,6 +26,7 @@ describe('Event', function() {
     sandbox = sinon.createSandbox();
 
     let registration = {};
+    let opted = {};
 
     fetchEventStub = sandbox.stub().resolves(sampleEvent1);
     rewired = rewire('../../../processes/event.js');
@@ -36,6 +37,10 @@ describe('Event', function() {
         updateRegistration: async newRegistration => {
           registration = newRegistration;
         },
+        fetchOpted: venId => Promise.resolve(opted[venId] || []),
+        updateOpted: async (venId, newOpted) => {
+          opted[venId] = newOpted || [];
+        },
       },
     });
   });

+ 0 - 17
__tests__/unit/processes/registration.spec.js

@@ -215,23 +215,6 @@ describe('VEN registration', function() {
       expect(cancelResponse.venId).to.eql(venId);
     });
 
-    it('fails if no registrationId is specified', async () => {
-      const cancelRequestId = v4().replace(/-/g, '');
-      const cancelRequest = {
-        requestId: cancelRequestId,
-        venId: venId,
-      };
-
-      let error;
-      try {
-        await cancelParty(cancelRequest, commonName, venId);
-      } catch (e) {
-        error = e;
-      }
-      expect(error).to.be.an('error');
-      expect(error.message).to.eql('No registrationID in request');
-    });
-
     it('fails if venID does not match certificate', async () => {
       const otherVenId = v4().replace(/-/g, '');
       const cancelRequestId = v4().replace(/-/g, '');

+ 25 - 0
modules/nantum.js

@@ -38,6 +38,14 @@ async function fetchRegistration(venId) {
   if (venRecord) return venRecord.data.registration;
 }
 
+async function fetchOpted(venId) {
+  const venRecord = await Ven.findOne({
+    where: { ven_id: venId },
+  });
+  if (venRecord) return venRecord.data.opted || [];
+  return [];
+}
+
 async function updateRegistration(registration) {
   if (registration.ven_id == null) {
     throw new Error('Registration is missing ven_id');
@@ -63,8 +71,25 @@ async function updateRegistration(registration) {
   await venRecord.save();
 }
 
+async function updateOpted(venId, opted) {
+  let venRecord = await Ven.findOne({
+    where: { ven_id: venId },
+  });
+
+  if (venRecord) {
+    const newData = venRecord.data || {};
+    newData.opted = opted;
+    venRecord.set('data', newData); // setting `data` directly on object doesn't trigger change detection
+  } else {
+    throw new Error(`Ven ${venId} must be registered`);
+  }
+  await venRecord.save();
+}
+
 module.exports = {
   fetchEvent,
+  fetchOpted,
   fetchRegistration,
   updateRegistration,
+  updateOpted,
 };

+ 6 - 16
processes/event.js

@@ -182,32 +182,23 @@ async function updateOptType(
   const requestVenId = oadrCreatedEvent.venId;
   validateVenId(requestVenId, clientCertificateFingerprint, true);
 
-  if (requestVenId !== clientCertificateFingerprint) {
-    // as per certification item #512, venId MUST be case-sensitive
-    const error = new Error('VenID does not match certificate');
-    error.responseCode = 452;
-    throw error;
-  }
-
-  const nantumRegistration = await nantum.fetchRegistration(requestVenId);
-
-  const opted = nantumRegistration.opted || [];
+  let opted = await nantum.fetchOpted(requestVenId);
 
   //TODO: more validation: VEN may opt into an event that doesn't exist. VEN may opt into an old version of an event
   // (modificationNumber doesn't match). May opt into a completed event. Indicate error(s) to client.
 
   for (const eventResponse of oadrCreatedEvent.eventResponses) {
     // remove existing opts for this eventId
-    nantumRegistration.opted = [
+    opted = [
       ...opted.filter(optedItem => optedItem.eventId !== eventResponse.eventId),
     ];
-    nantumRegistration.opted.push({
+    opted.push({
       eventId: eventResponse.eventId,
       modificationNumber: eventResponse.modificationNumber,
       optType: eventResponse.optType,
     });
-    await nantum.updateRegistration(nantumRegistration);
   }
+  await nantum.updateOpted(requestVenId, opted);
 
   return {
     responseCode: '200',
@@ -217,8 +208,7 @@ async function updateOptType(
 }
 
 async function filterOutAcknowledgedEvents(venId, events) {
-  const nantumRegistration = await nantum.fetchRegistration(venId);
-  const opted = nantumRegistration.opted || [];
+  const opted = (await nantum.fetchOpted(venId)) || [];
   return events.filter(
     event =>
       opted.filter(
@@ -264,7 +254,7 @@ async function pollForEvents(
   return undefined;
 }
 
-function validateVenId(requestVenId, clientCertificateFingerprint, required)  {
+function validateVenId(requestVenId, clientCertificateFingerprint, required) {
   if (requestVenId === clientCertificateFingerprint) {
     return;
   }

+ 13 - 4
processes/registration.js

@@ -49,8 +49,17 @@ async function registerParty(
   );
 }
 
-async function query(oadrQueryRegistration, clientCertificateCn, clientCertificateFingerprint) {
-  logger.info('query', oadrQueryRegistration, clientCertificateCn, clientCertificateFingerprint);
+async function query(
+  oadrQueryRegistration,
+  clientCertificateCn,
+  clientCertificateFingerprint,
+) {
+  logger.info(
+    'query',
+    oadrQueryRegistration,
+    clientCertificateCn,
+    clientCertificateFingerprint,
+  );
 
   const requestVenId = clientCertificateFingerprint;
 
@@ -139,7 +148,7 @@ function nantumRegistrationToOadrRegistrationCreated(
   };
 }
 
-function validateVenId(requestVenId, clientCertificateFingerprint, required)  {
+function validateVenId(requestVenId, clientCertificateFingerprint, required) {
   if (requestVenId === clientCertificateFingerprint) {
     return;
   }
@@ -151,7 +160,7 @@ function validateVenId(requestVenId, clientCertificateFingerprint, required)  {
     error.responseCode = 452;
     throw error;
   }
-  const error = new Error('VenID is invalid');
+  const error = new Error('VenID does not match certificate');
   error.responseCode = 452;
   throw error;
 }