187 lines
5.2 KiB
JavaScript
187 lines
5.2 KiB
JavaScript
const path = require('path')
|
|
const fs = require('fs')
|
|
|
|
exports.teamsnapMembersSortLineupAvailabilityLastName = (a, b) => {
|
|
status_code_sort = [
|
|
teamsnap.AVAILABILITIES.YES,
|
|
teamsnap.AVAILABILITIES.MAYBE,
|
|
teamsnap.AVAILABILITIES.NO,
|
|
teamsnap.AVAILABILITIES.NONE,
|
|
];
|
|
|
|
if (a.benchcoach.eventLineupEntry != null && b.benchcoach.eventLineupEntry != null){
|
|
return a.benchcoach.eventLineupEntry.sequence - b.benchcoach.eventLineupEntry.sequence
|
|
}
|
|
else if (a.benchcoach.eventLineupEntry != null && b.benchcoach.eventLineupEntry == null){
|
|
return -1
|
|
}
|
|
else if (a.benchcoach.eventLineupEntry == null && b.benchcoach.eventLineupEntry != null) {
|
|
return 1
|
|
}
|
|
else {
|
|
return teamsnapMembersSortAvailabilityLastName(a,b)
|
|
}
|
|
};
|
|
|
|
teamsnapMembersSortAvailabilityLastName = (a, b) => {
|
|
status_code_sort = [
|
|
teamsnap.AVAILABILITIES.YES,
|
|
teamsnap.AVAILABILITIES.MAYBE,
|
|
teamsnap.AVAILABILITIES.NO,
|
|
teamsnap.AVAILABILITIES.NONE,
|
|
];
|
|
|
|
a_sort = status_code_sort.indexOf(a.benchcoach.availability?.statusCode);
|
|
b_sort = status_code_sort.indexOf(b.benchcoach.availability?.statusCode);
|
|
if (a_sort > b_sort) {
|
|
return 1;
|
|
}
|
|
if (a_sort < b_sort) {
|
|
return -1;
|
|
}
|
|
if (a_sort == b_sort) {
|
|
if (a.lastName < b.lastName) {
|
|
return -1;
|
|
}
|
|
if (a.lastName > b.lastName) {
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
exports.teamsnapMembersSortAvailabilityLastName = teamsnapMembersSortAvailabilityLastName
|
|
|
|
exports.initTeamsnap = (req, res, next) => {
|
|
if (!teamsnap.isAuthed()) {
|
|
teamsnap.init(process.env["TEAMSNAP_CLIENT_ID"]);
|
|
teamsnap.auth(req.user.accessToken);
|
|
}
|
|
teamsnap.loadCollections((err) => {
|
|
teamsnap.enablePersistence();
|
|
next(req, res, next);
|
|
});
|
|
};
|
|
|
|
exports.teamsnapLog = (method, types, id, req, message="") => {
|
|
console.log(
|
|
'\x1b[33mTeamSnap:\x1b[0m',
|
|
`${method} for \x1b[33m\[${types}\]\x1b[0m on ${id}`,
|
|
`on url ${req.url}`,
|
|
`"${message}"`
|
|
)
|
|
return;
|
|
}
|
|
|
|
exports.tsPromise = (func_name, params) => {
|
|
return new Promise(function(resolve, reject) {
|
|
teamsnap.bulkLoad(
|
|
params,
|
|
(err, data) => {
|
|
console.log();
|
|
if (err !== null) {
|
|
reject(err);
|
|
}
|
|
else {
|
|
resolve(data);
|
|
}
|
|
}
|
|
)
|
|
;
|
|
});
|
|
}
|
|
|
|
exports.teamsnapCallback = (err,items) => {
|
|
if (err) {
|
|
console.log(err.message);
|
|
throw new Error(err)
|
|
}
|
|
return items;
|
|
}
|
|
|
|
exports.teamsnapFailure = (err, next) => {
|
|
if (err) {
|
|
console.log(err.message);
|
|
}
|
|
next(err);
|
|
}
|
|
|
|
const getPluralType = (type) =>{
|
|
// There are some instances where a type is not
|
|
// in the list of teamsnap.types, so a plural
|
|
// is not generated in the lookup. this is a
|
|
// kludge around that. (specifically availabilitySummary)
|
|
plural = teamsnap.getPluralType(type) || (function() {
|
|
switch (type.slice(-1)) {
|
|
case 'y':
|
|
return type.slice(0, -1) + 'ies';
|
|
case 's':
|
|
return type + 'es';
|
|
default:
|
|
return type + 's';
|
|
}
|
|
})();
|
|
return plural
|
|
}
|
|
|
|
exports.groupTeamsnapItems = (items, types = [], params = {}) => {
|
|
const result = {};
|
|
items.forEach(item => {
|
|
const type = item.type
|
|
const type_plural = getPluralType(type)
|
|
if ((types.length > 0 && types.includes(type)) || (types.length == 0)) {
|
|
if (!result[type_plural]) result[type_plural] = []
|
|
result[type_plural].push(item)
|
|
}
|
|
})
|
|
return result;
|
|
}
|
|
|
|
exports.embeddedSvgFromPath = (svg_path, additional_classes = "") => {
|
|
const iconStaticPaths = {
|
|
"/teamsnap-ui/assets":path.join(__dirname, "/../../node_modules/@teamsnap/teamsnap-ui/src/assets"),
|
|
"/bootstrap-icons":path.join(__dirname, "/../../node_modules/bootstrap-icons/icons"),
|
|
"/media":path.join(__dirname, "/../public/media")
|
|
}
|
|
|
|
for (const [key, value] of Object.entries(iconStaticPaths)) {
|
|
if (svg_path.startsWith(key)) {
|
|
svg_path = svg_path.replace(key, value)
|
|
}
|
|
}
|
|
|
|
const svg = fs.readFileSync(`${svg_path}`, 'utf8');
|
|
|
|
svgRegExWithClass = new RegExp(/<svg(.*)class="(.*?)"(.*)>/)
|
|
svgRegExWithoutClass = new RegExp(/<svg(.*?)>/)
|
|
|
|
if (svgRegExWithClass.test(svg)) {
|
|
return svg.replace(svgRegExWithClass, `<svg$1 class="$2 Icon ${additional_classes}"$3>`)
|
|
}
|
|
else if (svgRegExWithoutClass.test(svg)) {
|
|
return svg.replace(svgRegExWithoutClass, `<svg$1 class="Icon ${additional_classes}">`)
|
|
}
|
|
else return svg
|
|
}
|
|
|
|
exports.parsePositionLabel = (label) => {
|
|
if (label == null) {
|
|
return {
|
|
positionLabelWithoutFlags: null,
|
|
positionFlags: null
|
|
}
|
|
}
|
|
const pattern = /(?<pos>[A-Z0-9]+)(?:\s\[(?<flags>.[A-z,]+)\])?/g
|
|
const {pos, flags} = pattern.exec(label)?.groups || {}
|
|
const positionLabelWithoutFlags= pos
|
|
const positionFlags = flags?.split(',').map(f=>f.trim()) || []
|
|
return {positionLabelWithoutFlags, positionFlags}
|
|
}
|
|
|
|
exports.compilePositionLabel = (label, flags) => {
|
|
if (flags == null || flags == '' || flags.lengh == 0) {
|
|
return label
|
|
}
|
|
else {
|
|
const flags_set = new Set(flags.split(',').map(s=>s.trim()))
|
|
return `${label} [${Array.from(flags_set).sort().join(',')}]`
|
|
}
|
|
} |