Files
CyberGarden-Grade/hackathon-grading-system/scripts/export-marks-to-google.js
T

76 lines
2.1 KiB
JavaScript

import dotenv from 'dotenv';
dotenv.config();
import { initializeDbModels } from '../utils/db.js';
import { sequelize } from '../models/index.js';
import creds from '../config/google-credentials.js';
import { GoogleSpreadsheet } from 'google-spreadsheet';
import Case from '../models/case.js';
import Team from '../models/team.js';
import Mark from '../models/mark.js';
import User from '../models/user.js';
import markTypesNames from '../config/markTypesNames.js';
const doc = new GoogleSpreadsheet('1ofOGiYrDv_Pp-rJlgjKDJR2HY0IEHnSNmI1jwCIb7JU');
async function start() {
await initializeDbModels();
await doc.useServiceAccountAuth(creds);
await doc.loadInfo();
const cases = await Case.findAll({
attributes: ['id', 'name'],
include: [
{ model: Team, as: 'teams', order: 'name', attributes: ['id', 'name'], through: { attributes: [] } },
{
model: User,
as: 'users',
attributes: ['id', 'firstName', 'lastName'],
through: { attributes: [] },
include: { model: Mark, as: 'marks' },
},
],
});
let xOffset = 0;
let yOffset = 0;
for (const c of cases) {
const sheet = await doc.addSheet({ title: c.name });
await sheet.loadCells();
for (const user of c.users) {
const userName = sheet.getCell(xOffset, yOffset);
userName.value = user.firstName + ' ' + user.lastName;
xOffset += 2;
for (const team of c.teams) {
const teamName = sheet.getCell(xOffset, yOffset);
teamName.value = team.name;
xOffset++;
for (const [typeMark, nameTypeMark] of Object.entries(markTypesNames)) {
const markType = sheet.getCell(xOffset, yOffset);
const mark = sheet.getCell(xOffset, yOffset + 1);
markType.value = nameTypeMark;
const markValue = user.marks.find(m => m.teamId === team.id && m.markType === +typeMark)?.mark;
mark.value = markValue;
if (!markValue || markValue === 0)
mark.backgroundColor = {
red: 1,
green: 0,
blue: 0,
};
xOffset++;
}
xOffset++;
}
yOffset += 3;
xOffset = 0;
}
yOffset = 0;
await sheet.saveUpdatedCells();
}
await sequelize.close();
}
start();