Skip to content

Commit

Permalink
chore: PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Lazar committed Oct 14, 2022
1 parent 05b8f8b commit d43d80a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import { analyticsPush } from '../commands/analytics';
import { invokeAuthPush } from '../plugin-client-api-auth';
import { getAllDefaults } from '../provider-utils/awscloudformation/default-values/pinpoint-defaults';
import { getAnalyticsResources } from '../utils/analytics-helper';
import { readCFNTemplate, writeCFNTemplate } from '../../../amplify-cli-core/src/cfnUtilities';
import {
checkIfNotificationsCategoryHasPinpoint,
getTemplateMappings,
getNotificationsCategoryHasPinpointIfExists,
getPinpointRegionMappings,
pinpointHasInAppMessagingPolicy,
pinpointInAppMessagingPolicyName,
} from '../utils/pinpoint-helper';
Expand All @@ -40,34 +41,30 @@ export const inAppMessagingMigrationCheck = async (context: $TSContext): Promise
});
}

const pinpointApp = checkIfNotificationsCategoryHasPinpoint(context);
const pinpointApp = getNotificationsCategoryHasPinpointIfExists();
if (resources.length === 0 && pinpointApp) {
const defaultValues = getAllDefaults(context.amplify.getProjectDetails());
const resourceParameters = getAllDefaults(context.amplify.getProjectDetails());
const notificationsInfo = {
appName: pinpointApp.appName,
resourceName: pinpointApp.appName,
};

Object.assign(defaultValues, notificationsInfo);
Object.assign(resourceParameters, notificationsInfo);

const resource = defaultValues.resourceName;
delete defaultValues.resourceName;
const resource = resourceParameters.resourceName;
delete resourceParameters.resourceName;
const analyticsResourcePath = path.join(projectBackendDirPath, AmplifyCategories.ANALYTICS, resource);
fs.ensureDirSync(analyticsResourcePath);

const parametersFileName = 'parameters.json';
const parametersFilePath = path.join(analyticsResourcePath, parametersFileName);
const jsonString = JSON.stringify(defaultValues, null, 4);
fs.writeFileSync(parametersFilePath, jsonString, 'utf8');
stateManager.setResourceParametersJson(projectBackendDirPath, AmplifyCategories.ANALYTICS, resource, resourceParameters);

const templateFileName = 'pinpoint-cloudformation-template.json';
const templateFilePath = path.join(analyticsResourcePath, templateFileName);
if (!fs.existsSync(templateFilePath)) {
const templateSourceFilePath = `${__dirname}/../provider-utils/awscloudformation/cloudformation-templates/${templateFileName}`;
const templateSource = context.amplify.readJsonFile(templateSourceFilePath);
templateSource.Mappings = await getTemplateMappings(context);
const templateJsonString = JSON.stringify(templateSource, null, 4);
fs.writeFileSync(templateFilePath, templateJsonString, 'utf8');
const templateSourceFilePath = path.join(__dirname, '..', 'provider-utils', 'awscloudformation', 'cloudformation-templates', templateFileName);
const { cfnTemplate } = readCFNTemplate(templateSourceFilePath);
cfnTemplate.Mappings = await getPinpointRegionMappings(context);
writeCFNTemplate(cfnTemplate, templateFilePath);
}

const options = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import os from 'os';
import {
$TSContext, ResourceAlreadyExistsError, exitOnNextTick, AmplifyCategories,
$TSAny,
JSONUtilities,
} from 'amplify-cli-core';
import { printer } from 'amplify-prompts';
import { checkIfNotificationsCategoryHasPinpoint, getTemplateMappings } from '../../../utils/pinpoint-helper';
import { getNotificationsCategoryHasPinpointIfExists, getPinpointRegionMappings } from '../../../utils/pinpoint-helper';

// FIXME: may be removed from here, since addResource can pass category to addWalkthrough
const category = AmplifyCategories.ANALYTICS;
Expand Down Expand Up @@ -63,7 +64,7 @@ const configure = (
Object.assign(defaultValues, parameters);
}

const pinpointApp = checkIfNotificationsCategoryHasPinpoint(context);
const pinpointApp = getNotificationsCategoryHasPinpointIfExists();

if (pinpointApp) {
Object.assign(defaultValues, pinpointApp);
Expand Down Expand Up @@ -198,11 +199,10 @@ const writeCfnFile = async (context: $TSContext, resourceDirPath: string, force
fs.ensureDirSync(resourceDirPath);
const templateFilePath = path.join(resourceDirPath, templateFileName);
if (!fs.existsSync(templateFilePath) || force) {
const templateSourceFilePath = `${__dirname}/../cloudformation-templates/${templateFileName}`;
const templateSourceFilePath = path.join(__dirname, '..', 'cloudformation-templates', templateFileName);
const templateSource = context.amplify.readJsonFile(templateSourceFilePath);
templateSource.Mappings = await getTemplateMappings(context);
const jsonString = JSON.stringify(templateSource, null, 4);
fs.writeFileSync(templateFilePath, jsonString, 'utf8');
templateSource.Mappings = await getPinpointRegionMappings(context);
JSONUtilities.writeJson(templateFilePath, templateSource);
}
};

Expand Down
31 changes: 18 additions & 13 deletions packages/amplify-category-analytics/src/utils/pinpoint-helper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import {
AmplifySupportedService,
pathManager, readCFNTemplate,
open, $TSAny, $TSContext, $TSMeta, AmplifyCategories,
open, $TSAny, $TSContext, $TSMeta, AmplifyCategories, stateManager,
} from 'amplify-cli-core';
import { printer } from 'amplify-prompts';
import * as path from 'path';
import { getAnalyticsResources } from './analytics-helper';

/**
*
*/
export type PinpointApp = {
appId: string;
appName: string;
};

export const pinpointInAppMessagingPolicyName = 'pinpointInAppMessagingPolicyName';

/**
Expand Down Expand Up @@ -88,35 +96,32 @@ export const pinpointHasInAppMessagingPolicy = (context: $TSContext): boolean =>
/**
* checks if notifications category has a pinpoint resource - legacy projects
*/
export const checkIfNotificationsCategoryHasPinpoint = (context: $TSContext): $TSAny => {
const { amplify } = context;
const { amplifyMeta } = amplify.getProjectDetails();
let pinpointApp: $TSAny;
export const getNotificationsCategoryHasPinpointIfExists = (): PinpointApp | undefined => {
const amplifyMeta = stateManager.getMeta();

if (amplifyMeta.notifications) {
const categoryResources = amplifyMeta.notifications;
Object.keys(categoryResources).forEach(resource => {
if (categoryResources[resource].service === AmplifySupportedService.PINPOINT && categoryResources[resource].output.Id) {
pinpointApp = {};
pinpointApp.appId = categoryResources[resource].output.Id;
pinpointApp.appName = resource;
return {
appId: categoryResources[resource].output.Id,
appName: resource,
};
}
});
}

return pinpointApp;
return undefined;
};

/**
* returns provider pinpoint region mapping
*/
export const getTemplateMappings = async (context: $TSContext): Promise<Record<string, $TSAny>> => {
export const getPinpointRegionMappings = async (context: $TSContext): Promise<Record<string, $TSAny>> => {
const Mappings: Record<string, $TSAny> = {
RegionMapping: {},
};
const providerPlugins = context.amplify.getProviderPlugins(context);
const provider = await import(providerPlugins.awscloudformation);
const regionMapping = provider.getPinpointRegionMapping();
const regionMapping: $TSAny = await context.amplify.invokePluginMethod(context, 'awscloudformation', undefined, 'getPinpointRegionMapping', []);
Object.keys(regionMapping).forEach(region => {
Mappings.RegionMapping[region] = {
pinpointRegion: regionMapping[region],
Expand Down

0 comments on commit d43d80a

Please sign in to comment.