0
\$\begingroup\$

I am refactoring an old project, made in Nodejs with Express, applying the MVC pattern, today it is a fairly simple project: handling of the FCM and Remote Config tools from Firebase.

Would it be helpful, or is it good practice, to add the services file to work together with the controller?

The FCM part would look like this:

Model - FCMMessage.js

class FCMMessage {
 constructor(title, body, topic) {
 this.title = title;
 this.body = body;
 this.topic = topic;
 }
};
module.exports = {FCMMessage};

Controller - messagingController.js

var FCMMessage = require("../models/FCMMessage");
var messagingService = require("../services/messagingService");
const messagingView = (req, res) => {
 res.render('messaging');
};
const sendMessageToTopic = (req, res) => {
 const {title, body} = req.body;
 // TODO: Remove hard code topic
 const topic = "all_users";
 const message = new FCMMessage(title, body, topic);
 messagingService
 .sendMessageToTopic(message)
 .then((response) => {
 console.log("Successfully sent message to topic:", response);
 res.status(200).json({message: 'Message sent successfully'});
 })
 .catch((error) => {
 res.status(500).json({error: 'Failed to send message'});
 })
};
//...
module.exports = {messagingView, sendMessageToTopic};

Service - messagingService.js

var admin = require("firebase-admin");
var serviceAccount = require("../../placeholders/service-account.json");
admin.initializeApp({credential: admin.credential.cert(serviceAccount)});
const sendMessageToTopic = ({title, body, topic}) => {
 const message = {
 notification: {
 title,
 body,
 },
 topic,
 };
 return admin.messaging().send(message);
}
//...
module.exports = {sendMessageToTopic};

More route for the view and the posts router method:

var express = require("express")
var messagingController = require("../controllers/messagingController");
var router = express.Router();
router.get("/messaging", messagingController.messagingView);
router.post("/send-message-topic", messagingController.sendMessageToTopic);
//...
module.exports = router;

View - messaging.pug

doctype html
html
 head
 meta(charset='utf-8')
 title Messaging 
 link(rel="stylesheet", href="/stylesheets/style.css")
 link(rel="stylesheet", href="https://fonts.googleapis.com/css?family=Montserrat:100,200,300,400")
 body
 form(id="msform" role="form" action="/send-message-topic" method="POST")
 fieldset
 h2.fs-title Messaging
 h3.fs-subtitle Manage messaging campaigns with the new unified campaign experience.
 input(type="text" name="title" id="title" placeholder="Type your title" required)
 textarea(type="text" name="body" id="message" cols="30", rows="10" placeholder="Type your message" required)
 input.submit(type="submit", name="submit", placeholder="Submit")
 br 
 a.router(href="/translations") To remote config, manage translations.
asked Oct 25, 2023 at 9:38
\$\endgroup\$

1 Answer 1

-1
\$\begingroup\$

Finally fewer files, fewer places to maintain. I removed services folder and move Firebase admin sdk to the controller.

answered Oct 26, 2023 at 17:10
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.