Skip to content

Commit

Permalink
Merge pull request #179 from GSA/feature/95_ART_API
Browse files Browse the repository at this point in the history
Adding ART Language to Backend
  • Loading branch information
BuckinghamAJ authored Sep 24, 2024
2 parents 26a4e4a + da3bc78 commit 7a68bbf
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
4 changes: 1 addition & 3 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ module.exports = {
app.post('/api/solicitation', token(), solicitationRoutes.postSolicitation)
app.get('/api/solicitation/:id', token(), solicitationRoutes.get)
app.post('/api/solicitation/:id', token(), solicitationRoutes.update)
app.post('/api/solicitation/art/:id', token(), solicitationRoutes.postArtLanguage)
app.post('/api/feedback', token(), solicitationRoutes.solicitationFeedback)
app.get('/api/surveys', token(), surveyRoutes.getSurveyQuestions)
app.get('/api/surveys/:solNum', token(), surveyRoutes.get)
Expand All @@ -272,9 +273,6 @@ module.exports = {
app.get('/api/reports/predictionMetrics', token(), admin_only(), adminReportRoutes.predictionReport)
app.get('/api/reports/noticeTypeChangeReport', token(), admin_only(), adminReportRoutes.noticeTypeChangeReport)




app.use(expressWinston.errorLogger({
transports: transports,
format: winston.format.combine(
Expand Down
43 changes: 43 additions & 0 deletions server/models/art_language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

module.exports = (sequelize, DataTypes) => {
const ArtLanguage = sequelize.define('ArtLanguage',
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement:true
},
solicitation_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'Solicitation', // Name of the referenced table
key: 'id', // Key in the referenced table
}
},

language: {
type: DataTypes.JSONB,
allowNull: false
},
createdAt: { type: DataTypes.DATE, allowNull: false },
updatedAt: { type: DataTypes.DATE },
},{
tableName: 'art_language',
timestamps: true
}
)

ArtLanguage.associate = function(models) {
// associations can be defined here
ArtLanguage.belongsTo(models.Solicitation, {
foreignKey: 'solicitation_id',
targetKey: 'id',
as: 'solicitation'
})

}

return ArtLanguage;
};
9 changes: 9 additions & 0 deletions server/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@
*
*/

/**
* @type {Model} ArtLanguage
* @property {number} id: DataTypes.INTEGER,
* @property {number} solicitation_id: DataTypes.INTEGER,
* @property {Object} language: DataTypes.JSONB,
* @property {date} createdAt: DataTypes.DATE,
* @property {date} updatedAt: DataTypes.DATE
*/

const fs = require('fs')
const path = require('path')
const {Sequelize, Model} = require('sequelize')
Expand Down
36 changes: 36 additions & 0 deletions server/routes/solicitation.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const logger = require('../config/winston')
// noinspection JSUnresolvedVariable
const Notice = require('../models').notice
const Solicitation = require('../models').Solicitation
const ArtLanguage = require('../models').ArtLanguage
const predictionRoute = require('../routes/prediction.routes')
const authRoutes = require('../routes/auth.routes')
const surveyRoutes = require('../routes/survey.routes')
Expand Down Expand Up @@ -308,7 +309,42 @@ module.exports = function (db, userRoutes) {
// logger.log('error', 'error in: solicitationFeedback', { error:e, tag: 'solicitationFeedback' })
// res.status(400).send(e)
// })
},

postArtLanguage: async (req, res) => {
const solicitationId = req.params.id; // Assuming solicitation ID is passed as a URL parameter
const artLanguageData = req.body; // Assuming the new ArtLanguage data is in the request body

let art_db_form = {
solicitation_id: solicitationId,
language: artLanguageData,
}

try {
// Optionally, validate solicitationId and artLanguageData here

// Find or create ArtLanguage record
console.log('art_db_form:', art_db_form);

let artLanguage = await ArtLanguage.findOne({ where: { solicitation_id: solicitationId } });

if (artLanguage) {
// If exists, update the record
await artLanguage.update(art_db_form);
} else {
// If doesn't exist, create a new record with solicitation ID
artLanguage = await ArtLanguage.create(art_db_form);
}
// Send success response
res.json({
message: 'ArtLanguage updated successfully',
artLanguage
});
} catch (error) {
console.error('Error updating ArtLanguage:', error);
res.status(500).json({ message: 'Failed to update ArtLanguage' });
}
},

}
}
23 changes: 23 additions & 0 deletions server/tests/solicitation.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,5 +368,28 @@ describe('solicitation tests', () => {

})

test('creates a new ArtLanguage if not exists', async () => {
const req = {
params: { solicitationId: '1' },
body: { language: 'English', proficiency: 'Native' },
};
const res = {
json: jest.fn(),
status: jest.fn().mockReturnThis(),
};
ArtLanguage.findOne.mockResolvedValue(null);
ArtLanguage.create.mockResolvedValue({ id: '1', ...req.body });

await putArtLanguage(req, res);

expect(ArtLanguage.create).toHaveBeenCalledWith({
solicitation_id: '1',
language: 'English',
proficiency: 'Native',
});
expect(res.json).toHaveBeenCalledWith(expect.objectContaining({
message: 'ArtLanguage updated successfully',
}));
});

}) // end describe

0 comments on commit 7a68bbf

Please sign in to comment.