Skip to content

Commit 47d14d6

Browse files
authored
Merge pull request #15 from Nummenpojat/status-messages
Logs
2 parents a43301a + 130409e commit 47d14d6

File tree

15 files changed

+66
-39
lines changed

15 files changed

+66
-39
lines changed

app/.env

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ COPY package*.json ./
1111
RUN npm install
1212

1313
# Copy the rest of the application code to the container
14-
COPY ../suppis-nettisivut .
14+
COPY . .
1515

1616
# Build the React app
1717
RUN npm run build

app/src/pages/message.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default function MessageWrapper() {
3636
.catch(reason => handleMessageApiCallResponse(reason))
3737
}
3838

39-
alert(error.response.data)
39+
alert(`Ongelma ilmeentyi: ${error.response.data}`)
4040
}
4141

4242
const handleCsvToJson = (): Promise<any> => {
@@ -62,14 +62,24 @@ export default function MessageWrapper() {
6262

6363
const handleSend = async (event: any) => {
6464
event.preventDefault()
65+
66+
if (message == "") {
67+
alert("Sinun pitää antaa viesti")
68+
return
69+
}
70+
71+
if (!confirm(`Haluatko lähettää viestin:\n\n${message}\n\nHuomaathan että viestin lähettämisessä kestää hetki joten älä ihmettele jos näyttä siltä että mitään ei tapahdu`)) {
72+
return
73+
}
74+
6575
if (!toList) {
6676
try {
6777
const result: AxiosResponse = await core.post("/api/whatsapp/send/one", {
6878
number: phoneNumber,
6979
message: message
7080
})
7181

72-
alert(result.data)
82+
alert(`Viestin lähettäminen onnistui.`)
7383

7484
} catch (error: any) {
7585
handleMessageApiCallResponse(error)
@@ -84,7 +94,7 @@ export default function MessageWrapper() {
8494
message: message
8595
})
8696

87-
alert(result.data)
97+
alert("Viestien lähettäminen onnistui")
8898
} catch (error: any) {
8999
handleMessageApiCallResponse(error)
90100
}

core/.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
PORT= # Port number
22
FIREBASE_SECRET_KEY_PATH= # File path to Firebase service key. Example: "/config/firebase-admin-secrets/secret-key.json"
3-
WHATSAPP_SESSION_PATH= # File path where whatsapp-web.js stores session
3+
WHATSAPP_SESSION_PATH= # File path where whatsapp-web.js stores session
4+
LOG_GROUP_ID= # Whatsapp group that works as log storage

core/.gitignore

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
# Local configs
2-
config/whatsapp/*
3-
../suppis-core/.idea
4-
config/firebase-admin-secrets/*
1+
firebase.json
2+
whatsapp
53

64
# Logs
75
logs
@@ -74,6 +72,7 @@ typings/
7472
.yarn-integrity
7573

7674
# dotenv environment variables file
75+
../.env
7776
.env
7877
.env.test
7978

core/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ RUN \
1414
apt-get install -y google-chrome-stable && \
1515
rm -rf /var/lib/apt/lists/*s
1616

17-
COPY ../suppis-core ./
17+
COPY . .
1818

1919
RUN npm run build
2020

core/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ services:
66
restart: always
77
environment:
88
PORT: 3001
9-
FIREBASE_SECRET_KEY_PATH: /app/config/firebase-admin-secrets/suppis-firebase-admin-secrets.json
9+
FIREBASE_SECRET_KEY_PATH: /app/config/firebase-admin-secrets/firebase.json
1010
WHATSAPP_SESSION_PATH: /app/config/whatsapp
1111
volumes:
1212
- ./config/whatsapp:/app/config/whatsapp

core/src/auth/getCurrentUser.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {getAuth} from "firebase-admin/auth";
2+
3+
const getCurrentUser = async (idToken: string) => {
4+
try {
5+
// Verifies ID token to ensure correct access right to API
6+
const result = await getAuth().verifyIdToken(idToken)
7+
8+
// Gets user to check custom claims
9+
return await getAuth().getUser(result.uid)
10+
} catch (error) {
11+
console.log(`Error occurred when getting current user: ${error}`)
12+
}
13+
}
14+
15+
export default getCurrentUser;

core/src/modules/whatsapp/commands/sendMessage.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import {checkNumbers, client} from "../main";
1+
import {checkNumbers, client, logMessage} from "../main";
2+
import getCurrentUser from "../../../auth/getCurrentUser";
23

3-
/**
4-
* Send message with Whatsapp to single number
5-
* @param phoneNumber Number that message is sent
6-
* @param message Text that is sent to phoneNumber provided
7-
**/
8-
export const sendMessage = async (phoneNumber: string, message: string) => {
4+
export const sendMessage = async (phoneNumber: string, message: string, senderIdToken: string) => {
95

106
// Checking that phone number is not empty
117
if (phoneNumber == "" || phoneNumber == null) {
@@ -17,7 +13,6 @@ export const sendMessage = async (phoneNumber: string, message: string) => {
1713
throw "You need to provide message to send"
1814
}
1915

20-
2116
// Removing + at the start if it exits so the phone number is in right format
2217
if (phoneNumber.startsWith('+')) {
2318
phoneNumber = phoneNumber.substring(1)
@@ -33,6 +28,8 @@ export const sendMessage = async (phoneNumber: string, message: string) => {
3328
// Sending message to chosen chat
3429
const returnMessage = await client.sendMessage(chatId, message)
3530

31+
const user = await getCurrentUser(senderIdToken)
32+
logMessage(`${user?.email || "Unknown"} sent message:\n\n${returnMessage.body}`)
3633
console.log(`Message ${returnMessage.body} sent`);
3734
return `Message ${returnMessage.body} sent`
3835

core/src/modules/whatsapp/commands/sendMessageToList.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import {checkNumbers, client} from "../main";
1+
import {checkNumbers, client, logMessage} from "../main";
2+
import getCurrentUser from "../../../auth/getCurrentUser";
23

3-
/**
4-
* Send same message to list of people
5-
* @param message Text that is sent to list provided
6-
* @param persons Array that contains numbers of people to send the message
7-
* */
8-
export const sendMessageToList = async (message: string, numbers: string[]) => {
4+
export const sendMessageToList = async (message: string, numbers: string[], senderIdToken: string) => {
95

106
// Checking that message is not empty
117
if (message == "" || message == null) {
@@ -42,6 +38,8 @@ export const sendMessageToList = async (message: string, numbers: string[]) => {
4238
throw error
4339
}
4440
}
41+
const user = await getCurrentUser(senderIdToken)
42+
logMessage(`${user?.email || "Unknown"} sent message to ${numbers.length} people:\n\n${message}`)
4543
console.log(`Message ${message} sent to list of people`);
4644
return `Message ${message} sent to list of people`
4745
}

0 commit comments

Comments
 (0)