Skip to content

Commit

Permalink
Merge branch 'kathy-cpa' into moveon-prod
Browse files Browse the repository at this point in the history
  • Loading branch information
crayolakat committed Feb 28, 2024
2 parents ce0fbcc + acede3f commit 7edcc18
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 30 deletions.
35 changes: 6 additions & 29 deletions src/server/api/mutations/getOptOutMessage.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
import { getConfig } from "../lib/config";
import SmartyStreetsSDK from "smartystreets-javascript-sdk";
import optOutMessageCache from "../../models/cacheable_queries/opt-out-message";

const SmartyStreetsCore = SmartyStreetsSDK.core;
const Lookup = SmartyStreetsSDK.usZipcode.Lookup;

const clientBuilder = new SmartyStreetsCore.ClientBuilder(
new SmartyStreetsCore.StaticCredentials(
getConfig("SMARTY_AUTH_ID"),
getConfig("SMARTY_AUTH_TOKEN")
)
);
const client = clientBuilder.buildUsZipcodeClient();
import zipStateCache from "../../models/cacheable_queries/zip";

export const getOptOutMessage = async (
_,
{ organizationId, zip, defaultMessage }
) => {
const lookup = new Lookup();

lookup.zipCode = zip;

try {
const res = await client.send(lookup);
const lookupRes = res.lookups[0].result[0];
const queryResult = await optOutMessageCache.query({
organizationId: organizationId,
state: await zipStateCache.query({ zip: zip })
});

if (lookupRes.valid) {
const queryResult = await optOutMessageCache.query({
organizationId: organizationId,
state: lookupRes.zipcodes[0].stateAbbreviation
});

return queryResult || defaultMessage;
}

return defaultMessage;
return queryResult || defaultMessage;
} catch (e) {
console.error(e);
return defaultMessage;
Expand Down
4 changes: 3 additions & 1 deletion src/server/models/cacheable_queries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ manually referencing a key inline. All root keys are prefixed by the environmen
* SET `optouts${-orgId|}`
* if OPTOUTS_SHARE_ALL_ORGS is set, then orgId=''
* optOutMessage
* SET `optoutmessages-${orgId}`
* KEY `optoutmessages-${orgId}`
* campaign-contact (only when `REDIS_CONTACT_CACHE=1`)
* KEY `contact-${contactId}`
* Besides contact data, also includes `organization_id`, `messageservice_sid`, `zip.city`, `zip.state`
Expand All @@ -130,3 +130,5 @@ manually referencing a key inline. All root keys are prefixed by the environmen
* message (only when `REDIS_CONTACT_CACHE=1`)
* LIST `messages-${contactId}`
* Includes all message data
* zip
* KEY `state-of-${zip}`
65 changes: 65 additions & 0 deletions src/server/models/cacheable_queries/zip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { getConfig } from "../../api/lib/config";
import { r } from "..";
import SmartyStreetsSDK from "smartystreets-javascript-sdk";

// SmartyStreets
const SmartyStreetsCore = SmartyStreetsSDK.core;
const Lookup = SmartyStreetsSDK.usZipcode.Lookup;

const clientBuilder = new SmartyStreetsCore.ClientBuilder(
new SmartyStreetsCore.StaticCredentials(
getConfig("SMARTY_AUTH_ID"),
getConfig("SMARTY_AUTH_TOKEN")
)
);
const client = clientBuilder.buildUsZipcodeClient();

// Cache
const cacheKey = zip => `${process.env.CACHE_PREFIX || ""}state-of-${zip}`;

const zipStateCache = {
clearQuery: async ({ zip }) => {
if (r.redis) {
await r.redis.delAsync(cacheKey(zip));
}
},
query: async ({ zip }) => {
async function getState() {
const lookup = new Lookup();

lookup.zipCode = zip;

const res = await client.send(lookup);
const lookupRes = res.lookups[0].result[0];

if (lookupRes.valid) {
return lookupRes.zipcodes[0].stateAbbreviation;
} else {
throw new Error(`State not found for zip code ${zip}`);
}
}

if (r.redis) {
const key = cacheKey(zip);
let state = await r.redis.getAsync(key);

if (state !== null) {
return state;
}

state = await getState();

await r.redis
.multi()
.set(key, state)
.expire(key, 15780000) // 6 months
.execAsync();

return state;
}

return await getState();
}
};

export default zipStateCache;

0 comments on commit 7edcc18

Please sign in to comment.