Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newell: Hotfix update title feature #1170

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c5e7865
Revert "Revert "Revert "Revert "Backend Release to Main [1.92]""""
one-community Aug 15, 2024
332caed
Merge pull request #1069 from OneCommunityGlobal/revert-1068-revert-1…
one-community Aug 15, 2024
f966f8a
Revert "Revert "Revert "Revert "Revert "Backend Release to Main [1.92…
one-community Aug 15, 2024
c5c16d9
Merge pull request #1070 from OneCommunityGlobal/revert-1069-revert-1…
one-community Aug 15, 2024
74f1ae8
Merge pull request #1073 from OneCommunityGlobal/development
one-community Aug 15, 2024
c6f2eeb
Merge pull request #1074 from OneCommunityGlobal/development
one-community Aug 17, 2024
f1a7654
Merge pull request #1079 from OneCommunityGlobal/development
one-community Aug 20, 2024
d8352bb
Merge pull request #1086 from OneCommunityGlobal/development
one-community Aug 27, 2024
d15e4c4
Merge pull request #1090 from OneCommunityGlobal/development
one-community Sep 2, 2024
9d42d12
Merge pull request #1099 from OneCommunityGlobal/development
one-community Sep 12, 2024
4320629
Merge pull request #1109 from OneCommunityGlobal/development
one-community Sep 18, 2024
1afa0bd
Merge pull request #1121 from OneCommunityGlobal/development
one-community Oct 1, 2024
d76c0b3
Merge pull request #1134 from OneCommunityGlobal/development
one-community Oct 16, 2024
fda8fc9
Merge pull request #1137 from OneCommunityGlobal/development
one-community Oct 25, 2024
6d3af9a
Merge pull request #1147 from OneCommunityGlobal/development
one-community Nov 13, 2024
9088646
Add or update the Azure App Service build and deployment workflow config
one-community Nov 15, 2024
8768a6f
Explicitly disable tests during Azure CI
one-community Nov 15, 2024
ec76e9b
Merge pull request #1163 from OneCommunityGlobal/development
one-community Dec 4, 2024
f094262
Merge pull request #1168 from OneCommunityGlobal/development
one-community Dec 7, 2024
430e30f
fix: update title in title controller.
Ankuriboh Dec 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions .github/workflows/main_hgn-staging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy
# More GitHub Actions for Azure: https://github.com/Azure/actions

name: Build and deploy Node.js app to Azure Web App - hgn-staging

on:
push:
branches:
- main
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Node.js version
uses: actions/setup-node@v3
with:
node-version: '18.x'

- name: npm install, build, and test
run: |
npm install
npm run build --if-present

- name: Zip artifact for deployment
run: zip release.zip ./* -r

- name: Upload artifact for deployment job
uses: actions/upload-artifact@v4
with:
name: node-app
path: release.zip

deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'Production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

steps:
- name: Download artifact from build job
uses: actions/download-artifact@v4
with:
name: node-app

- name: Unzip artifact for deployment
run: unzip release.zip

- name: 'Deploy to Azure Web App'
id: deploy-to-webapp
uses: azure/webapps-deploy@v3
with:
app-name: 'hgn-staging'
slot-name: 'Production'
package: .
publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_D36DFF5BF4724498838A50C2C0D25D43 }}
248 changes: 162 additions & 86 deletions src/controllers/titleController.js
Original file line number Diff line number Diff line change
@@ -1,139 +1,215 @@
const Team = require('../models/team');
const Project = require('../models/project');
const cacheClosure = require('../utilities/nodeCache');
const { getAllTeamCodeHelper } = require("./userProfileController");
const userProfileController = require("./userProfileController");
const userProfile = require('../models/userProfile');
const project = require('../models/project');

const controller = userProfileController(userProfile, project);
const { getAllTeamCodeHelper } = controller;

const titlecontroller = function (Title) {
const cache = cacheClosure();

const getAllTitles = function (req, res) {
Title.find({})
.then((results) => res.status(200).send(results))
.catch((error) => res.status(404).send(error));
// Update: Confirmed with Jae. Team code is not related to the Team data model. But the team code field within the UserProfile data model.
async function checkTeamCodeExists(teamCode) {
try {
if (cache.getCache('teamCodes')) {
const teamCodes = JSON.parse(cache.getCache('teamCodes'));
return teamCodes.includes(teamCode);
}
const teamCodes = await getAllTeamCodeHelper();
return teamCodes.includes(teamCode);
} catch (error) {
console.error('Error checking if team code exists:', error);
throw error;
}
}

async function checkProjectExists(projectID) {
try {
const proj = await Project.findOne({ _id: projectID }).exec();
return !!proj;
} catch (error) {
console.error('Error checking if project exists:', error);
throw error;
}
}

const getAllTitles = function (req, res) {
Title.find({})
.then((results) => res.status(200).send(results))
.catch((error) => res.status(404).send(error));
};

const getTitleById = function (req, res) {
const { titleId } = req.params;
const getTitleById = function (req, res) {
const { titleId } = req.params;

Title.findById(titleId)
.then((results) => res.send(results))
.catch((error) => res.send(error));
};
Title.findById(titleId)
.then((results) => res.send(results))
.catch((error) => res.send(error));
};

const postTitle = async function (req, res) {
const title = new Title();
title.titleName = req.body.titleName;
title.titleCode = req.body.titleCode;
title.teamCode = req.body.teamCode;
title.projectAssigned = req.body.projectAssigned;
title.mediaFolder = req.body.mediaFolder;
title.teamAssiged = req.body.teamAssiged;

const titleCodeRegex = /^[A-Za-z]+$/;
if (!title.titleCode || !title.titleCode.trim()) {
return res.status(400).send({ message: 'Title code cannot be empty.' });
}

const postTitle = async function (req, res) {
const title = new Title();
if (!titleCodeRegex.test(title.titleCode)) {
return res.status(400).send({ message: 'Title Code must contain only upper or lower case letters.' });
}

// valid title name
if (!title.titleName.trim()) {
res.status(400).send({ message: 'Title cannot be empty.' });
return;
}

title.titleName = req.body.titleName;
title.teamCode = req.body.teamCode;
title.projectAssigned = req.body.projectAssigned;
title.mediaFolder = req.body.mediaFolder;
title.teamAssiged = req.body.teamAssiged;
// if media is empty
if (!title.mediaFolder.trim()) {
res.status(400).send({ message: 'Media folder cannot be empty.' });
return;
}

if (!title.teamCode) {
res.status(400).send({ message: 'Please provide a team code.' });
return;
}

const teamCodeExists = await checkTeamCodeExists(title.teamCode);
if (!teamCodeExists) {
res.status(400).send({ message: 'Invalid team code. Please provide a valid team code.' });
return;
}

// validate if project exist
const projectExist = await checkProjectExists(title.projectAssigned._id);
if (!projectExist) {
res.status(400).send({ message: 'Project lalala is empty or not exist!!!' });
return;
}

// validate if team exist
if (title.teamAssiged && title.teamAssiged._id === 'N/A') {
res.status(400).send({ message: 'Team not exists.' });
return;
}

title
.save()
.then((results) => res.status(200).send(results))
.catch((error) => res.status(404).send(error))
};

// update title function.
const updateTitle = async function (req, res) {
try {

const filter = req.body.id;

// valid title name
if (!title.titleName.trim()) {
if (!req.body.titleName.trim()) {
res.status(400).send({ message: 'Title cannot be empty.' });
return;
}

// if media is empty
if (!title.mediaFolder.trim()) {
res.status(400).send({ message: 'Media folder cannot be empty.' });
if (!req.body.titleCode.trim()) {
res.status(400).send({ message: 'Title code cannot be empty.' });
return;
}

const shortnames = title.titleName.trim().split(' ');
let shortname;
if (shortnames.length > 1) {
shortname = (shortnames[0][0] + shortnames[1][0]).toUpperCase();
} else if (shortnames.length === 1) {
shortname = shortnames[0][0].toUpperCase();
const titleCodeRegex = /^[A-Za-z]+$/;
if (!titleCodeRegex.test(req.body.titleCode)) {
return res.status(400).send({ message: 'Title Code must contain only upper or lower case letters.' });
}
title.shortName = shortname;

// Validate team code by checking if it exists in the database
if (!title.teamCode) {
// if media is empty
if (!req.body.mediaFolder.trim()) {
res.status(400).send({ message: 'Media folder cannot be empty.' });
return;
}

if (!req.body.teamCode) {
res.status(400).send({ message: 'Please provide a team code.' });
return;
}

const teamCodeExists = await checkTeamCodeExists(title.teamCode);
const teamCodeExists = await checkTeamCodeExists(req.body.teamCode);
if (!teamCodeExists) {
res.status(400).send({ message: 'Invalid team code. Please provide a valid team code.' });
return;
}

// validate if project exist
const projectExist = await checkProjectExists(title.projectAssigned._id);
const projectExist = await checkProjectExists(req.body.projectAssigned._id);
if (!projectExist) {
res.status(400).send({ message: 'Project is empty or not exist.' });
res.status(400).send({ message: 'Project is empty or not exist~~~' });
return;
}

// validate if team exist
if (title.teamAssiged && title.teamAssiged._id === 'N/A') {
if (req.body.teamAssiged && req.body.teamAssiged._id === 'N/A') {
res.status(400).send({ message: 'Team not exists.' });
return;
}
const result = await Title.findById(filter);
result.titleName = req.body.titleName;
result.titleCode = req.body.titleCode;
result.teamCode = req.body.teamCode;
result.projectAssigned = req.body.projectAssigned;
result.mediaFolder = req.body.mediaFolder;
result.teamAssiged = req.body.teamAssiged;
const updatedTitle = await result.save();
res.status(200).send({ message: 'Update successful', updatedTitle });

} catch (error) {
console.log(error);
res.status(500).send({ message: 'An error occurred', error });
}

};

const deleteTitleById = async function (req, res) {
const { titleId } = req.params;
Title.deleteOne({ _id: titleId })
.then((result) => res.send(result))
.catch((error) => res.send(error));
};

title
.save()
.then((results) => res.status(200).send(results))
.catch((error) => res.status(404).send(error));
};

const deleteTitleById = async function (req, res) {
const { titleId } = req.params;
Title.deleteOne({ _id: titleId })
.then((result) => res.send(result))
.catch((error) => res.send(error));
};

const deleteAllTitles = async function (req, res) {
Title.deleteMany({})
.then((result) => {
if (result.deletedCount === 0) {
res.send({ message: 'No titles found to delete.' });
} else {
res.send({ message: `${result.deletedCount} titles were deleted successfully.` });
}
})
.catch((error) => {
res.status(500).send(error);
});
};
// Update: Confirmed with Jae. Team code is not related to the Team data model. But the team code field within the UserProfile data model.
async function checkTeamCodeExists(teamCode) {
try {
if (cache.getCache('teamCodes')) {
const teamCodes = JSON.parse(cache.getCache('teamCodes'));
return teamCodes.includes(teamCode);
const deleteAllTitles = async function (req, res) {
Title.deleteMany({})
.then((result) => {
if (result.deletedCount === 0) {
res.send({ message: 'No titles found to delete.' });
} else {
res.send({ message: `${result.deletedCount} titles were deleted successfully.` });
}
const teamCodes = await getAllTeamCodeHelper();
return teamCodes.includes(teamCode);
} catch (error) {
console.error('Error checking if team code exists:', error);
throw error;
}
}
})
.catch((error) => {
console.log(error)
res.status(500).send(error);
});
};



async function checkProjectExists(projectID) {
try {
const project = await Project.findOne({ _id: projectID }).exec();
return !!project;
} catch (error) {
console.error('Error checking if project exists:', error);
throw error;
}
}

return {
getAllTitles,
getTitleById,
postTitle,
deleteTitleById,
deleteAllTitles,
updateTitle
};
};

module.exports = titlecontroller;

module.exports = titlecontroller;
6 changes: 4 additions & 2 deletions src/routes/titleRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ const express = require('express');

const router = function (title) {
const controller = require('../controllers/titleController')(title);

const titleRouter = express.Router();

titleRouter.route('/title')
.get(controller.getAllTitles)
.post(controller.postTitle);
.post(controller.postTitle)
// .put(controller.putTitle);

titleRouter.route('/title/update').post(controller.updateTitle);

titleRouter.route('/title/:titleId')
.get(controller.getTitleById)
Expand Down
Loading