Skip to content

Commit

Permalink
Merge branch 'hotfix/1.5.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasrafael committed Dec 4, 2019
2 parents d11cbff + fcb87f2 commit e77c3cd
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 99 deletions.
70 changes: 35 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "data-sync-agent",
"version": "1.5.3",
"version": "1.5.5",
"description": "Service responsible for data synchronization of FitBit and CVE platform with OCARIoT platform.",
"main": "dist/server.js",
"scripts": {
Expand Down Expand Up @@ -65,7 +65,7 @@
"inversify-express-utils": "^6.3.2",
"jsonwebtoken": "^8.5.1",
"moment": "^2.24.0",
"mongoose": "^5.7.12",
"mongoose": "^5.7.13",
"morgan": "^1.9.1",
"node-cron": "^2.0.3",
"query-strings-parser": "^2.1.3",
Expand Down
114 changes: 61 additions & 53 deletions src/application/service/user.auth.data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,34 @@ export class UserAuthDataService implements IUserAuthDataService {
}

public async add(item: UserAuthData): Promise<UserAuthData> {
try {
const authData: UserAuthData = await this.manageFitbitAuthData(item)
CreateUserAuthDataValidator.validate(item)
let result: UserAuthData = new UserAuthData()
return new Promise<UserAuthData>(async (resolve, reject) => {
try {
const authData: UserAuthData = await this.manageFitbitAuthData(item)
CreateUserAuthDataValidator.validate(item)
let result: UserAuthData = new UserAuthData()

authData.fitbit!.status = 'valid_token'
await this.subscribeFitbitEvents(item)
authData.fitbit!.status = 'valid_token'
await this.subscribeFitbitEvents(item)

const alreadySaved: UserAuthData = await this._userAuthDataRepo
.findOne(new Query().fromJSON({ filters: { user_id: authData.user_id! } }))
if (alreadySaved) {
authData.id = alreadySaved.id
result = await this._userAuthDataRepo.update(authData)
} else {
result = await this._userAuthDataRepo.create(authData)
}
const alreadySaved: UserAuthData = await this._userAuthDataRepo
.findOne(new Query().fromJSON({ filters: { user_id: authData.user_id! } }))
if (alreadySaved) {
authData.id = alreadySaved.id
result = await this._userAuthDataRepo.update(authData)
} else {
result = await this._userAuthDataRepo.create(authData)
}

if (authData.fitbit && authData.fitbit.last_sync) {
this._eventBus.bus.pubFitbitLastSync({ child_id: authData.user_id, last_sync: authData.fitbit.last_sync })
.then(() => this._logger.info(`Last sync from ${authData.user_id} successful published!`))
.catch(err => this._logger.error(`Error at publish last sync: ${err.message}`))
if (authData.fitbit && authData.fitbit.last_sync) {
this._eventBus.bus.pubFitbitLastSync({ child_id: authData.user_id, last_sync: authData.fitbit.last_sync })
.then(() => this._logger.info(`Last sync from ${authData.user_id} successful published!`))
.catch(err => this._logger.error(`Error at publish last sync: ${err.message}`))
}
return resolve(result)
} catch (err) {
return reject(err)
}
return Promise.resolve(result)
} catch (err) {
return Promise.reject(err)
}
})
}

public getAll(query: IQuery): Promise<Array<UserAuthData>> {
Expand Down Expand Up @@ -231,43 +233,48 @@ export class UserAuthDataService implements IUserAuthDataService {
}

private async subscribeFitbitEvents(data: UserAuthData): Promise<void> {
try {
if (!data || !data.fitbit || !data.fitbit.scope) return
return new Promise<void>(async (resolve, reject) => {
try {
if (!data || !data.fitbit || !data.fitbit.scope) return

const scopes: Array<string> = data.fitbit.scope.split(' ')
const scopes: Array<string> = data.fitbit.scope.split(' ')

if (scopes.includes('rwei')) { // Scope reference from fitbit to weight data is rwei
await this._fitbitAuthDataRepo.subscribeUserEvent(data.fitbit!, 'body', 'BODY')
}
if (scopes.includes('ract')) { // Scope reference from fitbit to activity data is ract
await this._fitbitAuthDataRepo.subscribeUserEvent(data.fitbit!, 'activities', 'ACTIVITIES')
}
if (scopes.includes('rsle')) { // Scope reference from fitbit to sleep data is rsle
await this._fitbitAuthDataRepo.subscribeUserEvent(data.fitbit!, 'sleep', 'SLEEP')
if (scopes.includes('rwei')) { // Scope reference from fitbit to weight data is rwei
await this._fitbitAuthDataRepo.subscribeUserEvent(data.fitbit!, 'body', 'BODY')
}
if (scopes.includes('ract')) { // Scope reference from fitbit to activity data is ract
await this._fitbitAuthDataRepo.subscribeUserEvent(data.fitbit!, 'activities', 'ACTIVITIES')
}
if (scopes.includes('rsle')) { // Scope reference from fitbit to sleep data is rsle
await this._fitbitAuthDataRepo.subscribeUserEvent(data.fitbit!, 'sleep', 'SLEEP')
}
return resolve()
} catch (err) {
return reject(err)
}
return Promise.resolve()
} catch (err) {
return Promise.reject(err)
}
})
}

private async unsubscribeFitbitEvents(data: UserAuthData): Promise<void> {
try {
if (!data || !data.fitbit || !data.fitbit.scope) return
return new Promise(async (resolve, reject) => {
try {
if (!data || !data.fitbit || !data.fitbit.scope) return

const scopes: Array<string> = data.fitbit.scope.split(' ')
if (scopes.includes('rwei')) { // Scope reference from fitbit to weight data is rwei
await this._fitbitAuthDataRepo.unsubscribeUserEvent(data.fitbit!, 'body', 'BODY')
}
if (scopes.includes('ract')) { // Scope reference from fitbit to activity data is ract
await this._fitbitAuthDataRepo.unsubscribeUserEvent(data.fitbit!, 'activities', 'ACTIVITIES')
}
if (scopes.includes('rsle')) { // Scope reference from fitbit to sleep data is rsle
await this._fitbitAuthDataRepo.unsubscribeUserEvent(data.fitbit!, 'sleep', 'SLEEP')
const scopes: Array<string> = data.fitbit.scope.split(' ')
if (scopes.includes('rwei')) { // Scope reference from fitbit to weight data is rwei
await this._fitbitAuthDataRepo.unsubscribeUserEvent(data.fitbit!, 'body', 'BODY')
}
if (scopes.includes('ract')) { // Scope reference from fitbit to activity data is ract
await this._fitbitAuthDataRepo.unsubscribeUserEvent(data.fitbit!, 'activities', 'ACTIVITIES')
}
if (scopes.includes('rsle')) { // Scope reference from fitbit to sleep data is rsle
await this._fitbitAuthDataRepo.unsubscribeUserEvent(data.fitbit!, 'sleep', 'SLEEP')
}
return resolve()
} catch (err) {
return reject(err)
}
} catch (err) {
return Promise.reject(err)
}
})
}

private async manageFitbitAuthData(data: UserAuthData): Promise<UserAuthData> {
Expand All @@ -281,9 +288,9 @@ export class UserAuthDataService implements IUserAuthDataService {
if (payload.scopes) data.fitbit!.scope = payload.scopes
if (payload.exp) data.fitbit!.expires_in = payload.exp
data.fitbit!.token_type = 'Bearer'
resolve(data)
return resolve(data)
} catch (err) {
reject(err)
return reject(err)
}
})
}
Expand Down Expand Up @@ -332,6 +339,7 @@ export class UserAuthDataService implements IUserAuthDataService {
break
}

this._logger.error(`Fitbit error: ${JSON.stringify(fitbit)}`)
this._eventBus.bus.pubFitbitAuthError(fitbit)
.then(() => this._logger.info(`Error message about ${error.type} from ${userId} successful published!`))
.catch(err => this._logger.error(`Error at publish error message from ${userId}: ${err.message}`))
Expand Down
12 changes: 4 additions & 8 deletions src/infrastructure/database/connection.factory.mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,20 @@ export class ConnectionFactoryMongoDB implements IConnectionFactory {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
bufferMaxEntries: 0,
reconnectTries: Number.MAX_SAFE_INTEGER,
reconnectInterval: 2000
useUnifiedTopology: true
}

/**
* Create instance of MongoDb.
* Create instance of MongoDB.
*
* @param uri This specification defines an URI scheme.
* For more details see: {@link https://docs.mongodb.com/manual/reference/connection-string/}
* @param options {IDBOptions} Connection setup Options.
* @return Promise<Connection>
*/
public createConnection(uri: string, options?: IDBOptions): Promise<Connection> {
if (options && options.retries && options.retries > 0) this.options.reconnectTries = options.retries
if (options && options.interval) this.options.reconnectInterval = options.interval

// if (options && options.retries && options.retries > 0) this.options.reconnectTries = options.retries
// if (options && options.interval) this.options.reconnectInterval = options.interval
return new Promise<Connection>((resolve, reject) => {
mongoose.connect(uri, this.options)
.then((result: Mongoose) => resolve(result.connection))
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/repository/fitbit.data.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ export class FitbitDataRepository implements IFitbitDataRepository {
}

private async getLastUserActivities(token: string): Promise<any> {
const now: string = moment().format('YYYY-MM-DD')
const now: string = moment().add(1, 'day').format('YYYY-MM-DD')
const path: string = `/activities/list.json?beforeDate=${now}&sort=desc&offset=0&limit=100`
return new Promise<any>((resolve, reject) => {
this._fitbitClientRepo.getDataFromPath(path, token)
Expand Down

0 comments on commit e77c3cd

Please sign in to comment.