UNPKG

mobile-session-promise

Version:

The modules creates a promise that resolves an existing or creates a brand new session

79 lines (67 loc) 3.39 kB
"use strict"; /* * ATTENTION: IF YOU USE THIS MODULE PLEASE MAKE SURE TO POPULATE THE ENV VARIABLES: TABLE_NAME (Mandatory) - Dynamo table name see ./cloudformation/dynamo-mobile-session TTL_LENGTH_DAYS (default: 7) - Time to live after which the record gets erased by DynamoDB SESSION_EXPIRATION_TIMEOUT (default: 1800000) - the period of time the same session is reused within * The modules creates a promise that resolves existing or creates a new * session in the format as follows: {"schema":"iglu:com.nordstrom/client_session/jsonschema/1-2-4", "user_id":[[DOMAIN_USER_ID_SUPPLIED]], "session_id": [[NEW_OR_EXISTING_SESSION_ID]], "session_index": [[SESSION_SEQUENTIAL_NUMBER]], "storage_mechanism": "DYNAMO", "previous_session_id": [[EXISTING_SESSION_ID_OR_ZZZZZZ]], //nulls disallowed in dynamoDB "orig_event_id": [[EVENT_ID_SUPPLIED]], "first_event_id":[[FIRST_EVENT_OF_CURRENT_SESSION]]}}` * * Copyright(c) Nordstrom,Inc. All Rights reserved. This software is the confidential and * proprietary information of Nordstrom, Inc. ("Confidential Information"). You shall not disclose * such Confidential Information and shall use it only in accordance with the terms of the license * agreement you entered into with Nordstrom, Inc. */ const calc_derived_tstamp = require('./utils/calc_derived_tstamp'); const DEBUG = process.env.SNOWPLOW_DEBUG === 'true'; /** The function produces a Promise that eventually delivers the information about new or re-used session. Whether the session is created anew or retrieved and re-used from the Dynamo DB is based on three main decision points: * No previous events for the given domain_userid is found in DB then the new Session Id is created (USER_ID_NOT_FOUND) * The previous most recent event is found and it happened less than 30 minutes ago then the Session Id is reused from the event found (<30_MINUTES) * The previous most recent event is found and it happened earlier than 30 minutes ago then the new Session Id is generated and the previous_session_id is populated from the event found (>30_MINUTES) @orig_event_id event id @domain_userid user id or cookie id @body the payload body, containing all of the necessary timestamps */ module.exports = function(orig_event_id, domain_userid, collector_tstamp, device_created_tstamp, device_sent_tstamp) { const derived_tstamp = calc_derived_tstamp(collector_tstamp, device_created_tstamp, device_sent_tstamp); if(DEBUG) {console.log(orig_event_id); console.log(domain_userid); console.log(derived_tstamp); } var promise = new Promise(function(resolve, reject) { // do a thing, possibly async, then… const coElement = {"schema":"iglu:com.nordstrom/client_session/jsonschema/1-2-4", "user_id": domain_userid, "session_id": "ZZZZZZ", "session_index": 0, "storage_mechanism": "", "previous_session_id": "ZZZZZZ", "orig_event_id": orig_event_id, "first_event_id": orig_event_id}; // if(DEBUG)console.log(coElement); if (true) { resolve(coElement); } else { reject(coElement); } }); if(DEBUG) console.log("before return",promise); return promise; };