Skip to content

Commit

Permalink
fix resync function
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPonchia committed Dec 19, 2024
1 parent a3d4bed commit 294c2b9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ export async function resyncUserHandler(event: {
readonly Records: SQSEvent['Records'];
}): Promise<APIGatewayProxyResult> {
try {
console.log('resyncUserHandler: Event:', event); // TODO: Remove after testing
const queueEvent = queueEventParser(event);
const cognitoUsername = queueEvent.detail.additionalEventData.sub;

const user = await getUserFromCognitoUsername(cognitoUsername);

console.log('user:', user); // TODO: Remove after testing

if (!user) {
const deletionResult = await deleteContact(cognitoUsername); // AC call * 2
if (
Expand All @@ -28,6 +31,8 @@ export async function resyncUserHandler(event: {
} else {
const contactResponse = await addOrUpdateContact(user); // AC call * 3

console.log('contactResponse:', contactResponse); // TODO: Remove after testing

const { listsToUnsubscribe, newWebinarSlugs } =
await getNewWebinarsAndUnsubsriptionLists(
contactResponse,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { acClient } from '../clients/activeCampaignClient';
import { fetchSubscribedWebinarsFromDynamo } from './fetchSubscribedWebinarsFromDynamo';
import { ActiveCampaignList } from '../types/activeCampaignList';
import { ContactResponseWithLists } from '../types/contactResponse';

export async function getNewWebinarsAndUnsubsriptionLists(
contactResponse: ContactResponseWithLists,
cognitoUsername: string
) {
// eslint-disable-next-line functional/prefer-readonly-type
const contactLists: ActiveCampaignList[] = [
...(await acClient.getLists(
contactResponse.contactLists
.filter(({ status }) => status === '1')
.map(({ list }) => list)
)),
];
const idsParams = contactResponse.contactLists
.filter(({ status }) => status === '1')
.map(({ list }) => list);

console.log('idsParams:', idsParams); // TODO: Remove after testing
const getListResponse = await acClient.getLists(idsParams);

const userWebinarsSubscriptions = await fetchSubscribedWebinarsFromDynamo(
cognitoUsername
Expand All @@ -31,17 +28,22 @@ export async function getNewWebinarsAndUnsubsriptionLists(

// eslint-disable-next-line functional/prefer-readonly-type
const newWebinarSlugs: string[] = [];
// eslint-disable-next-line functional/prefer-readonly-type
const contactLists: { name: string; id: string }[] =
getListResponse.lists.map(({ name, id }) => ({ name, id }));

webinarSlugs.forEach((webinarSlug) => {
const index = contactLists.findIndex(({ name }) => name === webinarSlug);
if (index) {
if (index >= 0) {
contactLists.splice(index, 1);
} else {
newWebinarSlugs.push(webinarSlug);
}
});

const listsToUnsubscribe = contactLists;
const listsToUnsubscribe: readonly number[] = contactLists.map(({ id }) =>
Number(id)
);
console.log('listsToUnsubscribe:', listsToUnsubscribe); // TODO: Remove after testing
console.log('New webinar Slugs:', newWebinarSlugs); // TODO: Remove after testing

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { acClient } from '../clients/activeCampaignClient';
import { ActiveCampaignList } from '../types/activeCampaignList';

export async function removeArrayOfListFromContact(event: {
readonly listsToUnsubscribe: ReadonlyArray<ActiveCampaignList>;
readonly listsToUnsubscribe: readonly number[];
readonly cognitoUsername: string;
readonly resyncTimeoutMilliseconds: number;
}) {
const { listsToUnsubscribe, cognitoUsername, resyncTimeoutMilliseconds } =
event;
// remove contact from list for each item in unsubscription lists
await listsToUnsubscribe.reduce(
async (prevPromise: Promise<void>, list: ActiveCampaignList) => {
async (prevPromise: Promise<void>, id: number) => {
await prevPromise;
// AC call * M
const result = await acClient.removeContactFromList(
cognitoUsername,
list.id
);
console.log('Remove contact from list result:', result, list); // TODO: Remove after testing
const result = await acClient.removeContactFromList(cognitoUsername, id);
console.log('Remove contact from list result:', result, id); // TODO: Remove after testing
await new Promise((resolve) =>
setTimeout(resolve, resyncTimeoutMilliseconds)
); // wait 1 sec to avoid rate limiting
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type ActiveCampaignList = {
readonly id: number;
readonly id: string;
readonly name: string;
};

0 comments on commit 294c2b9

Please sign in to comment.