diff --git a/package-lock.json b/package-lock.json index 75d2f94..be60607 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "data-sync-agent", - "version": "1.5.1", + "version": "1.5.5", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -200,9 +200,9 @@ } }, "@types/chai": { - "version": "4.2.5", - "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.2.5.tgz", - "integrity": "sha512-YvbLiIc0DbbhiANrfVObdkLEHJksQZVq0Uvfg550SRAKVYaEJy+V70j65BVe2WNp6E3HtKsUczeijHFCjba3og==", + "version": "4.2.6", + "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.2.6.tgz", + "integrity": "sha512-HF8faEUA4JurIm+68VaA2KedtZf5LYdXpQEAbIAN79DwWQbO82BNTksZgCH3UMqbZHXex9C6TrBfg7OUInRISQ==", "dev": true }, "@types/connect": { @@ -251,9 +251,9 @@ } }, "@types/ioredis": { - "version": "4.0.20", - "resolved": "https://registry.npmjs.org/@types/ioredis/-/ioredis-4.0.20.tgz", - "integrity": "sha512-8Z5k3yc7PWOpckWDgGKUQ7ebQT2daI1ga8j+gRbvPKh7XndOUkqhY1dFUG4dIaUzlvVNejLC9Ab7v9BC25gRiA==", + "version": "4.0.21", + "resolved": "https://registry.npmjs.org/@types/ioredis/-/ioredis-4.0.21.tgz", + "integrity": "sha512-IGMWaWsjPDh+aBx/HClsnUkalN+oPmSvUZxYUZVlOMPe9BZN8bi/2erTgSPDyIT52B9oHf/oSuW74OlmjJijkQ==", "dev": true, "requires": { "@types/node": "*" @@ -847,9 +847,9 @@ } }, "bluebird": { - "version": "3.7.1", - "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.1.tgz", - "integrity": "sha512-DdmyoGCleJnkbp3nkbxTLJ18rjDsE4yCggEwKNXkeV123sPNfOCYeDoeuOY+F2FrSjO1YXcTU+dsy96KMy+gcg==" + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", + "integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==" }, "body-parser": { "version": "1.19.0", @@ -1938,9 +1938,9 @@ } }, "es-abstract": { - "version": "1.16.2", - "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.16.2.tgz", - "integrity": "sha512-jYo/J8XU2emLXl3OLwfwtuFfuF2w6DYPs+xy9ZfVyPkDcrauu6LYrw/q2TyCtrbc/KUdCiC5e9UajRhgNkVopA==", + "version": "1.16.3", + "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.16.3.tgz", + "integrity": "sha512-WtY7Fx5LiOnSYgF5eg/1T+GONaGmpvpPdCpSnYij+U2gDTL0UPfWrhDw7b2IYb+9NQJsYpCA0wOQvZfsd6YwRw==", "requires": { "es-to-primitive": "^1.2.1", "function-bind": "^1.1.1", @@ -2144,9 +2144,9 @@ } }, "ext": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/ext/-/ext-1.2.1.tgz", - "integrity": "sha512-x+OKKC57tNiLhDW26UmWtvQBpvO+2wxdC/A0jP7RkmjAc4gze9/U98hQyIYJUzo9A+o9ntMHpC+LH3pWMSbrVQ==", + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/ext/-/ext-1.4.0.tgz", + "integrity": "sha512-Key5NIsUxdqKg3vIsdw9dSuXpPCQ297y6wBjL30edxwPgt2E44WcWBZey/ZvUc6sERLTxKdyCu4gZFmUbk1Q7A==", "dev": true, "requires": { "type": "^2.0.0" @@ -5378,9 +5378,9 @@ } }, "mongodb": { - "version": "3.3.4", - "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.3.4.tgz", - "integrity": "sha512-6fmHu3FJTpeZxacJcfjUGIP3BSteG0l2cxLkSrf1nnnS1OrlnVGiP9P/wAC4aB6dM6H4vQ2io8YDjkuPkje7AA==", + "version": "3.3.5", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.3.5.tgz", + "integrity": "sha512-6NAv5gTFdwRyVfCz+O+KDszvjpyxmZw+VlmqmqKR2GmpkeKrKFRv/ZslgTtZba2dc9JYixIf99T5Gih7TIWv7Q==", "requires": { "bson": "^1.1.1", "require_optional": "^1.0.1", @@ -5389,13 +5389,13 @@ } }, "mongoose": { - "version": "5.7.12", - "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.7.12.tgz", - "integrity": "sha512-TqRiJPUeHN1u7Xs/zQmiMHcrzEb1/uKjK3uGzZhyqw25RKQiMV2vSBeBTMt5HXoYlUuDEWXE75FMjpK9X9kD0w==", + "version": "5.7.13", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.7.13.tgz", + "integrity": "sha512-MoTCETTIfXU1PqIYc+YAkhSB3uzK5xseh6ysAUnoOowkqS66OjBJtsL/bcgAKi6XfMYkFU6M1abKyuL7k//xeg==", "requires": { "bson": "~1.1.1", "kareem": "2.3.1", - "mongodb": "3.3.4", + "mongodb": "3.3.5", "mongoose-legacy-pluralize": "1.0.2", "mpath": "0.6.0", "mquery": "3.2.2", @@ -6400,9 +6400,9 @@ "dev": true }, "psl": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/psl/-/psl-1.4.0.tgz", - "integrity": "sha512-HZzqCGPecFLyoRj5HLfuDSKYTJkAfB5thKBIkRHtGjWwY7p1dAyveIbXIq4tO0KYfDF2tHqPUgY9SDnGm00uFw==" + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/psl/-/psl-1.5.0.tgz", + "integrity": "sha512-4vqUjKi2huMu1OJiLhi3jN6jeeKvMZdI1tYgi/njW5zV52jNLgSAZSdN16m9bJFe61/cT8ulmw4qFitV9QRsEA==" }, "pstree.remy": { "version": "1.1.7", @@ -7809,9 +7809,9 @@ "integrity": "sha512-XrHUvV5HpdLmIj4uVMxHggLbFSZYIn7HEWsqePZcI50pco+MPqJ50wMGY794X7AOOhxOBAjbkqfAbEe/QMp2Lw==" }, "ts-node": { - "version": "8.5.2", - "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-8.5.2.tgz", - "integrity": "sha512-W1DK/a6BGoV/D4x/SXXm6TSQx6q3blECUzd5TN+j56YEMX3yPVMpHsICLedUw3DvGF3aTQ8hfdR9AKMaHjIi+A==", + "version": "8.5.4", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-8.5.4.tgz", + "integrity": "sha512-izbVCRV68EasEPQ8MSIGBNK9dc/4sYJJKYA+IarMQct1RtEot6Xp0bXuClsbUSnKpg50ho+aOAx8en5c+y4OFw==", "dev": true, "requires": { "arg": "^4.1.0", @@ -7956,9 +7956,9 @@ } }, "typescript": { - "version": "3.7.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.2.tgz", - "integrity": "sha512-ml7V7JfiN2Xwvcer+XAf2csGO1bPBdRbFCkYBczNZggrBZ9c7G3riSUeJmqEU5uOtXNPMhE3n+R4FA/3YOAWOQ==", + "version": "3.7.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.3.tgz", + "integrity": "sha512-Mcr/Qk7hXqFBXMN7p7Lusj1ktCBydylfQM/FZCk5glCNQJrCUKPkMHdo9R0MTFWsC/4kPFvDS0fDPvukfCkFsw==", "dev": true }, "tz-offset": { @@ -7967,9 +7967,9 @@ "integrity": "sha512-kMBmblijHJXyOpKzgDhKx9INYU4u4E1RPMB0HqmKSgWG8vEcf3exEfLh4FFfzd3xdQOw9EuIy/cP0akY6rHopQ==" }, "uglify-js": { - "version": "3.7.0", - "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.7.0.tgz", - "integrity": "sha512-PC/ee458NEMITe1OufAjal65i6lB58R1HWMRcxwvdz1UopW0DYqlRL3xdu3IcTvTXsB02CRHykidkTRL+A3hQA==", + "version": "3.7.1", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.7.1.tgz", + "integrity": "sha512-pnOF7jY82wdIhATVn87uUY/FHU+MDUdPLkmGFvGoclQmeu229eTkbG5gjGGBi3R7UuYYSEeYXY/TTY5j2aym2g==", "dev": true, "optional": true, "requires": { diff --git a/package.json b/package.json index 3a0e3e8..4c143a9 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/src/application/service/user.auth.data.service.ts b/src/application/service/user.auth.data.service.ts index dd17c2b..46c63f9 100644 --- a/src/application/service/user.auth.data.service.ts +++ b/src/application/service/user.auth.data.service.ts @@ -26,32 +26,34 @@ export class UserAuthDataService implements IUserAuthDataService { } public async add(item: UserAuthData): Promise { - try { - const authData: UserAuthData = await this.manageFitbitAuthData(item) - CreateUserAuthDataValidator.validate(item) - let result: UserAuthData = new UserAuthData() + return new Promise(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> { @@ -231,43 +233,48 @@ export class UserAuthDataService implements IUserAuthDataService { } private async subscribeFitbitEvents(data: UserAuthData): Promise { - 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 = data.fitbit.scope.split(' ') + const scopes: Array = 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 { - 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 = 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 = 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 { @@ -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) } }) } @@ -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}`)) diff --git a/src/infrastructure/database/connection.factory.mongodb.ts b/src/infrastructure/database/connection.factory.mongodb.ts index d7d2bf9..6e35db4 100644 --- a/src/infrastructure/database/connection.factory.mongodb.ts +++ b/src/infrastructure/database/connection.factory.mongodb.ts @@ -8,14 +8,11 @@ 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/} @@ -23,9 +20,8 @@ export class ConnectionFactoryMongoDB implements IConnectionFactory { * @return Promise */ public createConnection(uri: string, options?: IDBOptions): Promise { - 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((resolve, reject) => { mongoose.connect(uri, this.options) .then((result: Mongoose) => resolve(result.connection)) diff --git a/src/infrastructure/repository/fitbit.data.repository.ts b/src/infrastructure/repository/fitbit.data.repository.ts index bc56686..6b12bf9 100644 --- a/src/infrastructure/repository/fitbit.data.repository.ts +++ b/src/infrastructure/repository/fitbit.data.repository.ts @@ -566,7 +566,7 @@ export class FitbitDataRepository implements IFitbitDataRepository { } private async getLastUserActivities(token: string): Promise { - 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((resolve, reject) => { this._fitbitClientRepo.getDataFromPath(path, token)