Skip to content

Commit

Permalink
feat(announcements): filter by app name
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao authored Jan 8, 2023
1 parent 3bb98bd commit ab592ef
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/classes/Announcements.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ Announcements.prototype.findById = function (id) {
return found;
};

Announcements.prototype.findByTarget = function (appTarget) {
const found = this.list.filter((announcement) => announcement.appTarget === appTarget) || [];
return found;
};

Announcements.prototype.delete = async function (id) {
const announcementRef = db.collection('announcements').doc(id);
const announcementSnap = await announcementRef.get();
Expand Down
16 changes: 15 additions & 1 deletion src/controllers/users-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,21 @@ export const createAccount = async (req, res, next) => {

export const getAnnouncements = async (req, res, next) => {
try {
const list = announcements.list;
const { app } = req.headers;

const appsAllowed = ['lmm-oa', 'sws-vip', 'sws-pocket'];
if (appsAllowed.includes(app) === false) {
res.locals.type = 'warn';
res.locals.message = `invalid app`;

res.status(400).json({
message: 'Bad request: provided inputs are invalid.',
});

return;
}

const list = announcements.findByTarget(app);

res.locals.type = 'info';
res.locals.message = `client fetched announcements`;
Expand Down
30 changes: 18 additions & 12 deletions src/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@ import express from 'express';
import { body, header } from 'express-validator';
import { visitorChecker } from '../middleware/visitor-checker.js';
import {
createAccount,
deleteUserSession,
getAnnouncements,
getUserSecretToken,
getUserSessions,
resendVerificationEmail,
updateUserFullname,
updateUserPassword,
userLogout,
validateUser,
createAccount,
deleteUserSession,
getAnnouncements,
getUserSecretToken,
getUserSessions,
resendVerificationEmail,
updateUserFullname,
updateUserPassword,
userLogout,
validateUser,
} from '../controllers/users-controller.js';

const router = express.Router();

// create a new user account
router.post('/create-account', body('fullname').isLength({ min: 3 }), body('email').isEmail(), body('password').isLength({ min: 10 }), createAccount);
router.post(
'/create-account',
body('fullname').isLength({ min: 3 }),
body('email').isEmail(),
body('password').isLength({ min: 10 }),
createAccount
);

// get announcements
router.get('/announcement', getAnnouncements);
router.get('/announcement', header('app').isString().notEmpty(), getAnnouncements);

// resend verification email
router.get('/resend-verification', header('email').isEmail(), resendVerificationEmail);
Expand Down

0 comments on commit ab592ef

Please sign in to comment.