Skip to content

Commit

Permalink
Merge remote-tracking branch 'lperson/2186-no-opt-out-if-no-redis' in…
Browse files Browse the repository at this point in the history
…to stage-main-12-3
  • Loading branch information
Arique1104 committed Aug 1, 2022
2 parents 95f7a73 + 3dc689e commit afbc724
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 18 deletions.
101 changes: 98 additions & 3 deletions __test__/extensions/message-handlers/auto-optout.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { postMessageSave } from "../../../src/extensions/message-handlers/auto-optout";
import { cacheableData } from "../../../src/server/models";
import { cacheableData, r } from "../../../src/server/models";

import {
setupTest,
cleanupTest,
createStartedCampaign
} from "../../test_helpers";

const sendMessage = require("../../../src/server/api/mutations/sendMessage");

const CacheableMessage = require("../../../src/server/models/cacheable_queries/message");
const saveMessage = CacheableMessage.default.save;

const AutoOptout = require("../../../src/extensions/message-handlers/auto-optout");

describe("Auto Opt-Out Tests", () => {
let message;

Expand All @@ -22,7 +34,7 @@ describe("Auto Opt-Out Tests", () => {
is_from_contact: true,
contact_number: "+123456",
campaign_contact_id: 1,
text: 'please stop'
text: "please stop"
};
});

Expand Down Expand Up @@ -76,4 +88,87 @@ describe("Auto Opt-Out Tests", () => {

expect(sendMessage.sendRawMessage).not.toHaveBeenCalled();
});
})
});

describe("Tests for Auto Opt-Out's members getting called from messageCache.save", () => {
let contacts;
let organization;
let texter;

let service;
let messageServiceSID;

afterEach(async () => {
await cleanupTest();
if (r.redis) r.redis.flushdb();
}, global.DATABASE_SETUP_TEARDOWN_TIMEOUT);

beforeEach(async () => {
await cleanupTest();
await setupTest();
jest.restoreAllMocks();
const startedCampaign = await createStartedCampaign();
({
testContacts: contacts,
testOrganization: {
data: { createOrganization: organization }
},
testTexterUser: texter
} = startedCampaign);

service = "twilio";
messageServiceSID = "message_service_sid_vicious";

const messageToContact = {
is_from_contact: false,
contact_number: contacts[0].cell,
campaign_contact_id: contacts[0].id,
send_status: "SENT",
text: "Hey now!",
service,
messageservice_sid: messageServiceSID
};
await saveMessage({
messageInstance: messageToContact,
contact: contacts[0],
organization,
texter
});
}, global.DATABASE_SETUP_TEARDOWN_TIMEOUT);

it("gets called", async () => {
const message = {
is_from_contact: true,
contact_number: contacts[0].cell,
service,
messageservice_sid: messageServiceSID,
text: "stop2quit",
send_status: "DELIVERED"
};

jest.spyOn(AutoOptout, "preMessageSave").mockResolvedValue(null);
jest.spyOn(AutoOptout, "postMessageSave").mockResolvedValue(null);

await saveMessage({
messageInstance: message
});

expect(AutoOptout.preMessageSave).toHaveBeenCalledWith(
expect.objectContaining({
messageToSave: expect.objectContaining({
text: "stop2quit",
contact_number: contacts[0].cell
})
})
);

expect(AutoOptout.postMessageSave).toHaveBeenCalledWith(
expect.objectContaining({
message: expect.objectContaining({
text: "stop2quit",
contact_number: contacts[0].cell
})
})
);
});
});
102 changes: 102 additions & 0 deletions __test__/server/models/cacheable_queries/campaign-contact.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {
setupTest,
cleanupTest,
createStartedCampaign
} from "../../../test_helpers";
import { r } from "../../../../src/server/models";

const CacheableMessage = require("../../../../src/server/models/cacheable_queries/message");
const saveMessage = CacheableMessage.default.save;

const CacheableCampaignContact = require("../../../../src/server/models/cacheable_queries/campaign-contact");
const lookupByCell = CacheableCampaignContact.default.lookupByCell;

