Skip to content

Commit

Permalink
fix(amplify-category-notifications): fix redundant pinpoint creation (#…
Browse files Browse the repository at this point in the history
…474)

* resolve env config issue

* minor fix

* through lint

* minor fix

* add multienv support for hosting

* minor fix

* minor fix

* fix two notifications issues

* through lint

* code refactor

* through lint

* change per review request

* through lint

* work in progress

* work in progress

* refactor(amplify-category-notifications): minor fix

minor fix

* refactor(amplify-category-notifications): minor fix

minor fix

* style(amplify-category-notifications): fix lint

fix lint

* style(amplify-category-notifications): change per review comments

change per preview comments

* perf(amplify-category-notifications): minor fix

minor fix
  • Loading branch information
UnleashedMind committed Nov 19, 2018
1 parent 4371dc7 commit 5e28dc0
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 64 deletions.
25 changes: 14 additions & 11 deletions packages/amplify-category-notifications/lib/channel-APNS.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,22 @@ function pull(context, pinpointApp) {
const params = {
ApplicationId: pinpointApp.Id,
};
spinner.start(`Pulling ${channelName} Channel.`);
return new Promise((resolve, reject) => {
context.exeInfo.pinpointClient.getApnsChannel(params, (err, data) => {
if (err) {
spinner.fail(`get channel ${channelName} error`);
reject(err);
} else {
spinner.succeed(`get ${channelName} channel successful`);
pinpointApp[channelName] = data.APNSChannelResponse;
resolve(data.APNSChannelResponse);

spinner.start(`Retrieving channel information for ${channelName}.`);
return context.exeInfo.pinpointClient.getApnsChannel(params).promise()
.then((data) => {
spinner.succeed(`Channel information retrieved for ${channelName}`);
pinpointApp[channelName] = data.APNSChannelResponse;
return data.APNSChannelResponse;
})
.catch((err) => {
if (err.code === 'NotFoundException') {
spinner.succeed(`Channel is not setup for ${channelName} `);
return err;
}
spinner.stop();
throw err;
});
});
}

module.exports = {
Expand Down
25 changes: 14 additions & 11 deletions packages/amplify-category-notifications/lib/channel-Email.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,22 @@ function pull(context, pinpointApp) {
const params = {
ApplicationId: pinpointApp.Id,
};
spinner.start(`Pulling ${channelName} Channel.`);
return new Promise((resolve, reject) => {
context.exeInfo.pinpointClient.getEmailChannel(params, (err, data) => {
if (err) {
spinner.fail(`get channel ${channelName} error`);
reject(err);
} else {
spinner.succeed(`get ${channelName} channel successful`);
pinpointApp[channelName] = data.EmailChannelResponse;
resolve(data.EmailChannelResponse);

spinner.start(`Retrieving channel information for ${channelName}.`);
return context.exeInfo.pinpointClient.getEmailChannel(params).promise()
.then((data) => {
spinner.succeed(`Channel information retrieved for ${channelName}`);
pinpointApp[channelName] = data.EmailChannelResponse;
return data.EmailChannelResponse;
})
.catch((err) => {
if (err.code === 'NotFoundException') {
spinner.succeed(`Channel is not setup for ${channelName} `);
return err;
}
spinner.stop();
throw err;
});
});
}

module.exports = {
Expand Down
25 changes: 14 additions & 11 deletions packages/amplify-category-notifications/lib/channel-FCM.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,22 @@ function pull(context, pinpointApp) {
const params = {
ApplicationId: pinpointApp.Id,
};
spinner.start(`Pulling ${channelName} Channel.`);
return new Promise((resolve, reject) => {
context.exeInfo.pinpointClient.getGcmChannel(params, (err, data) => {
if (err) {
spinner.fail(`get channel ${channelName} error`);
reject(err);
} else {
spinner.succeed(`get ${channelName} channel successful`);
pinpointApp[channelName] = data.GCMChannelResponse;
resolve(data.GCMChannelResponse);

spinner.start(`Retrieving channel information for ${channelName}.`);
return context.exeInfo.pinpointClient.getGcmChannel(params).promise()
.then((data) => {
spinner.succeed(`Channel information retrieved for ${channelName}`);
pinpointApp[channelName] = data.GCMChannelResponse;
return data.GCMChannelResponse;
})
.catch((err) => {
if (err.code === 'NotFoundException') {
spinner.succeed(`Channel is not setup for ${channelName} `);
return err;
}
spinner.stop();
throw err;
});
});
}

module.exports = {
Expand Down
26 changes: 14 additions & 12 deletions packages/amplify-category-notifications/lib/channel-SMS.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,25 @@ function disable(context) {
});
}

function pull(context, pinpointApp) {
async function pull(context, pinpointApp) {
const params = {
ApplicationId: pinpointApp.Id,
};
spinner.start(`Pulling ${channelName} Channel.`);
return new Promise((resolve, reject) => {
context.exeInfo.pinpointClient.getSmsChannel(params, (err, data) => {
if (err) {
spinner.fail(`get channel ${channelName} error`);
reject(err);
} else {
spinner.succeed(`get ${channelName} channel successful`);
pinpointApp[channelName] = data.GCMChannelResponse;
resolve(data.SMSChannelResponse);
spinner.start(`Retrieving channel information for ${channelName}.`);
return context.exeInfo.pinpointClient.getSmsChannel(params).promise()
.then((data) => {
spinner.succeed(`Channel information retrieved for ${channelName}`);
pinpointApp[channelName] = data.SMSChannelResponse;
return data.SMSChannelResponse;
})
.catch((err) => {
if (err.code === 'NotFoundException') {
spinner.succeed(`Channel is not setup for ${channelName} `);
return err;
}
spinner.stop();
throw err;
});
});
}

module.exports = {
Expand Down
29 changes: 12 additions & 17 deletions packages/amplify-category-notifications/lib/multi-env-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,21 @@ async function constructPinpointNotificationsMeta(context) {
let serviceBackendConfig;
let pinpointNotificationsMeta;

const { envName } = context.exeInfo.localEnvInfo;
const { teamProviderInfo, localEnvInfo, amplifyMeta } = context.exeInfo;

const teamProviderInfoFilepath = context.amplify.pathManager.getProviderInfoFilePath();
if (fs.existsSync(teamProviderInfoFilepath)) {
const teamProviderInfo = JSON.parse(fs.readFileSync(teamProviderInfoFilepath));
if (teamProviderInfo[envName] &&
teamProviderInfo[envName].categories &&
teamProviderInfo[envName].categories[constants.CategoryName] &&
teamProviderInfo[envName].categories[constants.CategoryName][constants.PinpointName] &&
teamProviderInfo[envName].categories[constants.CategoryName][constants.PinpointName].id) {
pinpointApp =
teamProviderInfo[envName].categories[constants.CategoryName][constants.PinpointName];
}
const { envName } = localEnvInfo;

if (teamProviderInfo &&
teamProviderInfo[envName] &&
teamProviderInfo[envName].categories &&
teamProviderInfo[envName].categories[constants.CategoryName] &&
teamProviderInfo[envName].categories[constants.CategoryName][constants.PinpointName] &&
teamProviderInfo[envName].categories[constants.CategoryName][constants.PinpointName].Id) {
pinpointApp =
teamProviderInfo[envName].categories[constants.CategoryName][constants.PinpointName];
}

if (!pinpointApp) {
const metaFilePath = context.amplify.pathManager.getAmplifyMetaFilePath();
const amplifyMeta = JSON.parse(fs.readFileSync(metaFilePath));
const analyticsMeta = amplifyMeta[constants.AnalyticsCategoryName];
pinpointApp = pinpointHelper.scanCategoryMetaForPinpoint(analyticsMeta);
}
Expand All @@ -64,7 +61,7 @@ async function constructPinpointNotificationsMeta(context) {
await notificationManager.pullAllChannels(context, pinpointApp);
pinpointNotificationsMeta = {
Name: pinpointApp.Name,
serivce: constants.PinpointName,
service: constants.PinpointName,
output: pinpointApp,
};
}
Expand Down Expand Up @@ -150,7 +147,6 @@ async function pushChanges(context, pinpointNotificationsMeta) {
needToBeEnabled = pinpointInputParams[channel].Enabled;
}


// if (isCurrentlyEnabled && needToBeEnabled) {
// channelsToUpdate.push(channel);
// }
Expand All @@ -164,7 +160,6 @@ async function pushChanges(context, pinpointNotificationsMeta) {
const { amplifyMeta } = context.exeInfo;
amplifyMeta[constants.CategoryName] = {};
amplifyMeta[constants.CategoryName][pinpointNotificationsMeta.Name] = pinpointNotificationsMeta;

delete pinpointNotificationsMeta.channels;
delete pinpointNotificationsMeta.Name;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path');
const sequential = require('promise-sequential');
const constants = require('./constants');
const pintpointHelper = require('./pinpoint-helper');

Expand Down Expand Up @@ -79,9 +80,9 @@ async function pullAllChannels(context, pinpointApp) {
context.exeInfo.pinpointClient = await pintpointHelper.getPinpointClient(context);
Object.keys(channelWorkers).forEach((channelName) => {
const channelWorker = require(path.join(__dirname, channelWorkers[channelName]));
pullTasks.push(() => { channelWorker.pull(context, pinpointApp); });
pullTasks.push(() => channelWorker.pull(context, pinpointApp));
});
await Promise.all(pullTasks);
await sequential(pullTasks);
}

module.exports = {
Expand Down

0 comments on commit 5e28dc0

Please sign in to comment.