Skip to content

Commit dd06c34

Browse files
authored
Merge pull request #99 from Unipisa/copilot/make-ssd-field-multiple-selection
Make SSD field in Staff model a multiple selection field
2 parents fea458a + c651594 commit dd06c34

File tree

10 files changed

+57
-10
lines changed

10 files changed

+57
-10
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/controllers/processes/sanityCheck.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ router.get('/', async (req, res) => {
7676
isInternal: true,
7777
$or: [
7878
{ SSD: { $exists: false } },
79-
{ SSD: "" }
79+
{ SSD: [] },
80+
{ SSD: "" } // handle legacy data if migration hasn't run yet
8081
],
8182
startDate: { $lt: new Date() },
8283
qualification: { $ne: 'PTA' },

server/controllers/public/conferences.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ async function conferencesQuery(req) {
4343
}
4444

4545
if (req.query.ssd) {
46+
// SSD is now an array; MongoDB will match if the array contains this value
4647
match["SSD"] = req.query.ssd
4748
}
4849

server/controllers/public/grants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ async function grantsQuery(req) {
3838
}
3939

4040
if (req.query.ssd) {
41+
// SSD is now an array; MongoDB will match if the array contains this value
4142
match["SSD"] = req.query.ssd
4243
}
4344

server/migrations.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,36 @@ const migrations = {
533533
)
534534
return true
535535
},
536+
537+
D20251023_staff_ssd_to_array: async function(db) {
538+
const staffs = db.collection('staffs')
539+
// Convert existing SSD string values to arrays
540+
// For documents where SSD is a string (not already an array)
541+
const result = await staffs.updateMany(
542+
{ SSD: { $type: "string" } },
543+
[{
544+
$set: {
545+
SSD: {
546+
$cond: {
547+
if: { $or: [{ $eq: ["$SSD", ""] }, { $eq: ["$SSD", null] }] },
548+
then: [],
549+
else: ["$SSD"]
550+
}
551+
}
552+
}
553+
}]
554+
)
555+
console.log(`Converted ${result.modifiedCount} staff SSD fields from string to array`)
556+
557+
// Ensure documents without SSD field get an empty array
558+
const result2 = await staffs.updateMany(
559+
{ SSD: { $exists: false } },
560+
{ $set: { SSD: [] } }
561+
)
562+
console.log(`Set ${result2.modifiedCount} missing SSD fields to empty array`)
563+
564+
return true
565+
},
536566
}
537567

538568
async function migrate(db, options) {

server/models/Staff.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const {
44
ObjectId,
55
startDate,
66
endDate,
7-
SSD,
7+
multipleSSDs,
88
createdBy,
99
updatedBy,
1010
} = require('./Model')
@@ -27,7 +27,7 @@ const staffSchema = new Schema({
2727
isInternal: {type: Boolean, label: 'interno al dipartimento', default: true},
2828
startDate,
2929
endDate,
30-
SSD,
30+
SSD: multipleSSDs,
3131
// 09/04/2025: hiding as not used anymore, CDP
3232
// photoUrl: {type: String, label: 'URL foto'},
3333
// wordpressId: String,

server/test/ModelSchema.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,13 @@ describe('grant_SSD_can_filter', () => {
55
expect(ModelSchemas.Grant.fields.SSD.can_filter).toBe(true)
66
})
77
})
8+
9+
describe('staff_SSD_is_array', () => {
10+
it('is array type', async () => {
11+
expect(ModelSchemas.Staff.fields.SSD.type).toBe('array')
12+
})
13+
14+
it('can_filter', async () => {
15+
expect(ModelSchemas.Staff.fields.SSD.can_filter).toBe(true)
16+
})
17+
})

src/processes/Conference.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default function Conference() {
2323
title: "",
2424
startDate: null,
2525
endDate: null,
26-
SSD: null,
26+
SSD: [],
2727
url: "",
2828
conferenceRoom: null,
2929
institution: null,

src/processes/Visit.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ export default function Visit({variant}) {
3030
// set SSD from user staffs info
3131
if (id === '__new__' && variant === 'my/') {
3232
for (const staff of user.staffs) {
33-
if (staff.SSD) visit.SSD = staff.SSD
33+
if (staff.SSD && staff.SSD.length > 0) {
34+
visit.SSD = Array.isArray(staff.SSD) ? staff.SSD[0] : staff.SSD
35+
}
3436
}
3537
}
3638

@@ -422,8 +424,9 @@ function ActiveVisitDetailsBlock({data, setData, done, variant, fetchSeminars})
422424
for (const person of people) {
423425
if (!person.staffs) continue
424426
for (const staff of person.staffs) {
425-
if (staff.SSD) {
426-
setData(data => ({...data, SSD: staff.SSD}))
427+
if (staff.SSD && staff.SSD.length > 0) {
428+
const ssdValue = Array.isArray(staff.SSD) ? staff.SSD[0] : staff.SSD
429+
setData(data => ({...data, SSD: ssdValue}))
427430
}
428431
}
429432
}

widgets/src/components/PersonDetails.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ function PersonBlock({data, en}) {
4848
const photoUrl = data.photoUrl || "https://www.dm.unipi.it/wp-content/uploads/2024/07/No-Image-Placeholder.png";
4949
const feminine = data.gender === 'Donna';
5050
const qualification = (data.staffs || []).map(q => getRoleLabel(q.qualification, en, feminine)).join(', ');
51-
const researchGroup = [...new Set(data.staffs.map(q => q.SSD).filter(q => q).map(ssd => getResearchGroupLabel(ssd, en)))].join(', ');
51+
// SSD is now an array, so we need to flatten it
52+
const researchGroup = [...new Set(data.staffs.flatMap(q => q.SSD || []).filter(ssd => ssd).map(ssd => getResearchGroupLabel(ssd, en)))].join(', ');
5253
const roomDetails = (data.roomAssignments || []).map(r => getRoomDetails(r.roomDetails, r.room, en));
5354

5455
return (

0 commit comments

Comments
 (0)