describe("CampaignContactCache", () => {
let contacts;
let organization;
let texter;
let campaign;

let service;
let messageServiceSID;

afterEach(async () => {
await cleanupTest();
if (r.redis) r.redis.flushdb();
}, global.DATABASE_SETUP_TEARDOWN_TIMEOUT);

beforeEach(async () => {
await cleanupTest();
await setupTest();
jest.restoreAllMocks();
const startedCampaign = await createStartedCampaign();
({
testContacts: contacts,
testOrganization: {
data: { createOrganization: organization }
},
testTexterUser: texter,
testCampaign: campaign
} = startedCampaign);

service = "twilio";
messageServiceSID = "message_service_sid_vicious";
}, global.DATABASE_SETUP_TEARDOWN_TIMEOUT);

describe("lookupByCell", () => {
it("finds campaign_contact_id and campaign_id when there is a messageServiceSID", async () => {
const message = {
is_from_contact: false,
contact_number: contacts[0].cell,
campaign_contact_id: contacts[0].id,
send_status: "SENT",
text: "Hey now!",
service,
messageservice_sid: messageServiceSID
};
await saveMessage({
messageInstance: message,
contact: contacts[0],
organization,
texter
});
const foundMessage = await lookupByCell(
contacts[0].cell,
service,
messageServiceSID
);
expect(Number(foundMessage.campaign_id)).toBe(Number(campaign.id));
expect(Number(foundMessage.campaign_contact_id)).toBe(
Number(contacts[0].id)
);
});
it("finds campaign_contact_id and campaign_id when there is a userNumber", async () => {
const message = {
is_from_contact: false,
contact_number: contacts[0].cell,
campaign_contact_id: contacts[0].id,
send_status: "SENT",
text: "Hey now!",
service,
messageservice_sid: null, // necessary for sqlite tests to pass
user_number: texter.cell
};
await saveMessage({
messageInstance: message,
contact: contacts[0],
organization,
texter
});
const foundMessage = await lookupByCell(
contacts[0].cell,
service,
null, // messageServiceSid
texter.cell
);
expect(Number(foundMessage.campaign_id)).toBe(Number(campaign.id));
expect(Number(foundMessage.campaign_contact_id)).toBe(
Number(contacts[0].id)
);
});
});
});
2 changes: 1 addition & 1 deletion src/extensions/message-handlers/auto-optout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const postMessageSave = async ({
cell: message.contact_number,
campaignContactId: message.campaign_contact_id,
assignmentId: (contact && contact.assignment_id) || null,
campaign: campaign,
campaign,
noReply: true,
reason: handlerContext.autoOptOutReason,
// RISKY: we depend on the contactUpdates in preMessageSave
Expand Down
25 changes: 11 additions & 14 deletions src/server/models/cacheable_queries/campaign-contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,10 @@ const campaignContactCache = {
);
// console.log('lookupByCell cache', cell, service, messageServiceSid, cellData)
if (cellData) {
// eslint-disable-next-line camelcase
const [
campaign_contact_id,
_,
timezone_offset,
campaign_contact_id, // eslint-disable-line camelcase
_, // eslint-disable-line no-unused-vars
timezone_offset, // eslint-disable-line camelcase
...rest
] = cellData.split(":");
return {
Expand Down Expand Up @@ -415,16 +414,14 @@ const campaignContactCache = {
.whereNull("messageservice_sid")
.where("user_number", userNumber);
}
if (r.redis) {
// we get the campaign_id so we can cache errorCount and needsResponseCount
messageQuery = messageQuery
.join(
"campaign_contact",
"campaign_contact.id",
"message.campaign_contact_id"
)
.select("campaign_contact_id", "campaign_id");
}
// we get the campaign_id so we can cache errorCount and needsResponseCount
messageQuery = messageQuery
.join(
"campaign_contact",
"campaign_contact.id",
"message.campaign_contact_id"
)
.select("campaign_contact_id", "campaign_id");
const [lastMessage] = await messageQuery;
if (lastMessage) {
return {
Expand Down

0 comments on commit afbc724

Please sign in to comment.