2023-03-04
This commit is contained in:
143
src/lib/utils.js
143
src/lib/utils.js
@@ -1,27 +1,54 @@
|
||||
exports.teamsnapAvailabilitiesSort = (a, b) => {
|
||||
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,
|
||||
];
|
||||
a_sort = status_code_sort.indexOf(a.statusCode);
|
||||
b_sort = status_code_sort.indexOf(b.statusCode);
|
||||
if (a_sort > b_sort) {
|
||||
return 1;
|
||||
|
||||
if (a.benchcoach.eventLineupEntry != null && b.benchcoach.eventLineupEntry != null){
|
||||
return a.benchcoach.eventLineupEntry.sequence - b.benchcoach.eventLineupEntry.sequence
|
||||
}
|
||||
if (a_sort < b_sort) {
|
||||
return -1;
|
||||
else if (a.benchcoach.eventLineupEntry != null && b.benchcoach.eventLineupEntry == null){
|
||||
return -1
|
||||
}
|
||||
if (a_sort == b_sort) {
|
||||
if (a.member.lastName < b.member.lastName) {
|
||||
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.member.lastName > b.member.lastName) {
|
||||
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()) {
|
||||
@@ -33,3 +60,97 @@ exports.initTeamsnap = (req, res, next) => {
|
||||
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}"`
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
exports.teamsnapCallback = (err,items) => {
|
||||
if (err) {
|
||||
console.log(err.message);
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
exports.teamsnapFailure = (err) => {
|
||||
if (err) {
|
||||
console.log(err.message);
|
||||
}
|
||||
}
|
||||
|
||||
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 positionLabelWithoutFlags= label.replace(/(.*?)\s\[(.*?)\]/, "$1")
|
||||
const positionFlags = label.replace(/(.*?)\s\[(.*?)\]/, "$2")
|
||||
return {positionLabelWithoutFlags, positionFlags}
|
||||
}
|
||||
Reference in New Issue
Block a user