Skip to content

Commit

Permalink
Fix sequelize query
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniel Moschkin committed Dec 6, 2023
1 parent 440d876 commit a50a343
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
63 changes: 37 additions & 26 deletions app/logic/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,41 +113,52 @@ export async function voyageRawByDays(days: number, crewMatch?: string[]) {
return voyageRawByRange(startDate, endDate, crewMatch);
}

export async function voyageRawByRange(startDate?: Date, endDate?: Date, crewMatch?: string[]) {
export async function voyageRawByRange(startDate?: Date, endDate?: Date, crewMatch?: string[], crewOp?: string) {
endDate ??= new Date();
if (!startDate) {
startDate = new Date(endDate.getTime());
startDate.setDate(startDate.getDate() - 7);
}

const where = {
voyageDate: {
[Op.and]: [
{ [Op.gte]: startDate },
{ [Op.lte]: endDate }
]
},
crew: undefined as any
};

if (crewMatch) {
if (crewMatch.length > 1) {
where.crew = { [Op.or]: [] as any[] };
for (let crew of crewMatch) {
where.crew[Op.or].push({ [Op.like]: `%"${crew}"%`})
let results: Voyage[] | undefined = undefined;
if (crewMatch) {
results = await Voyage.findAll({
where: {
[Op.and]: [
{
voyageDate: {
[Op.and]: [
{ [Op.gte]: startDate },
{ [Op.lte]: endDate }
]
}
},
{
crew: {
[crewOp === 'and' ? Op.and : Op.or]: [
... crewMatch.map(c => {
return {
[Op.substring]: `"${c}"`
}
})
]
}
}
]
}
}
else {
where.crew = { [Op.like]: `%"${crewMatch[0]}"%` };
}
});
}
else {
delete where.crew;
results = await Voyage.findAll({
where: {
voyageDate: {
[Op.and]: [
{ [Op.gte]: startDate },
{ [Op.lte]: endDate }
]
}
}
});
}

let results = await Voyage.findAll({
where
});

return results;
}
Expand Down
1 change: 1 addition & 0 deletions app/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const app: express.Application = express();

// When used with nginx reverse proxy, pick a rerouting port
let port: number = 4420;
//let port: number = 4421;

if (process.argv.length > 2) {
port = parseInt(process.argv[2]);
Expand Down

0 comments on commit a50a343

Please sign in to comment.