Skip to content

Commit 6eefab8

Browse files
authored
Merge pull request BloomTech-Labs#144 from Lambda-School-Labs/messageEndpoints
Commit for push
2 parents f627c72 + d02b629 commit 6eefab8

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

ReadMe.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ Deployed [here](https://intense-stream-29923.herokuapp.com/api/database/seekers)
107107
- [auth](#auth)
108108
- [companies](#companies)
109109
- [constants](#constants)
110+
- [conversations](#conversations)
110111
- [firebase](#firebase)
111112
- [functions](#functions)
112113
- [markers](#markers)
@@ -260,6 +261,11 @@ Contains the server/backend of the project.
260261

261262
**Files:** frontend.js, stripe.js
262263

264+
- ### conversations
265+
The endpoints used to retrieve and manipulate data associated with conversations in the database.
266+
267+
**Files:** conversationsRouters.js
268+
263269
- ### firebase
264270
The configuration files for the client-side firebase integration.
265271

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const express = require('express');
2+
const firebase = require('../firebase/firebase.js');
3+
const rootRef = firebase.database().ref();
4+
const router = express.Router();
5+
6+
7+
//--------------------------------------------------------POSTS
8+
9+
router.post(
10+
'/addMessage',
11+
(req, res) => {
12+
13+
const messageKey = rootRef.push(null).key;
14+
15+
// Deconstruct Request Body
16+
const {
17+
uid, // User ID (Sender)
18+
pid, // Participant ID (Receiver)
19+
message,
20+
name,
21+
participant,
22+
date
23+
} = req.body;
24+
25+
cid = [uid, pid].sort().join('_'); // Conversation ID - Combination of Sender's and Receiver's UIDs.
26+
27+
// Check for Missing Required Information
28+
if (!pid || !uid || !message || !name || !participant || !date) {
29+
return res.status(400).json({"errorMessage":"Please send a message, date, and the names and ids of both participants."})
30+
}
31+
32+
// Construct New Message Object
33+
let newMessage = {
34+
date,
35+
message,
36+
name,
37+
}
38+
39+
// Construct Object to Add to User's Conversation Lookup (Sender)
40+
let newUserLookup = {
41+
lastMessage: newMessage,
42+
participant: {
43+
name: participant,
44+
uid: pid
45+
}
46+
}
47+
48+
// Construct Object to Add to Participant's Conversation Lookup (Receiver)
49+
let newParticipantLookup = {
50+
lastMessage: newMessage,
51+
participant: {
52+
name,
53+
uid
54+
}
55+
}
56+
57+
// Create Object to Update Firebase
58+
let updateObject = {};
59+
60+
// Set Up New Message to be Added to Conversations and Conversation Lookup
61+
updateObject[`conversations/${cid}/${messageKey}`] = newMessage;
62+
updateObject[`conversationLookup/${uid}/${cid}`] = newUserLookup; // Lookup for Sender
63+
updateObject[`conversationLookup/${pid}/${cid}`] = newParticipantLookup; // Lookup for Receiver
64+
65+
66+
// Update database with the new object
67+
rootRef.update(updateObject).catch(error => console.log(error));
68+
69+
// Success Message
70+
res.status(201).json({
71+
success: `Message has been sent.`,
72+
});
73+
}
74+
);
75+
76+
module.exports = router;

server/server.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const corsOptions = {
1414
const seekersRouters = require('./seekers/seekersRouters.js');
1515
const companiesRouters = require('./companies/companiesRouters.js');
1616
const markersRouters = require('./markers/markersRouters.js');
17-
1817
const favoritesRouters = require('./seekers/favoritesRoutes.js');
18+
const conversationsRouters = require('./conversations/conversationsRouters.js');
1919

2020
// const configureServer = require('./serverConfig');
2121
const configureRoutes = require('./stripe-routes');
@@ -27,9 +27,8 @@ server.use(express.json(), helmet());
2727
server.use('/api/database/seekers', seekersRouters);
2828
server.use('/api/database/companies', companiesRouters);
2929
server.use('/api/markers', markersRouters);
30-
31-
3230
server.use('/api/database/favorites', favoritesRouters);
31+
server.use('/api/database/conversations', conversationsRouters)
3332

3433
server.get('/', (req, res) => {
3534
res.status(200).send('Developer Map API. Currently In Development.');

0 commit comments

Comments
 (0)