Skip to content

Commit

Permalink
Merge pull request #736 from Program-AR/using-lastInteraction
Browse files Browse the repository at this point in the history
Usando lastInteraction para mantener la sesion
  • Loading branch information
asanzo authored Apr 7, 2021
2 parents ba5d07b + 473872e commit 3c40df7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
12 changes: 9 additions & 3 deletions app/services/pilas-bloques-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,28 @@ export default Service.extend({
_newSession() {
const newSession = {
id: uuidv4(),
firstInteraction: new Date(),
lastInteraction: new Date(),
answers: []
}
this.storage.saveAnalyticsSession(newSession)
return newSession
},

_updatedSession(session) {
const updatedSession = { ...session, lastInteraction: new Date() }
this.storage.saveAnalyticsSession(updatedSession)
return updatedSession
},

getSession() {
const session = this.storage.getAnalyticsSession()
if (!session) return this._newSession()
return this._isOld(session) ? this._newSession() : session
return this._isOld(session) ? this._newSession() : this._updatedSession(session)
},

logout() {
this.storage.saveAnalyticsSession(null)
},

_isOld({ firstInteraction }) { return (new Date() - new Date(firstInteraction)) / 1000 / 60 > sessionExpire }, // Minutes
_isOld({ lastInteraction }) { return (new Date() - lastInteraction) / 1000 / 60 > sessionExpire }, // Minutes
})
16 changes: 10 additions & 6 deletions app/services/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ export default Ember.Service.extend({
USER_KEY: 'PB_USER',
ANALYTICS_KEY: 'PB_ANALYTICS_SESSION',

getUserId() {
getUserId() {
const user = this.getUser()
return user && user.id
},

getUser() { return this._get(this.USER_KEY) },
saveUser(user) { this._save(this.USER_KEY, user) },
getUser() { return this._get(this.USER_KEY) },
saveUser(user) { this._save(this.USER_KEY, user) },

getAnalyticsSession() {
const session = this._get(this.ANALYTICS_KEY)
return session && { ...session, lastInteraction: new Date(session.lastInteraction) }
},

getAnalyticsSession() { return this._get(this.ANALYTICS_KEY) },
saveAnalyticsSession(session) { this._save(this.ANALYTICS_KEY, session) },


_get(key) { return JSON.parse(localStorage.getItem(key) || null) },
_save(key, data = null) { localStorage.setItem(key, JSON.stringify(data)) },
_get(key) { return JSON.parse(localStorage.getItem(key) || null) },
_save(key, data = null) { localStorage.setItem(key, JSON.stringify(data)) },
})
34 changes: 19 additions & 15 deletions tests/unit/services/pilas-bloques-analytics-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,56 @@ module('Unit | Service | pilas-bloques-analytics', function (hooks) {

var pbAnalytics
var storage
var firstSessionId
hooks.beforeEach(function () {
pbAnalytics = this.owner.lookup('service:pilas-bloques-analytics')
storage = this.owner.lookup('service:storage')
firstSessionId = pbAnalytics.getSession().id // Creates session
})

test('Should create new sessionId', function (assert) {
assert.ok(pbAnalytics.getSession().id)
assert.ok(firstSessionId)
})

test('Should save session', function (assert) {
pbAnalytics.getSession()
const { id, firstInteraction, answers } = getSession()
const { id, lastInteraction, answers } = getStorageSession()
assert.ok(id)
assert.ok(firstInteraction)
assert.ok(lastInteraction)
assert.ok(answers)
})

test('Should add new answers', function (assert) {
pbAnalytics.newAnswer({ id: 'TEST' })
const { answers } = getSession()
const { answers } = getStorageSession()
assert.deepEqual(answers, [{ id: 'TEST' }])
})

test('Should keep the session for a while', function (assert) {
const firstSessionId = pbAnalytics.getSession().id
changeSessionFirstInteractionByMinutes(1)
substractMinutesFromStorageSession(1)
const currentSessionId = pbAnalytics.getSession().id
assert.equal(firstSessionId, currentSessionId)
})

test('Should change the session after a long time', function (assert) {
const firstSessionId = pbAnalytics.getSession().id
changeSessionFirstInteractionByMinutes(31)
substractMinutesFromStorageSession(31)
const currentSessionId = pbAnalytics.getSession().id
assert.notEqual(firstSessionId, currentSessionId)
})

function getSession() {
test('Should update lastInteraction after an interaction', function (assert) {
substractMinutesFromStorageSession(15)
const firstSessionInteraction = getStorageSession().lastInteraction

assert.ok(pbAnalytics.getSession().lastInteraction > firstSessionInteraction)
})

function getStorageSession() {
return storage.getAnalyticsSession()
}

function changeSessionFirstInteractionByMinutes(minutes) {
const session = getSession()
const before = new Date()
before.setMinutes(before.getMinutes() - minutes)
session.firstInteraction = before
function substractMinutesFromStorageSession(minutes) {
const session = getStorageSession()
session.lastInteraction.setMinutes(session.lastInteraction.getMinutes() - minutes)
storage.saveAnalyticsSession(session)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/services/pilas-bloques-api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module('Unit | Service | pilas-bloques-api', function (hooks) {
test('should add context to body', async function (assert) {
await api.login({})
const context = fetchCallBody().context
assertHasProps(assert, context, 'answers', 'browserId', 'firstInteraction', 'id', 'online', 'userId')
assertHasProps(assert, context, 'answers', 'browserId', 'lastInteraction', 'id', 'online', 'userId')
})

test('should add timestamp to body', async function (assert) {
Expand Down

0 comments on commit 3c40df7

Please sign in to comment.