2023-03-04

This commit is contained in:
2024-03-04 13:32:25 -06:00
parent 6576d17539
commit a505747b06
85 changed files with 13977 additions and 2881 deletions

View File

@@ -0,0 +1,181 @@
const path = require('path')
const fs = require('fs')
const {embeddedSvgFromPath, parsePositionLabel} = require("../lib/utils")
const tsUtils = require('../lib/utils')
const { loadEventLineupEntries } = require('teamsnap.js')
exports.partials = path.join(__dirname, "../views/eventlineup/partials")
const statusCodeIcons = {
1: embeddedSvgFromPath("/teamsnap-ui/assets/icons/check.svg"),
0: embeddedSvgFromPath("/teamsnap-ui/assets/icons/dismiss.svg"),
2: embeddedSvgFromPath("/bootstrap-icons/question-lg.svg"),
null: embeddedSvgFromPath("/bootstrap-icons/question.svg"),
undefined: embeddedSvgFromPath("/bootstrap-icons/question-lg.svg")
}
exports.helpers = {
plus1: (i) => Number(i)+1,
positions: () => ["P", "C", "1B", "2B", "3B", "SS", "LF", "CF", "RF", "EH", "DH"],
defense_positions: () => ["C", "1B", "2B", "3B", "SS", "LF", "CF", "RF", "P"],
avail_status_code_icon: (status_code) => {
const icon_classes = {
1: "u-colorPositive",
0: "u-colorNegative",
2: "u-colorPrimary",
null: "u-colorGrey",
undefined: "u-colorGrey"
}
const button_classes = {
1: "Button--yes",
0: "Button--no",
2: "Button--maybe",
null: "",
undefined: ""
}
return `<button class="Button Button--smallSquare ${button_classes[status_code]}" type="button"><span class="">${statusCodeIcons[status_code]}</span></button>`
},
positionLabelWithoutFlags: (label) => {
return label.replace(/(.*?)\s\[(.*?)\]/, "$1");
},
comparePositionWithFlags: (labelWithoutFlags, eventLineupEntry, options) => {
labelWithFlags = eventLineupEntry?.label
const {positionLabelWithoutFlags} = parsePositionLabel(labelWithFlags);
return positionLabelWithoutFlags == labelWithoutFlags;
},
isStarting: (member) => {
return (member.benchcoach?.eventLineupEntry != null);
},
isInStartingLineup: (member) => {
if (member.benchcoach.eventLineupEntry == null) return false;
const {positionFlags} = parsePositionLabel(member.benchcoach.eventLineupEntry?.label);
return (positionFlags != "PO")
},
isInPositionOnly: (member) => {
if (!member.benchcoach || member.benchcoach.eventLineupEntry == null) return false;
const {positionFlags} = parsePositionLabel(member.benchcoach.eventLineupEntry?.label);
return (member.benchcoach.eventLineupEntry != null && positionFlags == "PO")
},
isInBench: (member) => {
if (member.benchcoach.eventLineupEntry != null || member.isNonPlayer) return false;
return (member.benchcoach.availability?.statusCode != 0 && member.benchcoach.availability?.statusCode != null)
},
isInOut: (member) => {
if (member.benchcoach.eventLineupEntry != null || member.isNonPlayer) return false;
return (member.benchcoach.availability?.statusCode == 0 || member.benchcoach.availability?.statusCode == null)
},
availabilityStatusShort: (availability) => {
const {YES, MAYBE, NO, NONE} = teamsnap.AVAILABILITIES
const statusShortLookup = {}
statusShortLookup[YES] = "YES"
statusShortLookup[MAYBE] = "MAY"
statusShortLookup[NO] = "NO"
statusShortLookup[NONE] = "UNK"
statusShortLookup[undefined] = "UNK"
return (statusShortLookup[availability?.statusCode])
}
}
exports.getEventLineup = async (req, res)=>{
// res.send(req.event_lineup)
await Promise.all(req.promises)
const {user, team, members, event, layout, event_lineup, event_lineup_entries, availabilities, availabilitySummary, csrfToken} = req
attachBenchcoachPropertiesToMember(members, event_lineup_entries, availabilities)
members.sort(tsUtils.teamsnapMembersSortLineupAvailabilityLastName)
res.render("eventlineup/edit", {user, team, members, event, layout, event_lineup, event_lineup_entries, availabilitySummary, csrfToken})
}
attachBenchcoachPropertiesToMember = (members, event_lineup_entries, availabilities) => {
members.forEach((member)=> {
// I *think* this can be done with linking https://github.com/teamsnap/teamsnap-javascript-sdk/wiki/Persistence#linking
// here's an example:
// member.link('eventLineupEntry', event_lineup_entries.find(i=>i.id=members[1].id))
member.benchcoach = {}
// I don't really like this, but the member_id changes once a season is archived.
// as far as I can tell, member_name should consistently be formulated from first and last name
// perhaps could have some edge cases if first or last names change, but this *should be* exceedingly rare.
const member_name = `${member.firstName} ${member.lastName}`
const event_lineup_entry = event_lineup_entries.find(e=> e.memberId == member.id || e.memberName == member_name)
const availability = availabilities.find(e=>e.memberId == member.id)
member.benchcoach.availability = availability
if (event_lineup_entry != null) {
// member.link('eventLineupEntry', event_lineup_entry)
member.benchcoach.eventLineupEntry = event_lineup_entry
const {positionLabelWithoutFlag} = parsePositionLabel(event_lineup_entry.label);
member.benchcoach.eventLineupEntry.positionLabelWithoutFlag = positionLabelWithoutFlag
}
else {
member.benchcoach.eventLineupEntry = null
}
}
)
}
exports.attachBenchcoachPropertiesToMember = attachBenchcoachPropertiesToMember
exports.getEventLineupEmail = async (req, res)=>{
const {body} = req
if (body.memberId == null) {res.status(400).end();return}
await Promise.all(req.promises)
const {user, team, members, event, layout, event_lineup, event_lineup_entries, availabilities, availabilitySummary} = req
const eventLineupEntries = req.event_lineup.eventLineupEntries
const {newEventLineupEntries} = processPostedEventLineupEntries(body, eventLineupEntries, event_lineup)
attachBenchcoachPropertiesToMember(members, newEventLineupEntries, availabilities)
members.sort(tsUtils.teamsnapMembersSortLineupAvailabilityLastName)
// eventLineup = await teamsnap.loadEventLineups(req.params.event_id)
res.status(200).render("eventlineup/email", {user, team, members, event, event_lineup, event_lineup_entries: newEventLineupEntries, availabilities, availabilitySummary})
}
exports.getEventLineupEntries = async (req, res)=>{
const {event_lineup, event_lineup_entries} = req
res.setHeader('Content-Type', 'application/json').send(JSON.stringify(req.event_lineup_entries))
}
exports.getEventLineupEntriesData = async (req, res)=>{
const {event_lineup, event_lineup_entries} = req
res.setHeader('Content-Type', 'application/json').send(JSON.stringify(req.event_lineup_entries))
}
exports.postEventLineup = async (req,res) => {
const {body} = req
if (body.memberId == null) {res.status(400).end();return}
await Promise.all(req.promises);
const eventLineupEntries = req.event_lineup.eventLineupEntries
const {newEventLineupEntries} = processPostedEventLineupEntries(body, eventLineupEntries, req.event_lineup)
newEventLineupEntries.forEach(e=>{
teamsnap.saveEventLineupEntry(e)
})
eventLineup = await teamsnap.loadEventLineups(req.params.event_id)
res.status(201).end()
}
const processPostedEventLineupEntries = (body, eventLineupEntries, eventLineup) => {
const newEventLineupEntries = []
body.memberId.forEach((memberId, i)=>{
const lineupEntryId = body.eventLineupEntryId[i]
const lineupEntryLabel = body.label[i]
const lineupEntrySequence = body.sequence[i]
if (lineupEntryId != '') {
// Update lineup entry
const eventLineupEntry = eventLineupEntries.find((e)=>e.id==Number(lineupEntryId))
eventLineupEntry.sequence = lineupEntrySequence
eventLineupEntry.label = lineupEntryLabel
newEventLineupEntries.push(eventLineupEntry)
}
else if (lineupEntryLabel != '') {
// Create lineup entry
const eventLineupEntry = teamsnap.createEventLineupEntry()
eventLineupEntry.eventLineupId = eventLineup.id
eventLineupEntry.memberId = memberId
eventLineupEntry.sequence = lineupEntrySequence
eventLineupEntry.label = lineupEntryLabel
newEventLineupEntries.push(eventLineupEntry)
}
else {
// Skip lineup entry
}
})
return {newEventLineupEntries, eventLineupEntries}
}