Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@doodle/lib-paywall / src / methods / verifyDuplicate.js
Size: Mime:
import { store } from '../state/store';
import { setData } from '../state/actions';

import { processType, getUserError, paywallStatus, getCalendarError } from '../constants/paywall';

import { getCalendarRequest, getUserDataRequest } from './requests';
import getStatusFromUserData from '../utils/getStatusFromUserData';

export default async ({ token, id, trackingEventPrefix }) => {
  const { apiHost } = store.getState();
  return new Promise(resolve => {
    getUserDataRequest({ token, apiHost })
      .then(async res => {
        const data = await res.text();

        const { status } = getStatusFromUserData({ data, token, trackingEventPrefix, apiHost });
        if (!status.shouldOpenModal) {
          getCalendarRequest({ token, id, apiHost, resolve })
            .then(async calRes => {
              const calendarResponse = await calRes.text();
              const parsedCalendarResponse = JSON.parse(calendarResponse);
              const { slots } = parsedCalendarResponse;

              const filteredSlots = slots
                .filter(slot => +new Date(slot.start) > +new Date())
                .map(slot => ({ ...slot, attendee: null }));
              const isSlotInThePast = slots.length !== filteredSlots.length;

              if (isSlotInThePast) {
                new Promise((duplicateResolveFunc, duplicateRejectFunc) => {
                  store.dispatch(
                    setData({
                      duplicateResolveFunc,
                      duplicateRejectFunc,
                      isWarningModalOpen: true,
                    })
                  );
                })
                  .then(() => {
                    resolve({
                      status: paywallStatus.PASS,
                    });
                  })
                  .catch(() => {
                    resolve({ status: paywallStatus.REJECT });
                  });
              } else {
                resolve({
                  status: paywallStatus.PASS,
                });
              }
            })
            .catch(e => {
              resolve({ ...getCalendarError, message: e.message });
            });
        } else {
          store.dispatch(
            setData({
              trackingEventPostfix: status.trackingEventPostfix,
              modalType: status.modalType,
              isPaywallModalOpen: true,
              resolve,
              process: processType.DUPLICATE,
            })
          );
        }
      })
      .catch(e => {
        resolve({ ...getUserError, message: e.message });
      });
  });
};