From 272bf6689ab595f45dfb90c74f663188d811a1fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Thorsnes?= Date: Thu, 21 Dec 2023 15:31:33 +0100 Subject: [PATCH] added county to document --- Documents/build-document-query.js | 2 ++ lib/repack-document-county.js | 33 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 lib/repack-document-county.js diff --git a/Documents/build-document-query.js b/Documents/build-document-query.js index 165b167..4416297 100644 --- a/Documents/build-document-query.js +++ b/Documents/build-document-query.js @@ -3,6 +3,7 @@ const { encryptContent } = require('@vtfk/encryption') const { ObjectId } = require('mongodb') const { dataModificationsAdd } = require('../lib/crud') const repackDocumentSchool = require('../lib/repack-document-school') +const repackDocumentCounty = require('../lib/repack-document-county') const repackDocumentStudent = require('../lib/repack-document-student') const repackDocumentTeacher = require('../lib/repack-document-teacher') const config = require('../config') @@ -42,6 +43,7 @@ module.exports.getNewDocumentQuery = ({ user, body, student, teacher }) => { query.student = repackDocumentStudent(student) query.teacher = repackDocumentTeacher(teacher) query.school = repackDocumentSchool(student) + query.county = repackDocumentCounty(student) query.isEncrypted = body.isEncrypted || false // TODO: Append additional content data for YFF diff --git a/lib/repack-document-county.js b/lib/repack-document-county.js new file mode 100644 index 0000000..7b6802b --- /dev/null +++ b/lib/repack-document-county.js @@ -0,0 +1,33 @@ +const HTTPError = require('./http-error') +const repackStudent = require('./repack-student') +const getSchools = require('vtfk-schools-info') + +module.exports = (student) => { + const repacked = student.username ? student : repackStudent(student, true, true) + const { schoolId: id } = repacked + { + // First try to find school with schoolId + const schools = getSchools({ schoolId: id }) + if (schools.length === 1) { + const { county, countyNumber } = schools[0] + if (!(county && countyNumber)) throw new HTTPError(500, `Could not find county and countyNumber for school with schoolId equal to ${id} `) + return { + county, + countyNumber + } + } + } + { + // If not found we try with schoolNumber + const schools = getSchools({ schoolNumber: id }) + if (schools.length === 1) { + const { county, countyNumber } = schools[0] + if (!(county && countyNumber)) throw new HTTPError(500, `Could not find county and countyNumber for school with schoolId equal to ${id} `) + return { + county, + countyNumber + } + } + } + throw new HTTPError(500, `Could not find any unique school in vtfk-schools-info with schoolId or schoolNumber equal to ${id} `) +}