Skip to content

Commit

Permalink
Merge pull request #136 from flickmatch/hvs-flick-patch-1
Browse files Browse the repository at this point in the history
Add functionality to format message
  • Loading branch information
abhimanyu-fm authored Feb 7, 2024
2 parents d546bd1 + ed48bdd commit afd69a7
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/whatsapp-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
port: 22
script: |
cd ~/flickmatch/home/whatsapp-js
git stash
git pull origin main
git status
~/.nvm/versions/node/v18.15.0/bin/pm2 restart main
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.flickmatch.platform.records.WhatsAppNotification;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand All @@ -18,10 +19,11 @@ public class WhatsAppProxy {
// Create a RestTemplate
@Autowired
private RestTemplate restTemplate;
@Value("${whatsapp.endpoint}")
private String nodeServerUrl;
private final ObjectMapper objectMapper = new ObjectMapper();

public void sendNotification(WhatsAppNotification eventDataForNotification) {
String nodeServerUrl = "http://ec2-18-223-205-234.us-east-2.compute.amazonaws.com:3000/";

// Create headers
HttpHeaders headers = new HttpHeaders();
Expand Down
4 changes: 3 additions & 1 deletion platform/src/main/resources/application-beta.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ amazon.aws.profile=beta

phonepe.merchant.id=M1FPXNBFIGDG
phonepe.saltkey=e7a5693b-0ccf-47ac-a258-c5d77e701e2c
phonepe.saltkey.index=2
phonepe.saltkey.index=2

whatsapp.endpoint=http://localhost:3000/notification
4 changes: 3 additions & 1 deletion platform/src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ amazon.aws.profile=dev

phonepe.merchant.id=test
phonepe.saltkey=test
phonepe.saltkey.index=1
phonepe.saltkey.index=1

whatsapp.endpoint=http://localhost:3000/notification
2 changes: 2 additions & 0 deletions platform/src/main/resources/application-prod.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ amazon.aws.profile=prod
phonepe.merchant.id=M1FPXNBFIGDG
phonepe.saltkey=e7a5693b-0ccf-47ac-a258-c5d77e701e2c
phonepe.saltkey.index=2

whatsapp.endpoint=http://ec2-18-223-205-234.us-east-2.compute.amazonaws.com:3000/notification
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.flickmatch.platform.proxy;

import com.flickmatch.platform.records.WhatsAppNotification;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.ResponseEntity;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.RestTemplate;

import static org.mockito.ArgumentMatchers.any;
Expand All @@ -21,6 +23,11 @@ class WhatsAppProxyTest {

@InjectMocks WhatsAppProxy whatsAppProxy;

@BeforeEach
void setUp() {
ReflectionTestUtils.setField(whatsAppProxy, "nodeServerUrl", "");
}

@Test
public void test_WhenCallIsSuccessful() {
Mockito.when(restTemplate.postForEntity(anyString(), any(), any())).thenReturn(ResponseEntity.accepted().build());
Expand Down
2 changes: 2 additions & 0 deletions platform/src/test/resources/application-test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ application.local.endpoint=http://localhost:8080/graphql
phonepe.merchant.id=test
phonepe.saltkey=test
phonepe.saltkey.index=1

whatsapp.endpoint=http://localhost:3000/notification
14 changes: 6 additions & 8 deletions whatsapp-js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import qrcode from "qrcode-terminal";
import pkg from "whatsapp-web.js";
const { Client, LocalAuth } = pkg;
import { processGroup } from "./src/inputParser.js";
import { createMessage } from "./src/notificationRequestParser.js";

export const isProd =
process.platform !== "win32" && process.platform !== "darwin";
Expand All @@ -12,19 +13,16 @@ export const groupName = isProd ? "Online Queue Update" : "Test group ";

import express from "express";
const app = express();
app.use(express.json());
const port = 3000;
const ggnSouthCityChatId = "[email protected]";
const hydChatId = "[email protected]";

app.post("/", (req, res) => {
app.post("/notification", (req, res) => {
const testGroupChatId = "[email protected]";
client.sendMessage(testGroupChatId, "Post Endpoint hit");
res.send("Hello World!");
});

app.get("/", (req, res) => {
const testGroupChatId = "[email protected]";
client.sendMessage(testGroupChatId, "Get Endpoint hit");
const jsonData = req.body;
//console.log('Received JSON data:', JSON.stringify(jsonData));
client.sendMessage(testGroupChatId, createMessage(jsonData));
res.send("Hello World!");
});

Expand Down
2 changes: 1 addition & 1 deletion whatsapp-js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion whatsapp-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"express": "^4.18.2",
"isomorphic-fetch": "^3.0.0",
"qrcode-terminal": "^0.12.0",
"whatsapp-web.js": "^1.19.5"
"whatsapp-web.js": "^1.23.0"
}
}
52 changes: 52 additions & 0 deletions whatsapp-js/src/notificationRequestParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

// Function to convert timestamps to timeslot
function convertToTimeSlot(timestamp1, timestamp2) {
// Convert timestamps to Date objects
const date1 = new Date(timestamp1);
const date2 = new Date(timestamp2);

// Extract hours and minutes from the Date objects
const hours1 = date1.getHours();
const minutes1 = date1.getMinutes();
const date = date1.getDate();
const month = date1.getMonth();
const year = date1.getFullYear();
const fullDate = `${date}-${month}-${year}`
const day = date1.getDay();

const hours2 = date2.getHours();
const minutes2 = date2.getMinutes();

// Format the timeslots
const timeSlot1 = `${padZero(hours1)}:${padZero(minutes1)}`;
const timeSlot2 = `${padZero(hours2)}:${padZero(minutes2)}`;

return `${timeSlot1} - ${timeSlot2} ${fullDate} (${daysOfWeek[day]})`;
}

// Function to pad zero for single-digit hours/minutes
function padZero(value) {
return value < 10 ? `0${value}` : `${value}`;
}

export const createMessage = (jsonData) => {
//const jsonDataObject = JSON.parse(jsonData);
const venueName = jsonData.venueName;
const mapLink = jsonData.venueLocationLink;
const format = jsonData.reservedPlayersCount/2;
const charges = jsonData.charges;
const dateTime = convertToTimeSlot(jsonData.startTime, jsonData.endTime);

var message = `${venueName} (${mapLink})
${dateTime}
${format}v${format}
${charges}/- Per Person.
Confirmed Players:`;
for (const [index, value] of jsonData.playerNameList.entries()) {
message = message + '\n' + `${index+1}. ${value}`;
}
message = message + '\n\nPlease pay at play.flickmatch.in to confirm your spot.';
return message;
}
22 changes: 22 additions & 0 deletions whatsapp-js/test/notificationRequestParserTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createMessage } from "../src/notificationRequestParser.js";

const jsonData = '{"startTime":1699715640000,"endTime":1699719240000,"venueName":"HotFut GachiBowli","reservedPlayersCount":10,"charges":200,"venueLocationLink":"https://goo.gl/maps/1QzakRkJPHxT8GjRA","localTimeZone":"GMT+5:30","playerNameList":["Player1","Player2","sam","nooo"]}';
const expected = `HotFut GachiBowli (https://goo.gl/maps/1QzakRkJPHxT8GjRA)
20:44 - 21:44 11-10-2023 (Saturday)
5v5
₹200/- Per Person.
Confirmed Players:
1. Player1
2. Player2
3. sam
4. nooo
Please pay at play.flickmatch.in to confirm your spot.`;

function assertEqual(actual, expected, label) {
const areEqual = JSON.stringify(actual) === JSON.stringify(expected);
console.assert(areEqual, `Assertion failed for ${label}:`, actual);
};

assertEqual(createMessage(JSON.parse(jsonData)), expected, 'createMessage test');

0 comments on commit afd69a7

Please sign in to comment.