Skip to content

Instantly share code, notes, and snippets.

@igoralves1
Last active August 23, 2024 18:18
Show Gist options
  • Save igoralves1/8b77abc8cc48054072d17df6ac345fd6 to your computer and use it in GitHub Desktop.
Save igoralves1/8b77abc8cc48054072d17df6ac345fd6 to your computer and use it in GitHub Desktop.
result ==> {
Items: [
{
reason: [Object],
GSI4PK: [Object],
GSI3PK: [Object],
GSI2PK: [Object],
fromNumber: [Object],
duration: [Object],
executionId: [Object],
GSI1PK: [Object],
GSI4SK: [Object],
GSI3SK: [Object],
GSI2SK: [Object],
GSI1SK: [Object],
SK: [Object],
flowSid: [Object],
PK: [Object],
time: [Object],
id: [Object],
toNumber: [Object],
direction: [Object],
status: [Object]
}
],
Count: 1,
ScannedCount: 1,
LastEvaluatedKey: {
SK: { S: 'SEARCH#FN846b958fe0c70b9c6c3e0388a2b004eb' },
PK: {
S: 'CALL#com.twilio.studio.flow#FN846b958fe0c70b9c6c3e0388a2b004eb'
},
GSI1PK: { S: 'SEARCH_DATE#com.twilio.studio.flow' },
GSI1SK: { S: '2024-07-25T13:24:00.351Z' }
}
}
API
const mockItems = [
{
"reason": "FLOW_END",
"GSI4PK": "SEARCH_ID#com.twilio.studio.flow",
"GSI3PK": "SEARCH_TO#com.twilio.studio.flow",
"GSI2PK": "SEARCH_FROM#com.twilio.studio.flow",
"fromNumber": "17799994699",
"duration": 0.002,
"executionId": "FNd038320e2aee704c6182d776a63e777d",
"GSI1PK": "SEARCH_DATE#com.twilio.studio.flow",
"GSI4SK": "CA3b22961df2224a0620c48945ed438c27",
"GSI3SK": "18172038030",
"GSI2SK": "17799994699",
"GSI1SK": "2024-07-26T10:58:45.448Z",
"SK": "SEARCH#FNd038320e2aee704c6182d776a63e777d",
"flowSid": "FW981d3b54b2a51179c93d147a3646ccb3",
"PK": "CALL#com.twilio.studio.flow#FNd038320e2aee704c6182d776a63e777d",
"time": "2024-07-26T10:58:45.448Z",
"id": "CA3b22961df2224a0620c48945ed438c27",
"toNumber": "18172038030",
"direction": "inbound",
"status": "Complete"
}
];
FROM API
{
"data": [
{
"reason": "FLOW_END",
"GSI4PK": "SEARCH_ID#com.twilio.studio.flow",
"GSI3PK": "SEARCH_TO#com.twilio.studio.flow",
"GSI2PK": "SEARCH_FROM#com.twilio.studio.flow",
"fromNumber": "18886602449",
"duration": 0.026,
"executionId": "FN846b958fe0c70b9c6c3e0388a2b004eb",
"GSI1PK": "SEARCH_DATE#com.twilio.studio.flow",
"GSI4SK": "CAf0473b17886439dc380356419cde4818",
"GSI3SK": "15108043852",
"GSI2SK": "18886602449",
"GSI1SK": "2024-07-25T13:24:00.351Z",
"SK": "SEARCH#FN846b958fe0c70b9c6c3e0388a2b004eb",
"flowSid": "FWd7b6ceaf8004f55275f8dc00c16589d2",
"PK": "CALL#com.twilio.studio.flow#FN846b958fe0c70b9c6c3e0388a2b004eb",
"time": "2024-07-25T13:24:00.351Z",
"id": "CAf0473b17886439dc380356419cde4818",
"toNumber": "15108043852",
"direction": "inbound",
"status": "Complete"
}
],
"count": 1,
"scannedCount": 1,
"lastEvaluatedKey": "eyJTSyI6eyJTIjoiU0VBUkNII0ZOODQ2Yjk1OGZlMGM3MGI5YzZjM2UwMzg4YTJiMDA0ZWIifSwiUEsiOnsiUyI6IkNBTEwjY29tLnR3aWxpby5zdHVkaW8uZmxvdyNGTjg0NmI5NThmZTBjNzBiOWM2YzNlMDM4OGEyYjAwNGViIn0sIkdTSTFQSyI6eyJTIjoiU0VBUkNIX0RBVEUjY29tLnR3aWxpby5zdHVkaW8uZmxvdyJ9LCJHU0kxU0siOnsiUyI6IjIwMjQtMDctMjVUMTM6MjQ6MDAuMzUxWiJ9fQ=="
}
============
it("should return search results between startDate and endDate", async () => {
const mockResponse = {
Items: [
{
reason: { S: "FLOW_END" },
GSI4PK: { S: "SEARCH_ID#com.twilio.studio.flow" },
GSI3PK: { S: "SEARCH_TO#com.twilio.studio.flow" },
GSI2PK: { S: "SEARCH_FROM#com.twilio.studio.flow" },
fromNumber: { S: "18886602449" },
duration: { N: "0.026" },
executionId: { S: "FN846b958fe0c70b9c6c3e0388a2b004eb" },
GSI1PK: { S: "SEARCH_DATE#com.twilio.studio.flow" },
GSI4SK: { S: "CAf0473b17886439dc380356419cde4818" },
GSI3SK: { S: "15108043852" },
GSI2SK: { S: "18886602449" },
GSI1SK: { S: "2024-07-25T13:24:00.351Z" },
SK: { S: "SEARCH#FN846b958fe0c70b9c6c3e0388a2b004eb" },
flowSid: { S: "FWd7b6ceaf8004f55275f8dc00c16589d2" },
PK: {
S: "CALL#com.twilio.studio.flow#FN846b958fe0c70b9c6c3e0388a2b004eb",
},
time: { S: "2024-07-25T13:24:00.351Z" },
id: { S: "CAf0473b17886439dc380356419cde4818" },
toNumber: { S: "15108043852" },
direction: { S: "inbound" },
status: { S: "Complete" },
},
],
Count: 1,
ScannedCount: 1,
LastEvaluatedKey: {
SK: { S: "SEARCH#FN846b958fe0c70b9c6c3e0388a2b004eb" },
PK: {
S: "CALL#com.twilio.studio.flow#FN846b958fe0c70b9c6c3e0388a2b004eb",
},
GSI1PK: { S: "SEARCH_DATE#com.twilio.studio.flow" },
GSI1SK: { S: "2024-07-25T13:24:00.351Z" },
},
};
const expectedOutput = {
data: [
{
reason: "FLOW_END",
GSI4PK: "SEARCH_ID#com.twilio.studio.flow",
GSI3PK: "SEARCH_TO#com.twilio.studio.flow",
GSI2PK: "SEARCH_FROM#com.twilio.studio.flow",
fromNumber: "18886602449",
duration: 0.026,
executionId: "FN846b958fe0c70b9c6c3e0388a2b004eb",
GSI1PK: "SEARCH_DATE#com.twilio.studio.flow",
GSI4SK: "CAf0473b17886439dc380356419cde4818",
GSI3SK: "15108043852",
GSI2SK: "18886602449",
GSI1SK: "2024-07-25T13:24:00.351Z",
SK: "SEARCH#FN846b958fe0c70b9c6c3e0388a2b004eb",
flowSid: "FWd7b6ceaf8004f55275f8dc00c16589d2",
PK: "CALL#com.twilio.studio.flow#FN846b958fe0c70b9c6c3e0388a2b004eb",
time: "2024-07-25T13:24:00.351Z",
id: "CAf0473b17886439dc380356419cde4818",
toNumber: "15108043852",
direction: "inbound",
status: "Complete",
},
],
count: 1,
scannedCount: 1,
lastEvaluatedKeyEncoded: undefined,
lastEvaluatedKey:
"eyJTSyI6eyJTIjoiU0VBUkNII0ZOODQ2Yjk1OGZlMGM3MGI5YzZjM2UwMzg4YTJiMDA0ZWIifSwiUEsiOnsiUyI6IkNBTEwjY29tLnR3aWxpby5zdHVkaW8uZmxvdyNGTjg0NmI5NThmZTBjNzBiOWM2YzNlMDM4OGEyYjAwNGViIn0sIkdTSTFQSyI6eyJTIjoiU0VBUkNIX0RBVEUjY29tLnR3aWxpby5zdHVkaW8uZmxvdyJ9LCJHU0kxU0siOnsiUyI6IjIwMjQtMDctMjVUMTM6MjQ6MDAuMzUxWiJ9fQ==",
};
mockDynamoDB.query().promise.mockResolvedValue(mockResponse);
const [error, response] = await callLogSearch.getByDate(
"2023-08-22",
"2023-08-23"
);
console.log("ress : ", response);
expect(error).toBeNull();
expect(response.count).toBe(expectedOutput.count);
expect(response.scannedCount).toBe(expectedOutput.scannedCount);
expect(response.data).toEqual(expectedOutput.data);
});
const { setGlobalVars } = require("./global-vars");
const responses = require("./api_responses");
const { CallLogSearch } = require("./data/call-log-search");
//"type": "com.twilio.studio.flow",
// https://stackoverflow.com/questions/56731369/jest-typeerror-require-is-not-a-function
const logCat = "CallLogSearch";
module.exports.handler = async (event) => {
const logSubCat = `${logCat}-handler`;
setGlobalVars();
Logger.info({
logCat, logSubCat, message: 'Event data', data: event?.body,
});
Logger.info({
logCat, logSubCat, message: 'Headers data', data: event.headers,
});
try {
const body = JSON.parse(event.body);
const type = body?.type || null;
const id = body?.id || null;
const startDate = body?.start_date || null;
const endDate = body?.end_date || null;
const fromNumber = body?.from_number || null;
const toNumber = body?.to_number || null;
const direction = body?.direction || null;
const status = body?.status || null;
const size = body?.size || null;
const page = body?.page || null;
if (!type) {
return responses._400({ error: "Type of search is required" });
}
const search = new CallLogSearch(type);
let error;
let result = [];
if (id) {
[error, result] = await search.getById(id, startDate, endDate, size, page)
} else if ((fromNumber || toNumber)) {
[error, result] = await search.getByPhone(fromNumber, toNumber, startDate, endDate, size, page)
} else {
[error, result] = await search.getByDate(startDate, endDate, status, direction, size, page)
}
if (error) {
Logger.error({logCat, logSubCat, message: 'Search Retrieve Error', data: error});
return responses._400({ error });
}
Logger.info({ logCat, logSubCat, message: 'Search Retrieved', data: `${result.length} records.` });
return responses._200(result);
} catch (error) {
Logger.error({logCat, logSubCat, message: 'Search Error', data: error});
return responses._400({ error });
}
}
const { CallLogSearch } = require("../data/call-log-search");
const search = new CallLogSearch("com.twilio.studio.flow");
const getByDate = search.getByDate()
const {getClient} = require("../data/client");
jest.mock('../data/client', () => ({
getClient: jest.fn(),
}));
describe("getByDate function", () => {
const mockResponseData = {
data: [
{
reason: "FLOW_END",
GSI4PK: "SEARCH_ID#com.twilio.studio.flow",
GSI3PK: "SEARCH_TO#com.twilio.studio.flow",
GSI2PK: "SEARCH_FROM#com.twilio.studio.flow",
fromNumber: "18886602449",
duration: 0.026,
executionId: "FN846b958fe0c70b9c6c3e0388a2b004eb",
GSI1PK: "SEARCH_DATE#com.twilio.studio.flow",
GSI4SK: "CAf0473b17886439dc380356419cde4818",
GSI3SK: "15108043852",
GSI2SK: "18886602449",
GSI1SK: "2024-07-25T13:24:00.351Z",
SK: "SEARCH#FN846b958fe0c70b9c6c3e0388a2b004eb",
flowSid: "FWd7b6ceaf8004f55275f8dc00c16589d2",
PK: "CALL#com.twilio.studio.flow#FN846b958fe0c70b9c6c3e0388a2b004eb",
time: "2024-07-25T13:24:00.351Z",
id: "CAf0473b17886439dc380356419cde4818",
toNumber: "15108043852",
direction: "inbound",
status: "Complete"
}
],
count: 1,
scannedCount: 1,
lastEvaluatedKey: "eyJTSyI6eyJTIjoiU0VBUkNII0ZOODQ2Yjk1OGZlMGM3MGI5YzZjM2UwMzg4YTJiMDA0ZWIifSwiUEsiOnsiUyI6IkNBTEwjY29tLnR3aWxpby5zdHVkaW8uZmxvdyNGTjg0NmI5NThmZTBjNzBiOWM2YzNlMDM4OGEyYjAwNGViIn0sIkdTSTFQSyI6eyJTIjoiU0VBUkNIX0RBVEUjY29tLnR3aWxpby5zdHVkaW8uZmxvdyJ9LCJHU0kxU0siOnsiUyI6IjIwMjQtMDctMjVUMTM6MjQ6MDAuMzUxWiJ9fQ=="
};
let dynamoDbMock;
beforeEach(() => {
dynamoDbMock = {
query: jest.fn().mockReturnThis(),
promise: jest.fn()
};
getClient.mockReturnValue(dynamoDbMock);
});
it("should return a successful response with the correct data", async () => {
dynamoDbMock.promise.mockResolvedValue(mockResponseData);
const event = {
queryStringParameters: {
startDate: "2024-07-25",
endDate: "2024-07-25",
gsi1pk: "SEARCH_DATE#com.twilio.studio.flow",
ended_reason: "FLOW_END",
status: "Complete",
},
};
const response = await getByDate(event);
console.log("Successful Response:", JSON.parse(response.body).data);
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body).data).toEqual([
{
reason: "FLOW_END",
GSI4PK: "SEARCH_ID#com.twilio.studio.flow",
GSI3PK: "SEARCH_TO#com.twilio.studio.flow",
GSI2PK: "SEARCH_FROM#com.twilio.studio.flow",
fromNumber: "18886602449",
duration: 0.026,
executionId: "FN846b958fe0c70b9c6c3e0388a2b004eb",
GSI1PK: "SEARCH_DATE#com.twilio.studio.flow",
GSI4SK: "CAf0473b17886439dc380356419cde4818",
GSI3SK: "15108043852",
GSI2SK: "18886602449",
GSI1SK: "2024-07-25T13:24:00.351Z",
SK: "SEARCH#FN846b958fe0c70b9c6c3e0388a2b004eb",
flowSid: "FWd7b6ceaf8004f55275f8dc00c16589d2",
PK: "CALL#com.twilio.studio.flow#FN846b958fe0c70b9c6c3e0388a2b004eb",
time: "2024-07-25T13:24:00.351Z",
id: "CAf0473b17886439dc380356419cde4818",
toNumber: "15108043852",
direction: "inbound",
status: "Complete",
},
]);
});
it("should return an error if startDate is after endDate", async () => {
const event = {
queryStringParameters: {
startDate: "2024-07-26",
endDate: "2024-07-25",
gsi1pk: "SEARCH_DATE#com.twilio.studio.flow",
},
};
const response = await getByDate(event);
console.log("Error Response:", JSON.parse(response.body));
expect(response.statusCode).toBe(500);
expect(JSON.parse(response.body).message).toBe("Failed to get data.");
expect(JSON.parse(response.body).errorMsg).toBe(
"Start Date Must Be Before or Equal to End Date"
);
});
it("should return an error if gsi1pk is missing", async () => {
const event = {
queryStringParameters: {
startDate: "2024-07-25",
endDate: "2024-07-25",
},
};
const response = await getByDate(event);
console.log("Error Response:", JSON.parse(response.body));
expect(response.statusCode).toBe(500);
expect(JSON.parse(response.body).message).toBe("Failed to get data.");
expect(JSON.parse(response.body).errorMsg).toBe(
"GSI1PK parameter is required"
);
});
});
const { CallLogSearch } = require("../data/call-log-search");
const search = new CallLogSearch("com.twilio.studio.flow");
// const getByDate = search.getByDate()
const {getClient} = require("../data/client");
jest.mock('../data/client', () => ({
getClient: jest.fn(),
}));
describe("getByDate function", () => {
const mockResponseData = {
data: [
{
reason: "FLOW_END",
GSI4PK: "SEARCH_ID#com.twilio.studio.flow",
GSI3PK: "SEARCH_TO#com.twilio.studio.flow",
GSI2PK: "SEARCH_FROM#com.twilio.studio.flow",
fromNumber: "18886602449",
duration: 0.026,
executionId: "FN846b958fe0c70b9c6c3e0388a2b004eb",
GSI1PK: "SEARCH_DATE#com.twilio.studio.flow",
GSI4SK: "CAf0473b17886439dc380356419cde4818",
GSI3SK: "15108043852",
GSI2SK: "18886602449",
GSI1SK: "2024-07-25T13:24:00.351Z",
SK: "SEARCH#FN846b958fe0c70b9c6c3e0388a2b004eb",
flowSid: "FWd7b6ceaf8004f55275f8dc00c16589d2",
PK: "CALL#com.twilio.studio.flow#FN846b958fe0c70b9c6c3e0388a2b004eb",
time: "2024-07-25T13:24:00.351Z",
id: "CAf0473b17886439dc380356419cde4818",
toNumber: "15108043852",
direction: "inbound",
status: "Complete"
}
],
count: 1,
scannedCount: 1,
lastEvaluatedKey: "eyJTSyI6eyJTIjoiU0VBUkNII0ZOODQ2Yjk1OGZlMGM3MGI5YzZjM2UwMzg4YTJiMDA0ZWIifSwiUEsiOnsiUyI6IkNBTEwjY29tLnR3aWxpby5zdHVkaW8uZmxvdyNGTjg0NmI5NThmZTBjNzBiOWM2YzNlMDM4OGEyYjAwNGViIn0sIkdTSTFQSyI6eyJTIjoiU0VBUkNIX0RBVEUjY29tLnR3aWxpby5zdHVkaW8uZmxvdyJ9LCJHU0kxU0siOnsiUyI6IjIwMjQtMDctMjVUMTM6MjQ6MDAuMzUxWiJ9fQ=="
};
let dynamoDbMock;
beforeEach(() => {
dynamoDbMock = {
query: jest.fn().mockReturnThis(),
promise: jest.fn()
};
getClient.mockReturnValue(dynamoDbMock);
});
it("should return a successful response with the correct data", async () => {
dynamoDbMock.promise.mockResolvedValue(mockResponseData);
const event = {
queryStringParameters: {
startDate: "2024-07-25",
endDate: "2024-07-25",
gsi1pk: "SEARCH_DATE#com.twilio.studio.flow",
ended_reason: "FLOW_END",
status: "Complete",
},
};
const response = await search.getByDate(event)
console.log("Successful Response:", JSON.parse(response.body).data);
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body).data).toEqual([
{
reason: "FLOW_END",
GSI4PK: "SEARCH_ID#com.twilio.studio.flow",
GSI3PK: "SEARCH_TO#com.twilio.studio.flow",
GSI2PK: "SEARCH_FROM#com.twilio.studio.flow",
fromNumber: "18886602449",
duration: 0.026,
executionId: "FN846b958fe0c70b9c6c3e0388a2b004eb",
GSI1PK: "SEARCH_DATE#com.twilio.studio.flow",
GSI4SK: "CAf0473b17886439dc380356419cde4818",
GSI3SK: "15108043852",
GSI2SK: "18886602449",
GSI1SK: "2024-07-25T13:24:00.351Z",
SK: "SEARCH#FN846b958fe0c70b9c6c3e0388a2b004eb",
flowSid: "FWd7b6ceaf8004f55275f8dc00c16589d2",
PK: "CALL#com.twilio.studio.flow#FN846b958fe0c70b9c6c3e0388a2b004eb",
time: "2024-07-25T13:24:00.351Z",
id: "CAf0473b17886439dc380356419cde4818",
toNumber: "15108043852",
direction: "inbound",
status: "Complete",
},
]);
});
it("should return an error if startDate is after endDate", async () => {
const event = {
queryStringParameters: {
startDate: "2024-07-26",
endDate: "2024-07-25",
gsi1pk: "SEARCH_DATE#com.twilio.studio.flow",
},
};
const response = await getByDate(event);
console.log("Error Response:", JSON.parse(response.body));
expect(response.statusCode).toBe(500);
expect(JSON.parse(response.body).message).toBe("Failed to get data.");
expect(JSON.parse(response.body).errorMsg).toBe(
"Start Date Must Be Before or Equal to End Date"
);
});
it("should return an error if gsi1pk is missing", async () => {
const event = {
queryStringParameters: {
startDate: "2024-07-25",
endDate: "2024-07-25",
},
};
const response = await getByDate(event);
console.log("Error Response:", JSON.parse(response.body));
expect(response.statusCode).toBe(500);
expect(JSON.parse(response.body).message).toBe("Failed to get data.");
expect(JSON.parse(response.body).errorMsg).toBe(
"GSI1PK parameter is required"
);
});
});
const { CallLogSearch } = require("../data/call-log-search");
const search = new CallLogSearch("com.twilio.studio.flow");
// const getByDate = search.getByDate()
const {getClient} = require("../data/client");
jest.mock('../data/client', () => ({
getClient: jest.fn(),
}));
describe("getByDate function", () => {
const mockResponseData = {
data: [
{
reason: "FLOW_END",
GSI4PK: "SEARCH_ID#com.twilio.studio.flow",
GSI3PK: "SEARCH_TO#com.twilio.studio.flow",
GSI2PK: "SEARCH_FROM#com.twilio.studio.flow",
fromNumber: "18886602449",
duration: 0.026,
executionId: "FN846b958fe0c70b9c6c3e0388a2b004eb",
GSI1PK: "SEARCH_DATE#com.twilio.studio.flow",
GSI4SK: "CAf0473b17886439dc380356419cde4818",
GSI3SK: "15108043852",
GSI2SK: "18886602449",
GSI1SK: "2024-07-25T13:24:00.351Z",
SK: "SEARCH#FN846b958fe0c70b9c6c3e0388a2b004eb",
flowSid: "FWd7b6ceaf8004f55275f8dc00c16589d2",
PK: "CALL#com.twilio.studio.flow#FN846b958fe0c70b9c6c3e0388a2b004eb",
time: "2024-07-25T13:24:00.351Z",
id: "CAf0473b17886439dc380356419cde4818",
toNumber: "15108043852",
direction: "inbound",
status: "Complete"
}
],
count: 1,
scannedCount: 1,
lastEvaluatedKey: "eyJTSyI6eyJTIjoiU0VBUkNII0ZOODQ2Yjk1OGZlMGM3MGI5YzZjM2UwMzg4YTJiMDA0ZWIifSwiUEsiOnsiUyI6IkNBTEwjY29tLnR3aWxpby5zdHVkaW8uZmxvdyNGTjg0NmI5NThmZTBjNzBiOWM2YzNlMDM4OGEyYjAwNGViIn0sIkdTSTFQSyI6eyJTIjoiU0VBUkNIX0RBVEUjY29tLnR3aWxpby5zdHVkaW8uZmxvdyJ9LCJHU0kxU0siOnsiUyI6IjIwMjQtMDctMjVUMTM6MjQ6MDAuMzUxWiJ9fQ=="
};
let dynamoDbMock;
beforeEach(() => {
dynamoDbMock = {
query: jest.fn().mockReturnThis(),
promise: jest.fn()
};
getClient.mockReturnValue(dynamoDbMock);
});
it("should return a successful response with the correct data", async () => {
dynamoDbMock.promise.mockResolvedValue(mockResponseData);
dynamoDbMock.query.mockResolvedValue(mockResponseData);
const event = {
startDate: "2024-07-25",
endDate: "2024-07-25",
gsi1pk: "SEARCH_DATE#com.twilio.studio.flow",
ended_reason: "FLOW_END",
status: "Complete",
};
const response = await search.getByDate("2024-07-01","2024-07-25","Complete","inbound",1, null)
// console.log("Successful Response:", JSON.parse(response.body).data);
console.log("response ==>",response)
expect(response.statusCode).toBe(200);
// expect(JSON.parse(response.body).data).toEqual([
// {
// reason: "FLOW_END",
// GSI4PK: "SEARCH_ID#com.twilio.studio.flow",
// GSI3PK: "SEARCH_TO#com.twilio.studio.flow",
// GSI2PK: "SEARCH_FROM#com.twilio.studio.flow",
// fromNumber: "18886602449",
// duration: 0.026,
// executionId: "FN846b958fe0c70b9c6c3e0388a2b004eb",
// GSI1PK: "SEARCH_DATE#com.twilio.studio.flow",
// GSI4SK: "CAf0473b17886439dc380356419cde4818",
// GSI3SK: "15108043852",
// GSI2SK: "18886602449",
// GSI1SK: "2024-07-25T13:24:00.351Z",
// SK: "SEARCH#FN846b958fe0c70b9c6c3e0388a2b004eb",
// flowSid: "FWd7b6ceaf8004f55275f8dc00c16589d2",
// PK: "CALL#com.twilio.studio.flow#FN846b958fe0c70b9c6c3e0388a2b004eb",
// time: "2024-07-25T13:24:00.351Z",
// id: "CAf0473b17886439dc380356419cde4818",
// toNumber: "15108043852",
// direction: "inbound",
// status: "Complete",
// },
// ]);
});
it("should return an error if startDate is after endDate", async () => {
const event = {
queryStringParameters: {
startDate: "2024-07-26",
endDate: "2024-07-25",
gsi1pk: "SEARCH_DATE#com.twilio.studio.flow",
},
};
// const response = await getByDate(event);
const response = await search.getByDate("2024-07-26","2024-07-25","Complete","inbound",1, null)
console.log("Error Response:", JSON.parse(response.body));
expect(response.statusCode).toBe(500);
expect(JSON.parse(response.body).message).toBe("Failed to get data.");
expect(JSON.parse(response.body).errorMsg).toBe(
"Start Date Must Be Before or Equal to End Date"
);
});
});
const AWS = require('aws-sdk');
// const { CallLogSearch } = require("../clientsidecode/temp");
const { CallLogSearch } = require("../data/call-log-search");
// const { getClient } = require("../client");
jest.mock('aws-sdk', () => {
const mDocumentClient = {
query: jest.fn().mockReturnThis(),
promise: jest.fn(),
};
return {
DynamoDB: {
DocumentClient: jest.fn(() => mDocumentClient),
},
};
});
describe('CallLogSearch', () => {
let callLogSearch;
let mockDynamoDB;
beforeEach(() => {
callLogSearch = new CallLogSearch(
'com.twilio.studio.flow',
'flowSid',
'executionId',
'flowName',
'time',
'id',
'fromNumber',
'toNumber',
'direction',
'status',
123,
'reason',
'callUrl'
);
mockDynamoDB = new AWS.DynamoDB.DocumentClient();
});
afterEach(() => {
jest.clearAllMocks();
});
it('should return search results between startDate and endDate', async () => {
const mockItems = [
{ GSI1PK: { S: 'SEARCH_DATE#com.twilio.studio.flow' }, GSI1SK: { S: '2023-08-22T00:00:00Z' } },
{ GSI1PK: { S: 'SEARCH_DATE#com.twilio.studio.flow' }, GSI1SK: { S: '2023-08-23T00:00:00Z' } },
];
mockDynamoDB.query().promise.mockResolvedValue({
Items: mockItems,
Count: mockItems.length,
ScannedCount: mockItems.length,
});
const [error, response] = await callLogSearch.getByDate('2023-08-22', '2023-08-23');
expect(error).toBeNull();
expect(response.count).toBe(mockItems.length);
expect(response.scannedCount).toBe(mockItems.length);
// expect(response.data).toEqual(expect.arrayContaining([expect.objectContaining({
// GSI1PK: 'SEARCH_DATE#com.twilio.studio.flow',
// })]));
});
it('should return error if endDate is before startDate', async () => {
const [error, response] = await callLogSearch.getByDate('2023-08-27', '2023-08-22', "test");
expect(error).toBeInstanceOf(Error);
expect(error.message).toBe('Start Date Must to Be Before or Equal to End Date');
expect(response).toBeUndefined();
});
});
const AWS = require('aws-sdk');
const { CallLogSearch } = require("../clientsidecode/temp");
jest.mock('aws-sdk', () => {
const mDynamoDB = {
query: jest.fn().mockReturnThis(),
promise: jest.fn(),
};
return {
DynamoDB: jest.fn(() => mDynamoDB),
};
});
describe('CallLogSearch', () => {
let callLogSearch;
let mockDynamoDB;
beforeEach(() => {
callLogSearch = new CallLogSearch(
'type',
'flowSid',
'executionId',
'flowName',
'time',
'id',
'fromNumber',
'toNumber',
'direction',
'status',
123,
'reason',
'callUrl'
);
mockDynamoDB = new AWS.DynamoDB();
});
afterEach(() => {
jest.clearAllMocks();
});
it('should return search results between startDate and endDate', async () => {
const mockItems = [
{ GSI1PK: { S: 'SEARCH_DATE#com.twilio.studio.flow' }, GSI1SK: { S: '2023-08-22T00:00:00Z' } },
{ GSI1PK: { S: 'SEARCH_DATE#com.twilio.studio.flow' }, GSI1SK: { S: '2023-08-23T00:00:00Z' } },
];
mockDynamoDB.query().promise.mockResolvedValue({
Items: mockItems,
Count: mockItems.length,
ScannedCount: mockItems.length,
});
const [error, response] = await callLogSearch.getByDate('2023-08-22', '2023-08-23');
expect(error).toBeNull();
expect(response.count).toBe(mockItems.length);
expect(response.scannedCount).toBe(mockItems.length);
// expect(response.data).toEqual(expect.arrayContaining([expect.objectContaining({
// GSI1PK: 'SEARCH_DATE#com.twilio.studio.flow',
// })]));
});
it('should return error if endDate is before startDate', async () => {
const [error, response] = await callLogSearch.getByDate('2023-08-23', '2023-08-22');
expect(error).toBeInstanceOf(Error);
expect(error.message).toBe('Start Date Must to Be Before or Equal to End Date');
expect(response).toBeUndefined();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment