Skip to content

Commit

Permalink
Merge pull request #735 from elmadev/feature/personal-crippled-time-file
Browse files Browse the repository at this point in the history
feat(levels): include time file data to personal crippled times
  • Loading branch information
sunehs authored Jan 20, 2025
2 parents 7122d40 + a6739bd commit 731654e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/api/crippled.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from 'express';
import { authContext } from '#utils/auth';
import sequelize, { Op } from 'sequelize';
import { orderBy, invert, groupBy, omit, mapValues } from 'lodash-es';
import { Crippled, Kuski, Level, Time } from '#data/models';
import { Crippled, Kuski, Level, Time, TimeFile } from '#data/models';
import { getCrippledTypes } from '#data/models/Crippled';
import { query } from '#utils/sequelize';
import { getPackByName } from './levelpack.js';
Expand Down Expand Up @@ -132,11 +132,31 @@ const getTimesEtc = async (LevelIndex, CrippledType) => {
return [times, parseBestTimes(times), parseLeaderHistory(times)];
};

const getKuskiTimes = async (LevelIndex, KuskiIndex, CrippledType) => {
const getKuskiTimes = async (
LevelIndex,
KuskiIndex,
CrippledType,
personal,
) => {
if (!LevelIndex || !KuskiIndex || CrippledType === null) {
return [[], []];
}

const include = [
{
model: Kuski,
as: 'KuskiData',
attributes: ['Kuski', 'Country'],
},
];

if (personal) {
include.push({
model: TimeFile,
as: 'TimeFileData',
});
}

// very fast when KuskiIndex provided
const kuskiTimes = await Crippled.findAll({
attributes: ['TimeIndex', 'Time', 'Driven'],
Expand All @@ -145,13 +165,7 @@ const getKuskiTimes = async (LevelIndex, KuskiIndex, CrippledType) => {
KuskiIndex,
CrippledType,
},
include: [
{
model: Kuski,
as: 'KuskiData',
attributes: ['Kuski', 'Country'],
},
],
include,
order: [
['Time', 'ASC'],
['TimeIndex', 'ASC'],
Expand Down Expand Up @@ -300,14 +314,16 @@ router
}

let KuskiIndex;
const auth = authContext(req);

if (+req.params.KuskiIndex > 0) {
KuskiIndex = +req.params.KuskiIndex;
} else {
const auth = authContext(req);
KuskiIndex = +auth.userid;
}

const personal = +auth.userid === KuskiIndex;

if (!KuskiIndex) {
res.status(400).send('KuskiIndex not valid, or not logged in.');
return;
Expand All @@ -317,6 +333,7 @@ router
+req.params.LevelIndex,
KuskiIndex,
getCrippledTypeInt(req.params.cripple),
personal,
);

res.json({
Expand Down
6 changes: 6 additions & 0 deletions src/data/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,12 @@ Crippled.belongsTo(Level, {
as: 'LevelData',
});

Crippled.hasOne(TimeFile, {
foreignKey: 'TimeIndex',
sourceKey: 'TimeIndex',
as: 'TimeFileData',
});

TimeFile.belongsTo(AllFinished, {
foreignKey: 'TimeIndex',
targetKey: 'TimeIndex',
Expand Down

0 comments on commit 731654e

Please sign in to comment.