Last active
April 23, 2020 21:10
-
-
Save digitalbase/5a882626574c1c7881200dab3e17602a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// /src/models/SourceAttribution | |
const ATTRIBUTION_TABLE = process.env.ATTRIBUTION_TABLE; | |
const extractor = require('../utils/event_extraction.util'); | |
class SourceAttributionModel { | |
get _baseParams() { | |
return { | |
TableName: ATTRIBUTION_TABLE | |
}; | |
} | |
constructor(documentClient) { | |
this._documentClient = documentClient; | |
} | |
async store(eventData, extraction) { | |
const extractedData = extractor(eventData); | |
const {referrer, href, anonymousId, userId, timestamp, messageId } = extractedData; | |
const eventId = `${timestamp}-${messageId}`; // using as hash function | |
const params = this._createParamObject({ | |
Item: { | |
anonymousId, | |
eventId, | |
messageId, | |
timestamp, | |
userId, | |
url: href, | |
referrerUrl: referrer, | |
...extraction | |
} | |
}); | |
return await this._documentClient.put(params).promise(); | |
} | |
async getForAnonymousId(anonymousId) { | |
const params = this._createParamObject({ | |
KeyConditionExpression: "#anonymousId = :anonymousId", | |
ExpressionAttributeNames:{ | |
"#anonymousId": "anonymousId" | |
}, | |
ExpressionAttributeValues: { | |
":anonymousId": anonymousId | |
} | |
}); | |
const allItems = await this._documentClient.query(params).promise(); | |
return allItems.Items; | |
} | |
async getForAnonymousIds(anonymousIds) { | |
const promises = anonymousIds.map(async anonymousId => { | |
return this.getForAnonymousId(anonymousId); | |
}); | |
const results = await Promise.all(promises); | |
return results.filter((el) => { | |
console.log(el); | |
return el.length !== 0; | |
}); | |
} | |
_createParamObject(additionalArgs = {}) { | |
return Object.assign({}, this._baseParams, additionalArgs); | |
} | |
} | |
exports.SourceAttributionModel = SourceAttributionModel; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment