diff --git a/.gitignore b/.gitignore
index c91a75046..8e0e2efda 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,23 +6,22 @@ shift.overrides
.idea/
!services/db/*
*.sql
+*.db
!services/db/*.sql
!services/db/*/*.sql
services/db/tmp/*.sql
*.today
*.DS_Store
+# this is the current location of event image file upload
+# (ex. on production and during local docker development)
backend/eventimages/
secrets
services/php/shift-mail.log
services/node/shift-mail.log
app/node_modules/
-# exclude all event images except the readme and test bike image.
-app/eventimages/*
-!app/eventimages/
-!app/eventimages/README.md
-!app/eventimages/bike.jpg
-!app/eventimages/bike-big.png
-!app/eventimages/bike-bad.tiff
+# once upon a time, this was where event images were uploaded.
+# therefore, exclude everything from this directory.
+app/eventimages/
bin/
node_modules/
/frontend/.vscode
diff --git a/app/app.js b/app/app.js
index c7335cba3..287fcfcba 100644
--- a/app/app.js
+++ b/app/app.js
@@ -5,20 +5,20 @@ const config = require('./config');
const { initMail } = require( './emailer');
const app = require( './appEndpoints');
const db = require('./db'); // initialize on startup
-const tables = require("./models/tables");
+const schema = require('shift-docs/schema');
// connect to the db
db.initialize().then(async () => {
// create db tables
- await tables.createTables();
+ await schema.setupTables(db);
// connect to the smtp server
await initMail().then(hostName => {
console.log("okay: verified email host", hostName);
}).catch(e => {
- console.error("failed smtp verification because", e.toString());
+ console.warn("Warning: Sending email will not work because", e.toString());
}).finally(_ => {
- console.log("and emails will log to", config.email.logfile || "console");
+ console.log("Emails will log to", config.email.logfile || "console");
});
// start a webserver to listen to all requests
diff --git a/app/config.js b/app/config.js
index 01c21843e..c0b1b4749 100644
--- a/app/config.js
+++ b/app/config.js
@@ -1,13 +1,40 @@
-const path = require('path');
-const fs = require("fs");
+/**
+ * Global configuration for the backend.
+ * Holds all environment, commandline options, paths, etc.
+ */
+const fs = require('node:fs'); // for reading email config
+const os = require('node:os'); // for tmpdir fallbacks
+const path = require('node:path');
+const CommandLine = require('shift-docs/util/cmdLine');
+// helper to read environment variables
function env_default(field, def) {
return process.env[field] ?? def;
}
-// node server listen for its http requests
-// fix? there is no such environment variable right now.
-const listen = env_default('NODE_PORT', 3080);
+const cmdLine = new CommandLine({
+ db_debug: [
+ "Can be either 'true' or 'false'.",
+ " Defaults to 'false'. When 'true', adds extra debugging info for queries.",
+ ],
+ db: [
+ "Can be either 'sqlite' or 'mysql'.",
+ "For prod and dev it defaults to 'mysql'.",
+ "For tests, it defaults to 'sqlite'.",
+ "- 'mysql': Requires docker.",
+ " For prod and dev it pulls its full configuration from the environment.",
+ " Tests have a hardcoded configuration.",
+ " This option can followed by a localhost port, '-db=mysql:61530'",
+ "- 'sqlite': Uses a sqlite database.",
+ " This option can followed by the path to its file.",
+ " The default is equivalent to specifying: '-db=sqlite:./bin/events.db'.",
+ ],
+});
+
+// determine the port for the node server.
+const listen = (() => {
+ return 3080;
+})();
// the user facing server.
const siteHost = siteUrl(listen);
@@ -18,20 +45,38 @@ const appPath = path.resolve(__dirname);
// for max file size
const bytesPerMeg = 1024*1024;
-const staticFiles = env_default('SHIFT_STATIC_FILES');
+// ex. npm_lifecycle_event: 'dev-setup'
+const isTesting = process.env.npm_lifecycle_event && process.env.npm_lifecycle_event.startsWith('test');
+
+// optional directory containing front-end content.
+// if it doesnt exist, assumes the app isn't trying to serve static files.
+// ( and that there is a separate front-end running out there, ex. production. )
+const staticFiles = (() => {
+ const p = env_default('SHIFT_STATIC_FILES');
+ return !p ? null : path.resolve(appPath, p);
+})();
-const isTesting = !!(process.env.npm_lifecycle_event || "").match(/test$/);
-// read the command line parameter for db configuration
-const dbType = env_default('npm_config_db');
-const dbDebug = !!env_default('npm_config_db_debug');
+// storage location for uploaded images.
+// forces a temp directory when running unit tests.
+const imageDestination = (() => {
+ const p = env_default('SHIFT_IMAGE_DIR');
+ if (isTesting) {
+ return os.tmpdir();
+ } else if (p) {
+ return path.resolve(appPath, p);
+ }
+})();
+const dbConfig = getDatabaseConfig(cmdLine.options.db, cmdLine.bool('db_debug'), isTesting);
+
+// The config object exported
const config = {
appPath,
api: {
header: 'Api-Version',
- version: "3.65.0",
+ version: "3.66.0",
},
- db: getDatabaseConfig(dbType, isTesting),
+ db: dbConfig,
// maybe bad, but some code likes to know:
isTesting,
// a nodemailer friendly config, or false if smtp is not configured.
@@ -40,7 +85,7 @@ const config = {
name: "SHIFT to Bikes",
listen,
path: "/",
- // used for shareable links, and the manage url sent in email
+ // used for crawl url, shareable links, and the manage url sent in email
url(...parts) {
const base = `${siteHost}${config.site.path}`;
return base + parts.join("/");
@@ -49,6 +94,10 @@ const config = {
return config.site.url("pages/calendar-faq/");
},
staticFiles,
+ // expose frontend content for adding and viewing events.
+ // in production: these endpoints are served via netfliy.
+ // uses path.posix to get forward slashes even on windows
+ // i forget exactly why...
devEndpoints: !staticFiles ? null : [{
// ex. http://localhost:3080/addevent/edit-1-d00c888b0a1d4bab8107ba2fbe2beddf
// loads http://localhost:3080/addevent/index.html
@@ -88,8 +137,7 @@ const config = {
},
image: {
// storage location for event images
- dir: path.resolve(appPath,
- env_default('SHIFT_IMAGE_DIR', "eventimages")),
+ dir: imageDestination,
// used for generated event image links
// ( see also the ngnix config )
path: env_default('SHIFT_IMAGE_PATH', "/eventimages/"),
@@ -142,7 +190,7 @@ module.exports = config;
// determine the user facing prefix for generating page and image links.
// production - https://shift2bikes.org
// docker dev - https://localhost:4443
-// standalone or test - http://localhost:3080
+// npm dev, npm test - http://localhost:3080
function siteUrl(proxyPort) {
const hostname = env_default('SHIFT_DOMAIN', 'localhost');
// production uses 443, docker dev uses 4443, standalone is null.
@@ -190,23 +238,18 @@ function getSmtpSettings() {
}
// our semi-agnostic database configuration
-function getDatabaseConfig(dbType, isTesting) {
- // dbType comes from the command-line
- // if nothing was specfied, use the MYSQL_DATABASE environment variable
- const env = env_default('MYSQL_DATABASE')
- if (!dbType && env) {
- dbType = env.startsWith("sqlite") ? env : null;
- }
- if (!dbType) {
- dbType = isTesting ? 'sqlite' : 'mysql'
- }
+function getDatabaseConfig(dbType, dbDebug, isTesting) {
+ dbType = dbType || readDbEnv(isTesting ? 'sqlite' : null);
+
const [name, parts] = dbType.split(':');
const config = {
mysql: !isTesting ? getMysqlDefault : getMysqlTesting,
sqlite: getSqliteConfig,
}
- if (!name in config) {
- throw new Error(`unknown database type '${dbType}'`)
+ if (!Object.hasOwn(config, name)) {
+ const types = Object.keys(config).join(", ");
+ const unknownConfig = `unknown database type '${dbType}'. expected one of: ${types}.`;
+ throw new Error(unknownConfig);
}
return {
type: name,
@@ -215,22 +258,46 @@ function getDatabaseConfig(dbType, isTesting) {
}
}
-// the default for mysql when running dev or production
-function getMysqlDefault() {
- return {
- host: env_default('MYSQL_HOST', 'db'),
- port: env_default('MYSQL_PORT', 3306), // standard mysql port.
- user: env_default('MYSQL_USER', 'shift'),
- pass: env_default('MYSQL_PASSWORD', 'ok124'),
- name: env_default('MYSQL_DATABASE', 'shift'),
+// nothing specified in the env fails
+// sqlite specified in the env uses.... sqlite.
+// anything other than sqlite in the env is assumed to be a mysql spec.
+function readDbEnv(fallback, env = 'MYSQL_DATABASE') {
+ const dbType = env_default(env) || fallback;
+ if (!dbType) {
+ throw new Error(`expected db type on command line or '${env}' env variable`);
}
+ return dbType.startsWith("sqlite") ? dbType : 'mysql';
+}
+
+// the default for mysql when running dev or production
+ // 3306 standard mysql port.
+function getMysqlDefault(port) {
+ const common = {
+ user: 'shift',
+ pass: 'ok124',
+ name: 'shift',
+ };
+ const env = {
+ user: env_default('MYSQL_USER'),
+ pass: env_default('MYSQL_PASSWORD'),
+ name: env_default('MYSQL_DATABASE'),
+ host: env_default('MYSQL_HOST', "db"),
+ port: env_default('MYSQL_PORT', 3306),
+ };
+ // normally, a port isnt assigned via the command line
+ // in that case, the environment wins over the common settings
+ // with a port, we ignore the environment
+ return Object.assign({}, common, !port ? env : {
+ host: "localhost",
+ port: port,
+ });
}
// the default for mysql when running tests
-function getMysqlTesting() {
+function getMysqlTesting(port) {
return {
host: "localhost",
- port: 3308, // custom EXTERNAL port to avoid conflicts
+ port: port ?? 3308, // custom EXTERNAL port to avoid conflicts
user: 'shift_test',
pass: 'shift_test',
name: 'shift_test',
@@ -239,9 +306,9 @@ function getMysqlTesting() {
// the default for sqlite
// if filename is null, it uses a memory database
-// paths are relative to npm's starting path.
+// relative paths are resolved using the shift root directory
function getSqliteConfig(filename) {
- const connection = !filename ? ":memory:" : path.resolve(appPath, filename);
+ const connection = !filename ? ":memory:" : path.resolve(appPath , filename);
return {
name: connection
};
diff --git a/app/db.js b/app/db.js
index 4d603b9e6..0f1b6099d 100644
--- a/app/db.js
+++ b/app/db.js
@@ -1,12 +1,10 @@
const knex = require('knex'); // the knex constructor
-const pickBy = require('lodash/pickBy'); // a dependency of package knex
-const config = require("./config");
-const dt = require("./util/dateTime");
+const config = require('shift-docs/config');
+const dt = require('shift-docs/util/dateTime');
// build knex configuration from our own
const dbConfig = unpackConfig(config.db);
const useSqlite = config.db.type === 'sqlite';
-const dropOnCreate = config.db.connect?.name === 'shift_test';
const db = {
config: config.db,
@@ -17,12 +15,13 @@ const db = {
query: false,
// waits to open a connection.
- async initialize(name) {
+ async initialize(debugName = "unknown") {
if (db.query) {
- throw new Error("db already initialized");
+ throw new Error(`db being initialized by ${debugName} when already initialized by ${this.debugName}.`);
}
const connection = knex(dbConfig);
db.query = connection;
+ db.debugName = debugName;
await connection;
},
@@ -33,6 +32,7 @@ const db = {
throw new Error("db already destroyed");
}
db.query = false;
+ db.debugName = `destroyed ${db.debugName}`;
return connection.destroy();
},
@@ -40,70 +40,9 @@ const db = {
raw(...args) {
return db.query.raw(...args);
},
-
- // convert a dayjs object to a 'date' column.
- //
- // the sqlite driver stores a date as a number
- // re: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
- // while that works, it's hard to read.
- //
- // mysql stores a date as "YYYY-MM-DD"
- // re: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-literals.html
- // but the code has been handing it javascript dates, and it seems to like that.
- // tbd: test if mysql can (also) use strings; if so, remove the if statement here.
- //
- toDate(date) {
- return !useSqlite ? date.toDate() : dt.toYMDString(date);
- },
-
- // sqlite and mysql differ on the keyword.
- currentDateString() {
- return !useSqlite ? `CURDATE()` : `DATE()`;
- },
-
- /**
- * update or insert into the database.
- * @param table string table name.
- * @oaram rec the object containing the data.
- * @return promises the rec ( with its new id ).
- *
- * fix? an orm would probably be smart enough to only update the needed fields.
- * this updates *everything*.
- */
- store(table, idField, rec) {
- const q = db.query(table);
- // get everything from that isn't a function()
- let cleanData = pickBy(rec, isSafe);
- if (rec.exists()) {
- // fix: manually set modified for sqlite?
- // cleanData.modified = dt.toTimestamp();
- return q.update(cleanData)
- .where(idField, rec[idField])
- .then(_ => rec);
- } else {
- return q.insert(cleanData)
- .then(row => {
- rec[idField] = row[0];
- return rec;
- });
- }
- },
- /**
- * delete one (or more) rows from the named table
- * where the named field has the value in the passed record.
- */
- del(table, idField, rec) {
- return db.query(table).where(idField, rec[idField]).del();
- },
};
-module.exports = db;
-// ugh. if knex sees a function in an object,
-// it assumes the function generates knex style queries and tries to call them.
-// filter them out, mimicking what knex does internally for undefined values.
-function isSafe(v, k) {
- return (typeof v !== 'function');
-}
+module.exports = db;
// turn the shift config into knex format
function unpackConfig({ type, connect, debug }) {
diff --git a/app/db.js.orig b/app/db.js.orig
new file mode 100644
index 000000000..b9a530fad
--- /dev/null
+++ b/app/db.js.orig
@@ -0,0 +1,137 @@
+const knex = require('knex'); // the knex constructor
+const config = require('shift-docs/config');
+const dt = require('shift-docs/util/dateTime');
+
+// build knex configuration from our own
+const dbConfig = unpackConfig(config.db);
+const useSqlite = config.db.type === 'sqlite';
+
+const db = {
+ config: config.db,
+
+ // access to the created knex object.
+ // ex. their `knex.select('*')` is our `db.query.select('*')`
+ // valid in-between db.initialize() and db.destroy()
+ query: false,
+
+ // waits to open a connection.
+ async initialize(debugName = "unknown") {
+ if (db.query) {
+ throw new Error(`db being initialized by ${debugName} when already initialized by ${this.debugName}.`);
+ }
+ const connection = knex(dbConfig);
+ db.query = connection;
+ db.debugName = debugName;
+ await connection;
+ },
+
+ // promise to close connections.
+ destroy() {
+ const connection = db.query;
+ if (!connection) {
+ throw new Error("db already destroyed");
+ }
+ db.query = false;
+ db.debugName = `destroyed ${db.debugName}`;
+ return connection.destroy();
+ },
+
+ // helper shortcut to make raw queries easier to write
+ raw(...args) {
+ return db.query.raw(...args);
+<<<<<<< HEAD
+=======
+ },
+
+ // convert a dayjs object to a 'date' column.
+ //
+ // the sqlite driver stores a date as a number
+ // re: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
+ // while that works, it's hard to read.
+ //
+ // mysql stores a date as "YYYY-MM-DD"
+ // re: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-literals.html
+ // but the code has been handing it javascript dates, and it seems to like that.
+ // tbd: test if mysql can (also) use strings; if so, remove the if statement here.
+ //
+ toDate(date) {
+ return !useSqlite ? date.toDate() : dt.toYMDString(date);
+ },
+
+ // sqlite and mysql differ on the keyword.
+ currentDateString() {
+ return !useSqlite ? `CURDATE()` : `DATE()`;
+ },
+
+ /**
+ * update or insert into the database.
+ * @param table string table name.
+ * @oaram rec the object containing the data.
+ * @return promises the rec ( with its new id ).
+ *
+ * fix? an orm would probably be smart enough to only update the needed fields.
+ * this updates *everything*.
+ */
+ store(table, idField, rec) {
+ const q = db.query(table);
+ // get everything from that isn't a function()
+ let cleanData = pickBy(rec, isSafe);
+ if (rec.exists()) {
+ // fix: manually set modified for sqlite?
+ // cleanData.modified = dt.toTimestamp();
+ return q.update(cleanData)
+ .where(idField, rec[idField])
+ .then(_ => rec);
+ } else {
+ return q.insert(cleanData)
+ .then(row => {
+ rec[idField] = row[0];
+ return rec;
+ });
+ }
+ },
+ /**
+ * delete one (or more) rows from the named table
+ * where the named field has the value in the passed record.
+ */
+ del(table, idField, rec) {
+ return db.query(table).where(idField, rec[idField]).del();
+>>>>>>> update-migration
+ },
+};
+
+module.exports = db;
+
+// turn the shift config into knex format
+function unpackConfig({ type, connect, debug }) {
+ return (type === 'mysql') ? {
+ client: 'mysql2',
+ debug,
+ connection: {
+ host : connect.host,
+ port : connect.port,
+ user : connect.user,
+ password : connect.pass,
+ database : connect.name,
+ // match the utf8mb4 database/server so 4-byte characters
+ // ( ex. emoji ) round-trip cleanly. re: #796
+ charset : 'utf8mb4',
+ },
+ // knex recommends setting the min pool size to 0
+ // ( for backcompat, their default is 2. )
+ // https://knexjs.org/guide/#pool
+ pool: {
+ min: 0,
+ max: 7,
+ },
+ } : (type === 'sqlite') ? {
+ client: "sqlite3",
+ debug,
+ connection: {
+ filename: connect.name, // memory or a filename
+ },
+ useNullAsDefault: true,
+ } : {
+ client: "unknown"
+ }
+}
\ No newline at end of file
diff --git a/app/endpoints/delete_event.js b/app/endpoints/delete_event.js
index 0af6cc873..daa2ecc08 100644
--- a/app/endpoints/delete_event.js
+++ b/app/endpoints/delete_event.js
@@ -1,78 +1,40 @@
/**
* Delete Event: Removes an event from the calendar.
- * Expects a JSON post with an id and password.
*
- * You can use curl to post json for testing. For example:
- * curl -k -H 'Content-Type: application/json' -X POST --data-binary \
- * "@delete_event.json" https://localhost:4443/api/delete_event.php
- * {
- * "id": "6245",
- * "secret": "example"
- * }
- *
- * If there was an error ( for example, if the id was missing or the event wasn't found )
- * returns http 400 "Bad Request" and a json error response (see util/errors.js)
+ * Expects a JSON post containing an `id` and its corresponding `secret`.
+ * Responds with either HTTP success ( 200 ) or an HTTP error
+ * and a json object containing a success or error message.
*
+ * See the CALENDAR_API.md for more details.
*/
-const config = require("../config");
-const express = require('express');
-const textError = require("../util/errors");
-const { CalEvent } = require("../models/calEvent");
-const { uploader } = require("../uploader");
+const reconcile = require('shift-docs/support/reconcile');
+const { uploader } = require('shift-docs/uploader');
+const { parseJson } = require('shift-docs/util/parse');
+const { ErrBadRequest, ErrForbidden } = require('shift-docs/util/errors');
+const handleCall = require('shift-docs/util/handleCall');
+const db = require('shift-docs/db');
// the front end sends a multi-part form post
// so... we need to handle that.
-exports.post = [ uploader.makeHandler(), handleRequest ];
+exports.post = [ uploader.makeHandler(), handleCall(postDelete) ];
-function handleRequest(req, res, next) {
- let data = req.body;
- // fix? client uploads form data containing json...
- // probably to match manage_event where its currently required.
- if (data && data.json) {
- data = safeParse(data.json);
- }
- if (!data) {
- return res.textError('JSON could not be decoded');
+function postDelete(req) {
+ const { id, secret } = parseJson(req.body);
+ if (!id) {
+ throw new ErrBadRequest("Missing id");
}
- if (!data.id) {
- return res.textError('Missing id');
+ if (!secret) {
+ throw new ErrBadRequest("Missing secret");
}
- // get the event: delete seems to send an int, where manage is a string.
- // normalize it to a string for consistency ( tbd: is that good, or even needed? )
- return CalEvent.getByID(''+data.id).then((evt) => {
- // verify the event exists.
- if (!evt) {
- return res.textError('Event not found', 404);
- }
-
- // validate the password.
- if (!evt.isSecretValid(data.secret)) {
- return res.textError('Invalid secret, use link from email', 403);
+ return db.query.transaction((tx) => {
+ return reconcile.revokeEntireSeries(tx, id, secret);
+ })
+ .then((count) => {
+ // note: doesn't distinguish between invalid secret and unknown event.
+ // think that's okay: we don't have to reveal more info about the event than needed.
+ if (!count) {
+ throw new ErrForbidden("Invalid secret, use link from email");
}
-
- // if the event was never published, we can delete it completely;
- // otherwise, soft delete it.
- let q = !evt.isPublished() ? evt.eraseEvent() : evt.softDelete();
- q.then((_) => {
- // note: the frontend currently doesn't use this json;
- // instead it looks for request success ( http 200 )
- res.set(config.api.header, config.api.version);
- res.json({
- success: true
- })
- }).catch((e) => {
- console.error("error trying to cancel an event", e);
- return res.textError('Server error');
- });
- }).catch(next);
-}
-
-// read json into a javascript object.
-// returns undefined for any error.
-function safeParse(json) {
- try {
- return JSON.parse(json);
- } catch (err) {
- console.error(err);
- }
-}
+ return { success: true };
+ });
+}
\ No newline at end of file
diff --git a/app/endpoints/events.js b/app/endpoints/events.js
index 1f3057b74..0a9401c30 100644
--- a/app/endpoints/events.js
+++ b/app/endpoints/events.js
@@ -5,7 +5,6 @@
* This endpoint supports two different queries:
* id=caldaily_id ( the time id )
* startdate=YYYY-MM-DD & enddate=YYYY-MM-DD
- * &all=true ( to include soft deleted rides )
*
* For example:
* http://localhost:3080/api/events.php?id=1893
@@ -16,8 +15,7 @@
* events: [ {...}, ... ]
* }
*
- * If there is a problem the error code will be 400 -- or 404 when an id
- * doesn't match a visible event -- with a json response of the form:
+ * Errors are returned with a json response of the form:
* {
* "error": { "message": "Error message" }
* }
@@ -26,63 +24,68 @@
* https://github.com/shift-org/shift-docs/blob/main/docs/CALENDAR_API.md#viewing-events
*/
const config = require("../config");
-const { fromYMDString, to24HourString, toYMDString } = require("../util/dateTime");
+const { ErrBadRequest, ErrNotFound, StatusError } = require('shift-docs/util/errors');
+const { toYMDString } = require("../util/dateTime");
const { CalEvent } = require("../models/calEvent");
const { CalDaily } = require("../models/calDaily");
-const { EventsRange } = require("../models/calConst");
+const { EventsRange } = require("../models/newConst");
+const handleCall = require('shift-docs/util/handleCall');
+const { parseInt, parseYmd } = require('shift-docs/util/parse');
+
+exports.get = handleCall(listEvents);
// the events endpoint:
-exports.get = function(req, res, next) {
- let id = req.query.id;
- let start = req.query.startdate;
- let end = req.query.enddate;
+function listEvents(req) {
+ const pkid = parseInt(req.query.id);
+ const start = parseYmd(req.query.startdate);
+ const end = parseYmd(req.query.enddate);
const includeAllEvents = (req.query.all === "true") || (req.query.all === "1");
- if (id && start && end) {
- res.textError("expected only an id or date range"); // fix, i think its supposed be sending a json error.
- } else if (id) {
- // return the summary of a particular daily event:
- return CalDaily.getByDailyID(id).then((daily) => {
- if (!daily) {
- // the id didn't match a visible event: not found, rather than a bad request.
- // ( getByDailyID() also returns null for hidden and delisted events. )
- res.textError("no such time", 404);
- } else {
- return getSummaries([daily]).then((events) => {
- res.set(config.api.header, config.api.version);
- res.json({
- events
- });
- });
- }
- }).catch(next);
- } else {
- // return a range of dailies between two times:
- start = fromYMDString(start);
- end = fromYMDString(end);
- if (!start.isValid() || !end.isValid()) {
- res.textError("need valid start and end times");
- } else {
- // add 1 so days in range is inclusive
- // e.g. (2025-06-10 - 2025-06-01 = 9 days difference) + 1 day = 10 day range
- const range = end.diff(start, 'day') + 1;
- if (range < 1) {
- res.textError("end date cannot be before start date", 422);
- } else if (range > EventsRange.MaxDays) {
- res.textError(`event range too large: ${range} days requested; max ${EventsRange.MaxDays} days`, 413);
- } else {
- return CalDaily.getRangeVisible(start, end, includeAllEvents).then((dailies) => {
- return getSummaries(dailies).then((events) => {
- const pagination = getPagination(start, end, events.length);
- res.set(config.api.header, config.api.version);
- res.json({
- events,
- pagination,
- });
- });
- }).catch(next);
- }
+ //
+ if (pkid && (start || end)) {
+ throw new ErrBadRequest("expected only an id or date range");
+ }
+ if (pkid && includeAllEvents) {
+ throw new ErrBadRequest("include all events only makes sense for range queries");
+ }
+ return pkid ? listDay(pkid) : listRange(start, end, includeAllEvents);
+}
+
+// promise the summary of a particular daily event
+function listDay(pkid) {
+ return CalDaily.getByDailyID(pkid).then((daily) => {
+ if (!daily) {
+ // the id didn't match a visible event: not found, rather than a bad request.
+ // ( getByDailyID() also returns null for hidden events. )
+ throw new ErrNotFound("no such time");
}
+ const events = getSummaries([daily]);
+ return {
+ events,
+ // no pagination for single events.
+ };
+ });
+}
+
+ // promise a range of dailies between two times:
+function listRange(start, end, includeAllEvents) {
+ if (!start || !end || !start.isValid() || !end.isValid()) {
+ throw new ErrBadRequest("need valid start and end times");
+ }
+ const range = end.diff(start, 'day') + 1; // add 1 to make test inclusive
+ if (range < 1) {
+ throw new StatusError(422, "end date cannot be before start date");
+ }
+ if (range > EventsRange.MaxDays) {
+ throw new StatusError(413, `event range too large: ${range} days requested; max ${EventsRange.MaxDays} days`);
}
+ return CalDaily.getDateRange(start, end, includeAllEvents).then((dailies) => {
+ const events = getSummaries(dailies);
+ const pagination = getPagination(start, end, events.length);
+ return {
+ events,
+ pagination,
+ };
+ });
}
// promise an array containing json-friendly summaries of all the passed CalDaily(s)
@@ -90,31 +93,20 @@ exports.get = function(req, res, next) {
function getSummaries(dailies) {
// a cache because multiple dailies may have the same event.
const events = new Map();
- // for all dailies:
- return Promise.all( dailies.map((at) => {
- // if this is the first time we've seen the event id:
+ return dailies.map((at) => {
+ // summarize the event the first time we see it.
if (!events.has(at.id)) {
- // go create the event and end time summary pair.
- events.set(at.id, CalEvent.getByID(at.id).then(specialSummary));
+ const evt = CalEvent.wrap(at);
+ const evtJson = evt.getSummary();
+ const endTime = evt.getEndTime(); // an invalid duration generates a null here.
+ events.set(at.id, [evtJson, endTime]);
}
- // wait till the event summary is complete then merge it with the daily:
- return events.get(at.id).then((specialSum) => {
- const [ evtJson, endTime ] = specialSum;
- return Object.assign( {}, evtJson, at.getJSON(endTime) );
- });
- }));
+ const [evtJson, endTime] = events.get(at.id);
+ return Object.assign({}, evtJson, at.getSchedule(evtJson.deleted, endTime));
+ });
}
exports.getSummaries = getSummaries;
-// pool the events so each daily doesn't separately query for its (shared) event.
-// the end time lives on the event, but needs to appear after each daily's json,
-// so it's tacked on manually below.
-function specialSummary(evt) {
- // an invalid duration generates a null here.
- const endTime = to24HourString(evt.getEndTime());
- return [ evt.getJSON(), endTime ];
-}
-
// expects days are dayjs objects
// and count is the number of events between the two
function getPagination(firstDay, lastDay, count) {
diff --git a/app/endpoints/ical.js b/app/endpoints/ical.js
index 9df4401bd..144221cfa 100644
--- a/app/endpoints/ical.js
+++ b/app/endpoints/ical.js
@@ -20,7 +20,7 @@ const wordwrap = require('wordwrapjs');
const nunjucks = require("../nunjucks");
const { CalEvent } = require("../models/calEvent");
const { CalDaily } = require("../models/calDaily");
-const { EventsRange } = require("../models/calConst");
+const { EventsRange } = require("../models/newConst");
const dt = require("../util/dateTime");
const config = require("../config");
@@ -149,8 +149,8 @@ function respondWith(cal, res, filename, events) {
// Promise a single occurrence of a single event in ical format as a string.
// id is a caldaily id.
-function buildOne(id) {
- return CalDaily.getByDailyID(id).then((daily) => {
+function buildOne(pkid) {
+ return CalDaily.getByDailyID(pkid).then((daily) => {
if (!daily) {
// an id that matches nothing is "not found", not a bad request.
return Promise.reject(expected(404, "no such event"));
@@ -175,7 +175,7 @@ function buildCurrent() {
const now = dt.getNow();
const started = now.subtract(1, 'month');
const ended = now.add(6, 'month');
- return CalDaily.getFullRange(started, ended).then((dailies) => {
+ return CalDaily.getDateRange(started, ended).then((dailies) => {
return buildEntries(dailies);
});
}
@@ -193,10 +193,7 @@ function buildRange(start, end, includeDeleted) {
if (range > EventsRange.MaxDays) {
return Promise.reject(expected(413, `event range too large: ${range} days requested; max ${EventsRange.MaxDays} days`));
}
- const q = includeDeleted?
- CalDaily.getFullRange:
- CalDaily.getRangeVisible;
- return q(start,end).then((dailies)=>{
+ return CalDaily.getDateRange(start, end, includeDeleted).then((dailies) => {
return buildEntries(dailies);
});
}
@@ -211,15 +208,13 @@ function buildRange(start, end, includeDeleted) {
function buildEntries(dailies) {
// a cache because multiple dailies may have the same event.
const events = new Map();
- // generate the array of promises:
- return Promise.all( dailies.map((at) => {
- // make a promise for this event ( if we've never see the event before )
+ return dailies.map((at) => {
if (!events.has(at.id)) {
- events.set(at.id, CalEvent.getByID(at.id));
+ events.set(at.id, CalEvent.wrap(at));
}
- // promise the entry after the event is available:
- return events.get(at.id).then(evt => buildCalEntry(evt, at));
- }));
+ const evt = events.get(at.id);
+ return buildCalEntry(evt, at);
+ });
}
/**
diff --git a/app/endpoints/manage_event.js b/app/endpoints/manage_event.js
index 2d6269613..e90d4495f 100644
--- a/app/endpoints/manage_event.js
+++ b/app/endpoints/manage_event.js
@@ -15,143 +15,120 @@
* https://localhost:4443/addevent/edit-$event_id-$secret
* /site/themes/s2b_hugo_theme/static/js/cal/addevent.js
*/
-const { CalEvent } = require("../models/calEvent");
-const { CalDaily } = require("../models/calDaily");
-const { uploader } = require("../uploader");
-const validator = require('validator');
-const dt = require("../util/dateTime");
-const config = require("../config");
-const emailer = require("../emailer");
-const nunjucks = require("../nunjucks");
-const { validateEvent } = require("../models/calEventValidator");
+const { parseJson } = require('shift-docs/util/parse');
+const { newValidator } = require('shift-docs/support/eventValidator');
+const { checkEventSecret, writeEvent, writeImage } = require('shift-docs/support/requestedEvent');
+const { ErrorCollector, FormError, ErrForbidden } = require('shift-docs/util/errors');
+const { uploader } = require('shift-docs/uploader');
+const handleCall = require('shift-docs/util/handleCall');
+const { Area, Audience } = require('shift-docs/models/newConst');
+const db = require('shift-docs/db');
+const reconcile = require('shift-docs/support/reconcile');
+const config = require('shift-docs/config');
+// for sendConfirmationEmail
+const dt = require('shift-docs/util/dateTime');
+const emailer = require('shift-docs/emailer');
+const nunjucks = require('shift-docs/nunjucks');
// read multipart (and curl) posts.
-exports.post = [ uploader.makeHandler(), handleRequest ];
+exports.post = [ uploader.makeHandler(), handleCall(handleRequest) ];
/**
- * read data the posted to this endpoint.
- * req.body contains parsed json.
- * req.file might exist if an image was uploaded.
- *
- * for things like validation errors, handleRequest() "successfully" generates a 400 status.
- * it should only reject due to things like database or programmer errors.
+ * read the data posted to this endpoint.
+ * - req.body contains parsed json.
+ * - req.file might exist if an image was uploaded.
+ * throws on error.
*/
-function handleRequest(req, res, next) {
- let input = req.body;
- // fix? the client uploads form data containing json
- // ( rather than raw json ) because multi-part forms require that.
- // a more rest-like api might use a separate put at some event/image url.
- if (input && input.json) {
- input = safeParse(input.json);
- }
- if (!input) {
- return res.textError("invalid request");
- }
- // parse for errors
- const data = validateEvent(input);
- // the client code doesnt allow organizers to upload the image for new events
- // ensuring that's the case here, simplifies the file handling (below)
- if (req.file && !data.id) {
- data.errors.addError("image", "Images can only be added to existing events.");
+async function handleRequest(req) {
+ const input = parseJson(req.body);
+ const errors = new ErrorCollector();
+ const v = newValidator(input, errors);
+ const values = v.validateInput({
+ defaultArea: Area.Portland,
+ defaultAudience: Audience.General,
+ defaultDistance: null,
+ defaultLoop: false,
+ defaultSaftey: false
+ });
+ if (!input.id) {
+ v.validateTerms();
+ if (req.file) {
+ errors.addError("image", "Images can only be added to existing events.");
+ }
}
- if (data.errors.count) {
- return res.fieldError(data.errors.getErrors());
+ if (errors.hasErrors()) {
+ throw new FormError(errors.getErrors());
}
- // find or make the event, then update it with the sent data.
- // ( depending on whether an id was submitted )
- return getOrCreateEvent(data.id, data.secret).then((evt)=> {
- if (!evt) {
- // getOrCreateEvent() returns null for an unknown id as well as a bad
- // secret; 403 covers both rather than revealing which. re: #559
- res.textError("Invalid secret, use link from email", 403);
- } else {
- // save the uploaded file (if any)
- const saveImage = !req.file ? Promise.resolve() :
- // the image gets written to disk as "id.ext"
- uploader.write( req.file, evt.id ).then(f => {
- // the image name gets stored in the db as "id-sequence.ext"
- // the sequence number needs to be different for each new image.
- // ( shift.conf strips off the sequence when the file is requested; see cache_busting.md )
- const sequence = evt.nextChange();
- evt.image = `${f.name}-${sequence}${f.ext}`;
- });
- // if the save succeeded, update the event.
- // ( errors are handled inside uploader.js )
- return saveImage.then(_ => {
- return updateEvent(evt, data.values, data.statusList)
- .then(details => {
- res.set(config.api.header, config.api.version);
- res.json(details);
- });
- });
- }
- }).catch(next);
+ return !input.id ?
+ createEvent(values) :
+ updateEvent(input, values, req.file);
}
-// if no id was specified, create a new event.
-// otherwise, the id and secret are required or this returns null.
-function getOrCreateEvent(id, secret) {
- if (!id) {
- return Promise.resolve(CalEvent.newEvent());
- } else {
- return CalEvent.getByID(id).then(evt => {
- return (evt && evt.isSecretValid(secret)) ? evt : null;
+function createEvent(values) {
+ const { event, schedule } = values;
+ return db.query.transaction((tx) => {
+ return reconcile.newEventData(tx, event, schedule);
+ }).then((next) => {
+ const { id, secret } = next;
+ return sendConfirmationEmail(id, secret, event, schedule).then((_) => {
+ return {
+ id: id.toString(),
+ // image: none
+ published: false,
+ // secret: only sent in the email
+ };
});
- }
+ });
}
-// promises the summary of the event after doing a successful update.
-function updateEvent(evt, values, statusList) {
- // if the user is saving an existing event,
- // we assume they are publishing it.
- // ( the secret has been validated in handleRequest. )
- const existed = evt.exists();
- const previouslyPublished = evt.isPublished();
-
- // NOTE: overwrites (most) fields in the event with the user's input.
- evt.updateFromJSON(values);
- if (existed) {
- evt.setPublished();
+// update all the data, or throws if it can't.
+// promises an object containing { id, published, image(url) }
+async function updateEvent(input, values, fileData) {
+ const { id, secret } = input;
+ // validate id and secret while determining publication count
+ const prevPublished = await checkEventSecret(id, secret);
+ if (prevPublished === null) {
+ // returns null for an unknown id as well as a bad
+ // secret; 403 covers both rather than revealing which. re: #559
+ throw new ErrForbidden("Invalid secret, use link from email");
}
- // we dont know whether something significant changed or not:
- // so we always have to store.
- // ( this creates the event if it didnt exist. )
- return evt.storeChange().then(() =>{
- // now that the event has been stored, and it has an id: add/remove times.
- const statusMap = new Map(statusList.map(status => [status.date, status]));
- return CalDaily.reconcile(evt, statusMap, previouslyPublished).then((dailies) => {
- // email the organizer about new events.
- // ( although we dont need to wait on the email transport, doing so catches any internal exceptions )
- let q = !existed ? sendConfirmationEmail(evt, dailies) : Promise.resolve();
- const statuses = dailies.map(at => at.getStatus());
- return q.then(_ => {
- // finally, return a summary of the CalEvent and its CalDaily(s).
- // includes private contact info ( like email, etc. )
- // ( because this is the organizer saving their event )
- return evt.getDetails(statuses, {includePrivate:true});
- });
- });
- });
+ // write the (validated) event data, updating the publication count
+ const { event, schedule } = values;
+ await writeEvent(id, event, schedule, 1 + prevPublished);
+ // then write the image data
+ const image = await (fileData && writeImage(id, fileData));
+ // return the smallest bit of data the client needs in response.
+ return {
+ id: id.toString(),
+ image: image,
+ published: true,
+ };
}
// promises a sent email
-// evt is a CalEvent; dailies is an array of CalDaily.
-function sendConfirmationEmail(evt, dailies) {
- const url = config.site.url('addevent', `edit-${evt.id}-${evt.password}`);
- const subject = `Shift2Bikes Secret URL for ${evt.title}`;
- console.debug("sending confirmation for", url);
-
- const dates = dailies
- .filter(d => !d.isDelisted())
- .map(d => dt.friendlyDate(d.eventdate))
- .join('; ');
- const time = dt.to12HourString(dt.from24HourString(evt.eventtime));
- const location = [evt.locname, evt.address].filter(Boolean).join(', ');
+// evt is the prepared table data
+function sendConfirmationEmail(id, secret, evt, schedule) {
+ // direct access to the server form:
+ // const url = config.site.url(`api/v2/events/${id}.html?secret=${secret}`);
+ // backcompat access: requires a redirect somewhere
+ const url = config.site.url('addevent', `edit-${id}-${secret}`);
+ const subject = `Shift2Bikes Secret URL for ${evt.series.title}`;
+ const logMessage = `Sending confirmation for ${url}...`;
+ if (!config.isTesting) {
+ console.info(logMessage);
+ }
+ const dates = schedule
+ .filter((d) => d.is_scheduled)
+ .map((d) => dt.friendlyDate(d.ymd))
+ .join("; ");
+ const time = dt.to12HourString(dt.from24HourString(evt.series.start_time));
+ const start = evt.location[0];
+ const location = [start.place_name, start.address].filter(Boolean).join(", ");
const support = config.email.support;
const body = nunjucks.render('email.njk', {
- organizer: evt.name,
- title: evt.title,
+ organizer: evt.series.organizer,
+ title: evt.series.title,
date: dates,
time,
location,
@@ -159,28 +136,20 @@ function sendConfirmationEmail(evt, dailies) {
help: config.site.helpPage(),
support: support.address || support, // a string or object
});
- console.debug("confirmation email body:\n", body);
+ if (!config.isTesting) {
+ console.info("confirmation email body:\n", body);
+ }
return emailer.sendMail({
subject,
text: body,
to: {
- name: evt.name,
- address: evt.email,
+ name: evt.series.organizer,
+ address: evt.private.private_email,
},
from: config.email.sender,
replyTo: config.email.support,
bcc: config.email.moderator, // backup copy for debugging and/or moderating
// html
// attachments
- }, evt.name, evt.email, evt.title, url);
-}
-
-// read json into a javascript object.
-// returns undefined for any error.
-function safeParse(json) {
- try {
- return JSON.parse(json);
- } catch (err) {
- console.error(err);
- }
+ });
}
diff --git a/app/endpoints/retrieve_event.js b/app/endpoints/retrieve_event.js
index a96f1a38e..d9329d72f 100644
--- a/app/endpoints/retrieve_event.js
+++ b/app/endpoints/retrieve_event.js
@@ -1,51 +1,44 @@
/**
- * Retrieve Event: returns the summary of an event and all of its event times.
+ * Retrieve Event: Responds with a summary of an event and all of its scheduled days.
+ *
* Currently used only for displaying a ride to its organizer so they can edit the ride.
- * Could also be used for requesting all days a particular event takes place.
- * Expects an calevent id (and optionally, its matching secret) using url query parameters.
+ * Expects an series id (and optionally, its matching secret) using url query parameters.
*
* For example:
* https://localhost:3080/api/retrieve_event.php?id=595&secret=12e1c433836d6c92431ac71f1ff6dd97
*
- * On success, returns a json summary of event.
- * If there was an error ( for example, if the id was missing or the event wasn't found )
- * returns http 400 "Bad Request" with a json error response ( see util/errors.js )
+ * On success, responds with HTTP 200, and a json summary of the event.
+ * On failure, responds with an HTTP error, abd a json error message.
*
- * See also:
- * https://github.com/shift-org/shift-docs/blob/main/docs/CALENDAR_API.md#retrieving-public-event-data
+ * See the CALENDAR_API.md for more details.
*/
-const config = require("../config");
const { CalEvent } = require("../models/calEvent");
const { CalDaily } = require("../models/calDaily");
+const { ErrBadRequest, ErrNotFound } = require('shift-docs/util/errors');
+const { parseInt, parseString } = require('shift-docs/util/parse');
+const handleCall = require('shift-docs/util/handleCall');
-exports.get = function get(req, res, next) {
- let id = req.query.id;
- let secret = req.query.secret;
+exports.get = handleCall(retrieveEvent);
+function retrieveEvent(req) {
+ const id = parseInt(req.query.id);
+ const secret = parseString(req.query.secret);
if (!id) {
- res.textError("Request incomplete, please pass an id in the url");
- } else {
- return CalEvent.getByID(id).then((evt) => {
- if (!evt) {
- res.textError("Event not found", 404);
- } else if (evt.isDeleted()) {
- res.textError("Event was deleted", 404);
- } else {
- // the php version didnt error on invalid secret;
- // so this doesnt either ( private data is only returned with a valid secret )
- const includePrivate = evt.isSecretValid(secret);
- if (!evt.isPublished() && !includePrivate) {
- // act exactly as if unpublished events don't exist
- // ( unless you know the secret )
- res.textError("Event not found", 404);
- } else {
- const statuses = CalDaily.getStatusesByEventId(evt.id);
- return evt.getDetails(statuses, {includePrivate}).then(details => {
- res.set(config.api.header, config.api.version);
- res.json(details);
- });
- }
- }
- }).catch(next);
+ throw new ErrBadRequest("Missing id");
}
+ return CalEvent.getByID(id).then((evt) => {
+ if (!evt) {
+ throw new ErrNotFound("Event not found");
+ }
+ // the php version didnt error on invalid secret;
+ // so this doesnt either ( private data is only returned with a valid secret )
+ const includePrivate = evt.isSecretValid(secret);
+ if (!evt.isPublished() && !includePrivate) {
+ // act exactly as if unpublished events don't exist
+ // ( unless you know the secret )
+ throw new ErrNotFound("Event not found");
+ }
+ const statuses = CalDaily.getStatusesByEventId(id);
+ return evt.getDetails(statuses, {includePrivate});
+ });
}
diff --git a/app/endpoints/search.js b/app/endpoints/search.js
index a6f01abda..f8fcbbca9 100644
--- a/app/endpoints/search.js
+++ b/app/endpoints/search.js
@@ -28,7 +28,7 @@
const dayjs = require("dayjs");
const config = require("../config");
const { CalDaily } = require("../models/calDaily");
-const { EventSearch } = require("../models/calConst");
+const { EventSearch } = require("../models/newConst");
const { getSummaries } = require("./events.js");
const validator = require('validator');
@@ -45,18 +45,20 @@ exports.get = function(req, res, next) {
// Search for the given search term, starting from today
const startDate = dayjs().startOf('day');
return CalDaily.getEventsBySearch(startDate, term, limit, offset, searchOldEvents).then((dailies) => {
- return getSummaries(dailies).then((events) => {
- // fullcount appears in every returned row; the same in every row.
- const fullcount = events.length ? events[0].fullcount : 0;
- res.set(config.api.header, config.api.version);
- res.json({
- events,
- pagination: {
- offset, limit, fullcount
- }
- });
+ const events = getSummaries(dailies);
+ // fullcount appears in every returned row; the same in every row.
+ const fullcount = events.length ? events[0].fullcount : 0;
+ res.set(config.api.header, config.api.version);
+ res.json({
+ events,
+ pagination: {
+ offset, limit, fullcount
+ }
});
- }).catch(next);
+ }).catch((err) => {
+ console.error("search endpoint error", err);
+ next(err);
+ });
}
}
diff --git a/app/eventimages/README.MD b/app/eventimages/README.MD
deleted file mode 100644
index 445ee54e6..000000000
--- a/app/eventimages/README.MD
+++ /dev/null
@@ -1,6 +0,0 @@
-This directory provides a place for image upload during local testing.
-( see `image.dir` in `config.js` )
-
-The bike.jpg is [creative commons licensed](https://creativecommons.org/licenses/by/2.0/) 齐健 from Peking, People's Republic of China - Down the Hutong
-https://www.flickr.com/photos/timquijano/6337789618/
-via wikipedia.
\ No newline at end of file
diff --git a/app/images/README.MD b/app/images/README.MD
new file mode 100644
index 000000000..54bed06d6
--- /dev/null
+++ b/app/images/README.MD
@@ -0,0 +1,53 @@
+This directory provides images used during development and testing.
+
+# Image Configuration
+
+The environment variable `SHIFT_IMAGE_DIR`, along with some application rules, configures where the backend reads and writes image files.
+
+## Docker
+
+The `nginx` and `node` containers both handle images. Ngnix reads existing images and sends them to users; node receives image uploads from users and writes them to disk.
+
+Nginx is hardwired ( in `shift.conf` ) to serve urls of the format `/eventimages/123.ext` from `opt/backend/eventimages/`.
+
+Docker via `docker-compose.yml` maps that to a physical location relative to the `shift` script: `./backend/eventimages`.
+
+On production it winds up as "/opt/shift-docs/backend/eventimages".
+
+For node, docker compose is similarly hardwired to expose a path to `/opt/backend/eventimages/`, and map that relative to the shift script at `./backend/eventimages/` ( see TODOs below about cleaning that up connection. )
+
+## Node Development
+
+When running node yourself -- outside of docker -- node handles not only the writing but also the reading of images.
+
+`config.js` exports an `images` member, with various fields including:
+
+* `config.image.path`: used to generate the url given to users. By default this is 'eventimages' ( ex. https://www.shift2bikes.org/eventimages/11030-8.jpg ) -- the same paths that the nginx `shift.conf` uses. This is also the config used to serve files when running without docker. The node side, but not the docker side, can be configured via the env var `SHIFT_IMAGE_PATH` ( see TODOs about cleaning that up. )
+
+* `config.image.dir`: the local directory where images are read from and written to. This is configured with
+
+When running via `npm dev` that location becomes: `./bin/eventimages`. When running via `npm test` the code uses an os generated temp directory. Docker uses `npm run` and that's described above. The node side of this can be configured via `SHIFT_IMAGE_DIR` but the docker side cannot. ( see TODOs about cleaning that up. )
+
+# Image Files
+
+The `setupEventImages` tool ensures the directory used by `npm dev` exists. It also copies the `bike.jpg` image from this directory to that directory. That image is used by events generated via the `make-fake-events` tool.
+
+# Legacy Info
+
+The `./app/eventimages` directory used to be a mix of development files and uploaded files ( the php era. ) At some point the uploaded files were moved to `./backend`, but special gitignore rules were needed to exclude legacy image files while including the dev ones. As of this writing, the dev images were finally moved to their own separate directory so `app/eventimages` could be nixed from source control completely.
+
+# TODO:
+
+cleanup environment var handling: EVENTIMAGES, SHIFT_IMAGE_DIR, and the unused SHIFT_IMAGE_PATH.
+
+1. either npm dev should initialize the `EVENTIMAGES` environment var, and use that var in config.js -- or that var should be removed from the ./shift script and hardwired in `docker-compose.yml` ( to `./backend/eventimages` )
+
+hardwiring is probably the best route for simplicity's sake. rationale: this is the directory understood by docker. it doesn't really need configuration because it doesn't need to change.
+
+however, if its kept, maybe consider changing the name from `EVENTIMAGES` to `SHIFT_IMAGE_URL` to better communicate its intent.
+
+note: there is an SHIFT_IMAGE_PATH var -- which never gets set, but which node tries to read. whatever happens to EVENTIMAGES should happen to it.
+
+2. `docker-compose.yml` has multiple references to `/opt/backend/eventimages`, it should probably use `SHIFT_IMAGE_DIR`. rationale: these contain the images on the user's local filesystem. the environment var is used by npm already -- right now they have to be manually kept inline.
+
+OR, consider a volume for docker images so that uploaded files don't appear on the user's local filesystem. BUT it would need some finagling on production: either remapping on production to keep the current location, or some way to move existing files into the volume. and, if in a volume, production would need some backup method.
diff --git a/app/eventimages/bike-bad.tiff b/app/images/bike-bad.tiff
similarity index 100%
rename from app/eventimages/bike-bad.tiff
rename to app/images/bike-bad.tiff
diff --git a/app/eventimages/bike-big.png b/app/images/bike-big.png
similarity index 100%
rename from app/eventimages/bike-big.png
rename to app/images/bike-big.png
diff --git a/app/eventimages/bike.jpg b/app/images/bike.jpg
similarity index 100%
rename from app/eventimages/bike.jpg
rename to app/images/bike.jpg
diff --git a/app/images/bike.png b/app/images/bike.png
new file mode 100644
index 000000000..778a1fb38
Binary files /dev/null and b/app/images/bike.png differ
diff --git a/app/images/index.js b/app/images/index.js
new file mode 100644
index 000000000..f0daba5cb
--- /dev/null
+++ b/app/images/index.js
@@ -0,0 +1,16 @@
+// This file exports the location of sample images for development and testing.
+//
+// The bike images are [creative commons licensed](https://creativecommons.org/licenses/by/2.0/) 齐健 from Peking, People's Republic of China - Down the Hutong
+// https://www.flickr.com/photos/timquijano/6337789618/
+// via wikipedia.
+const path = require('node:path');
+const imageDir = __dirname;
+
+module.exports = {
+ bike: {
+ jpg: path.join(imageDir, "bike.jpg"),
+ png: path.join(imageDir, "bike.png"),
+ big: path.join(imageDir, "bike-big.png"),
+ unsupportedFormat: path.join(imageDir, "bike-bad.tiff"),
+ }
+}
diff --git a/app/models/calConst.js b/app/models/calConst.js
deleted file mode 100644
index c635c8a16..000000000
--- a/app/models/calConst.js
+++ /dev/null
@@ -1,61 +0,0 @@
-// CalEvent.review enum.
-// see helpers.js for the authoritative version.
-// fix: how can we share? symlink? some sort of copy by hugo build? remote fetch and make the api server authoritative??
-const Area = Object.freeze({
- Portland : 'P',
- Vancouver : 'V',
- //
- Westside : 'W',
- Eastside : 'E',
- Clackamas: 'C',
-});
-
-const Audience = Object.freeze({
- General : 'G',
- Family : 'F',
- Adult : 'A',
-});
-
-// legacy CalEvent.datestype enum.
-// all dates are now "one day"
-const DatesType = Object.freeze({
- OneDay : 'O',
- Scattered : 'S',
- Consecutive : 'C',
-});
-
-// CalDaily.eventstatus enum.
-// the status of a single instance of an event on a given day.
-const EventStatus = Object.freeze({
- Active : 'A',
- Cancelled : 'C', // explicitly marked as canceled by the owner.
- Delisted : 'D', // the day has been removed without being explicitly canceled.
- Skipped : 'S', // legacy value.
-});
-
-// CalEvent.review enum.
-const Review = Object.freeze({
- Inspect : 'I',
- Excluded : 'E', // legacy; reused for "soft deleting" already published events.
- Approved : 'A',
- SentEmail : 'S',
- Revised : 'R',
-});
-
-const RideLength = Object.freeze({
- '0-3' : '0-3 miles',
- '3-8' : '3-8 miles',
- '8-15' : '8-15 miles',
- '15+' : '15+ miles',
-});
-
-const EventsRange = Object.freeze({
- MaxDays : 100,
-});
-
-const EventSearch = Object.freeze({
- Limit : 25,
-});
-
-//
-module.exports = { Area, Audience, DatesType, EventStatus, Review, RideLength, EventsRange, EventSearch};
diff --git a/app/models/calDaily.js b/app/models/calDaily.js
index cdb8c1641..5b1b4b589 100644
--- a/app/models/calDaily.js
+++ b/app/models/calDaily.js
@@ -1,79 +1,11 @@
-const knex = require("../db");
-const config = require("../config");
-const dt = require("../util/dateTime");
-const { EventStatus, Review, EventSearch } = require("./calConst");
+const knex = require('shift-docs/db');
+const config = require('shift-docs/config');
+const dt = require('../util/dateTime');
+const { EventStatus } = require('shift-docs/models/newConst');
+
// helper to add methods to a returned database object.
const methods = {
- // was this record read from the database?
- exists() {
- return !!this.pkid;
- },
-
- // store to the db if force is true.
- // promises this object when done.
- _store(force= true) {
- return force ? knex.store('caldaily', 'pkid', this) : Promise.resolve(this);
- },
-
- // Remove this record from the db.
- // promises this object when done.
- // WARNING: should really only call this for unpublished events
- // otherwise the ical clients might not see the change in the event.
- eraseOccurrence() {
- return knex.del('caldaily', 'pkid', this);
- },
-
- // Mark the day as having been removed from the calendar, and update the db.
- // Promises "this" after storing the change.
- delistOccurrence() {
- let changed = false;
- if (this.eventstatus !== EventStatus.Delisted) {
- this.eventstatus = EventStatus.Delisted;
- changed = true;
- }
- return this._store(changed).then(_ => this);
- },
-
- // store the status and newflash if they changed.
- // promises this after storing the change.
- // note: empty strings are stored as null, to stay consistent with existing data.
- _updateStatus(dateStatus) {
- let changed = false;
- const newStatus = dateStatus.status || null;
- if (this.eventstatus !== newStatus) {
- this.eventstatus = newStatus;
- changed = true;
- }
- const newsFlash = dateStatus.newsflash || null;
- if (this.newsflash !== newsFlash) {
- this.newsflash = newsFlash;
- changed = true;
- }
- return this._store(changed).then(_ => this);
- },
-
- // returns a date in YYYY-MM-DD format ( ex. 2006-01-02 )
- getFormattedDate() {
- // note: dates are returned by the mysql2 driver as a json Date.
- return dt.toYMDString( this.eventdate );
- },
-
- // return an object containing: {
- // id: CalDaily primary key.
- // date: YYYY-MM-DD ( ex. 2006-01-02 )
- // status: a single letter: 'A' for active, or 'C' for cancelled.
- // newsflash: a special bit of text from the user, typically for canceled or rescheduled events.
- // }
- // note: used by the "manage" and "retrieve" endpoints.
- getStatus() {
- return {
- id : this.pkid.toString(),
- date : this.getFormattedDate(),
- status : this.eventstatus,
- newsflash : this.newsflash
- };
- },
// return a url which provides a view of this particular occurrence.
// ex. https://localhost:4443/calendar/event-13662
@@ -87,43 +19,42 @@ const methods = {
return config.site.url("api", `ics.php?event_id=${this.pkid}`);
},
- // return true if the occurrence has been removed from the calendar; false otherwise.
- // ( differentiates between explicitly canceled, and no longer scheduled. )
isDelisted() {
- return this.eventstatus == EventStatus.Delisted;
+ return this.eventstatus === EventStatus.Delisted.toString();
+ },
+
+ isCancelled() {
+ return this.eventstatus === EventStatus.Cancelled.toString();
},
- // return true if the occurrence has been cancelled or delisted;
- // false if confirmed.
- // fix: an isConfirmed() would make more sense.
isUnscheduled() {
- return (this.eventstatus == EventStatus.Cancelled) ||
- (this.eventstatus == EventStatus.Delisted);
+ return this.isDelisted() || this.isCancelled();
},
getNewsFlash() {
- return !this.isDelisted() ? this.newsflash : null
+ return !this.isDelisted() ? this.newsflash : null;
},
// return a summary of this occurrence for the "events" endpoint.
// backcompat: include the endtime if specified.
- getJSON(endtime) {
- let data = {
- date: this.getFormattedDate(),
+ getSchedule(deleted, endtime) {
+ const data = {
+ date: dt.toYMDString(this.eventdate),
caldaily_id: this.pkid.toString(),
shareable: this.getShareable(),
exportable: this.getExportable(),
- cancelled: this.isUnscheduled(), // better would have been "scheduled:true"
- // don't send newsflash when delisted:
- // it's not scheduled and may be deleted
- // either way, its not info we want to show.
- newsflash: this.getNewsFlash(),
- status: this.eventstatus,
- fullcount: this.fullcount, // Full count of results for pagination
+ // if deleted, report as cancelled.
+ cancelled: !deleted ? this.isUnscheduled() : true,
+ // if deleted, report without news
+ newsflash: !deleted ? this.getNewsFlash() : null,
+ // if deleted, report as delisted
+ status: !deleted ? this.eventstatus : EventStatus.Delisted,
+ // Full count of results for pagination
+ fullcount: this.fullcount,
};
- // see notes in CalEvent.getJSON()
+ // see notes in CalEvent.getSummary()
if (endtime !== undefined) {
- data.endtime = endtime;
+ data.endtime = dt.to24HourString(endtime);
}
return data;
}
@@ -134,159 +65,103 @@ function addMethods(res) {
}
class CalDaily {
- // promise a new occurrence of an existing event in the database.
- // dateStatus['date'] is YYYY-MM-DD
- static createNewEventDaily(evt, dateStatus) {
- if (!evt || !evt.id) {
- throw new Error("daily requires a valid event id");
- }
- const eventdate = dt.fromYMDString(dateStatus && dateStatus.date);
- if (!eventdate.isValid()) {
- throw new Error("daily requires a valid YMD string");
- }
- const at = addMethods({
- id: evt.id,
- // for the sake of the sqlite driver, convert to a javascript date manually
- // the mysql driver does this automatically.
- eventdate: knex.toDate(eventdate),
- // defaults here are mainly to simplify testing
- // in theory, the client always specifies them
- eventstatus: dateStatus.status || EventStatus.Active,
- newsflash: dateStatus.newsflash || null,
- });
- // store the new daily.
- return at._store();
- }
- // promises one CalDaily from the db only for tests.
- static getForTesting(pkid) {
- return knex
- .query('caldaily')
- .where('pkid', pkid)
- .first()
- .then(function(at) {
- return at? addMethods(at): null;
- });
- }
-
-
- // promises one CalDaily but only for published Events.
- // yields null if not found or not published.
- // ( this is the php EventTime::getByID )
+ // promises one day of a published event
+ // pkid is the id of the scheduled day
+ // yields null if not found ( or not published. )
static getByDailyID(pkid) {
return knex
- .query('caldaily')
- .join('calevent', 'caldaily.id', 'calevent.id') // join for hidden test.
+ .query('view_event_schedule')
.where('pkid', pkid)
- .whereRaw('not coalesce(hidden, 0)') // zero when published; null for legacy events.
- .whereNot('eventstatus', EventStatus.Delisted)
.first()
.then(function(at) {
return at? addMethods(at): null;
});
}
- // promises an array of CalDaily(s).
- // aka. the php buildEventTime('id')
+ // promises an array of CalDaily(s) for a published event.
+ // ex. the ical view of all days from an entire event series.
static getByEventID(id) {
return knex
- .query('caldaily')
+ .query('view_event_schedule')
.where('id', id)
.then(function(dailies) {
return dailies.map(at => addMethods(at));
});
}
- // promise an array of cal daily statuses
- // ( similar to the php getEventDateStatuses() )
+ // promises an array of cal daily objects for editing events.
+ // including events which haven't been published yet.
+ // revoked events can't be edited: they shouldn't use this function.
static getStatusesByEventId(id) {
- return CalDaily.getByEventID(id).then((dailies) => {
- // fix: rather than filtering here, add a filter to getByEventId
- // trying to keep it somewhat like the php right now.
- return dailies.filter(at=> !at.isDelisted()).map(at => at.getStatus());
- });
- }
-
- // Promises all occurrences of any CalDaily within the specified date range....
- // including excluded and delisted ones. ( see also: getRangeVisible )
- // Days are datejs objects.
- static getFullRange(firstDay, lastDay) {
+ // return objects containing: {
+ // id: CalDaily primary key.
+ // date: YYYY-MM-DD ( ex. 2006-01-02 )
+ // status: a single letter: 'A' for active, or 'C' for cancelled.
+ // newsflash: a special bit of text from the user, typically for canceled or rescheduled events.
return knex
- .query('caldaily')
- .join('calevent', 'caldaily.id', 'calevent.id')
- .whereRaw('not coalesce(hidden, 0)') // zero when published; null for legacy events.
- .where('eventdate', '>=', knex.toDate(firstDay))
- .where('eventdate', '<=', knex.toDate(lastDay))
+ .query('view_all_days')
+ .where('id', id)
+ .whereNot('eventstatus', 'D')
.orderBy('eventdate')
- .orderBy('eventtime')
- .then(function(dailies) {
- return dailies.map(at => addMethods(at));
- });
+ // .orderBy('eventtime') - all days have the same time
+ .then(dailies => dailies.map(at => {
+ return {
+ id: at.pkid.toString(),
+ date: dt.toYMDString(at.eventdate),
+ status: at.eventstatus,
+ newsflash : at.newsflash
+ };
+ }));
}
- // Promises all occurrences of any scheduled CalDaily within the specified date range.
- // Days are datejs objects.
- static getRangeVisible(firstDay, lastDay, includeAllEvents=false) {
+ // Promises CalDaily objects for all occurrences of
+ // any published events within the specified date range.
+ // The passed dates are datejs objects.
+ static getDateRange(firstDay, lastDay, includeAllEvents = false) {
+ const min = dt.toYMDString(firstDay);
+ const max = dt.toYMDString(lastDay);
+ // fix? a manual join because view_event_schedule excludes deleted days
+ // maybe adjust view_event_schedule and the other queries instead;
+ // or add change this query into its own view.
return knex
- .query('caldaily')
- .join('calevent', 'caldaily.id', 'calevent.id')
- .whereRaw('not coalesce(hidden, 0)') // calevent: zero when published; null for legacy events.
- .where(function(q) {
+ .query('view_published_series')
+ // ie. using(id) -- but knex doesn't support that.
+ .join('view_all_days', 'view_all_days.id', '=', 'view_published_series.id')
+ .where(q => {
if (!includeAllEvents) {
- // calevent: a legacy code; reused for soft-delete
- q.whereNot('review', Review.Excluded)
- // caldaily: for deselected days; soft-deleted days are also deselected.
- q.whereNot('eventstatus', EventStatus.Delisted)
+ // exclude deleted events delisted days
+ q.whereNot('deleted', 1).whereNot('eventstatus', 'D');
}
- // the normal behavior is to not show "delisted days":
- // those are days deselected by an organizer on the calendar widget
- // but not explicitly canceled.
- //
- // enabling this block hides "delisted days" when includingDeleted events
- // commenting out this block shows "delisted days" when includingDeleted events.
- // else {
- // q.whereNot('eventstatus', EventStatus.Delisted)
- // .orWhere('eventstatus', EventStatus.Delisted)
- // .andWhere('review', Review.Excluded)
})
- .whereNot('eventstatus', EventStatus.Skipped) // caldaily: a legacy status code.
- .where('eventdate', '>=', knex.toDate(firstDay)) // caldaily: instance of the event.
- .where('eventdate', '<=', knex.toDate(lastDay))
+ .where('eventdate', '>=', min)
+ .where('eventdate', '<=', max)
.orderBy('eventdate')
.orderBy('eventtime')
- .then(function(dailies) {
- return dailies.map(at => addMethods(at));
- });
+ .then(dailies => dailies.map(addMethods));
}
- // Promises all occurrences of any scheduled CalDaily within the specified date range.
- // Days are datejs objects.
- static getEventsBySearch(firstDay, term, limit, offset, searchOldEvents=false) {
- let query = knex.query('caldaily')
+
+ // Promises CalDaily objects for all occurrences of
+ // any published events matching the passed search terms.
+ static getEventsBySearch(firstDay, term, limit, offset, searchOldEvents = false) {
+ const query = knex.query('view_event_schedule')
.column(knex.query.raw('*, COUNT(*) OVER() AS fullcount')) // COUNT OVER is our pagination hack
- .join('calevent', 'caldaily.id', 'calevent.id')
- .whereRaw('not coalesce(hidden, 0)')
.where(function(q) {
q.where('title', 'LIKE', `%${term}%`)
.orWhere('descr', 'LIKE', `%${term}%`)
.orWhere('name', 'LIKE', `%${term}%`);
})
- // .whereRaw("title like '%??%'", [term]) // late binding xperiment
- .where(function(q) {
- q.whereNot('review', Review.Excluded)
- q.whereNot('eventstatus', EventStatus.Delisted)
- })
- .whereNot('eventstatus', EventStatus.Skipped)
.where(function(q) {
// If we're NOT searching for old events, aka future search, look today and greater.
if (!searchOldEvents) {
- q.where('eventdate', '>=', knex.toDate(firstDay))
+ q.where('eventdate', '>=', dt.toYMDString(firstDay))
}
})
.where(function(q) {
// If we ARE searching for old events, only show old events, including today's events. [#1004]
// Decision stardate 2025-11-03 by JP; AK
if (searchOldEvents) {
- q.where('eventdate', '<=', knex.toDate(firstDay))
+ q.where('eventdate', '<=', dt.toYMDString(firstDay))
}
})
// If we are searching old events, eventdata claus never occurs and we add a desc to the ordering.
@@ -300,74 +175,26 @@ class CalDaily {
return rows.map(at => addMethods(at));
});
}
- // Promises all occurrences of any scheduled CalDaily within the specified date range.
- // Days are datejs objects.
+
+ // Promises an object containing the number of scheduled events within the specified date range.
+ // The object contains { total, past, upcoming } where past and upcoming are relative to the current date and time.
+ // The passed dates are datejs objects.
static getEventsCount(startDate, endDate) {
- const currDate = knex.currentDateString()
- const query = knex.query('caldaily')
+ const currDate = dt.toYMDString();
+ const query = knex.query('view_event_schedule')
.column(knex.query.raw('COUNT(*) as total'))
- .column(knex.query.raw(`COUNT(CASE WHEN eventdate < ${currDate} THEN 1 END) AS past`))
- .column(knex.query.raw(`COUNT(CASE WHEN eventdate >= ${currDate} THEN 1 END) AS upcoming`))
- .join('calevent', 'caldaily.id', 'calevent.id')
- .whereRaw('not coalesce(hidden, 0)')
+ .column(knex.query.raw('COUNT(CASE WHEN eventdate < ? THEN 1 END) AS past', [currDate]))
+ .column(knex.query.raw('COUNT(CASE WHEN eventdate >= ? THEN 1 END) AS upcoming', [currDate]))
.where(function(q) {
- q.whereNot('review', Review.Excluded)
- q.whereNot('eventstatus', EventStatus.Delisted)
- q.whereNot('eventstatus', EventStatus.Skipped)
- q.whereNot('eventstatus', EventStatus.Cancelled)
+ q.whereNot('eventstatus', EventStatus.Cancelled.toString())
})
.where(function(q) {
- q.where('eventdate', '>=', knex.toDate(startDate))
- q.where('eventdate', '<=', knex.toDate(endDate))
+ q.where('eventdate', '>=', dt.toYMDString(startDate))
+ q.where('eventdate', '<=', dt.toYMDString(endDate))
}).first();
// console.log(query.toSQL().toNative());
return query;
}
- /**
- * Add, cancel, and update occurrences of a particular event.
- *
- * @param event the relevant event.
- * @param statusMap a js Map containing {YYYY-MM-DD: dateStatus }
- * @return the promise of valid CalDaily(s)
- *
- * @see: dateStatus.js, manage_event.js
- */
- static reconcile(evt, statusMap, softDelete = true) {
- return CalDaily.getByEventID(evt.id).then((dailies) => {
- const promises = []; // our promised CalDaily(s)
- const skips = []; // promises we wait on, but dont return.
- dailies.forEach((at)=> {
- // the map is keyed by date string:
- const date = at.getFormattedDate();
- const status = statusMap.get(date);
- // if our event time is still desired by the organizer:
- // update the status with whatever the organizer provided.
- // otherwise: the organizer wants to remove the occurrence.
- // so cancel or delete the time depending on whether the event is published.
- if (status) {
- const after = at._updateStatus(status); // automatically calls store()
- promises.push( after );
- statusMap.delete( date ); // remove from map, so we dont create it (below)
- } else if (softDelete) {
- const after = at.delistOccurrence(); // automatically calls store()
- skips.push( after ); // not in the map, so dont have to remove.
- } else {
- const after = at.eraseOccurrence(); // also not in the map, ...
- skips.push( after );
- }
- });
- // create any (new) days the organizer requested:
- statusMap.forEach((status)=> {
- const at = CalDaily.createNewEventDaily(evt, status);
- promises.push( at );
- });
- // wait till any removals are completed
- // then return the promised statuses
- return Promise.all(skips).then(() => {
- return Promise.all(promises);
- });
- });
- }
// for testing:
static wrap(res) {
diff --git a/app/models/calEvent.js b/app/models/calEvent.js
index d004edfca..6bd3f40dc 100644
--- a/app/models/calEvent.js
+++ b/app/models/calEvent.js
@@ -1,20 +1,26 @@
-const crypto = require("crypto");
-const knex = require("../db");
-const { CalDaily } = require("./calDaily");
-const { Review } = require("./calConst");
+const knex = require('shift-docs/db');
const dt = require("../util/dateTime");
-const config = require("../config");
+const config = require('shift-docs/config');
+
+// when allowNull is true, this can return null if there is no tinytitle;
+// when false, this will generate a tinytitle from the real title.
+// this provides compatibility with the original code which would store the generated tinytitle:
+// the v2 tables only store a tinytitle if the organizer has specified one.
+function getTinyTitle(evt, allowNull) {
+ return (evt.tinytitle || allowNull) ?
+ evt.tinytitle : (evt.title || "").substring(0, 24).trim();
+}
// helper to add methods to a returned database object.
const methods = {
// aka Event::toArray in php
- getJSON({includePrivate}= {}) {
+ getSummary({ includePrivate } = {}) {
let duration = this.eventduration;
if (duration <= 0) {
duration = null;
}
- return this.isDeleted() ? {
+ return this.deleted ? {
id : this.id.toString(),
deleted : true,
} : {
@@ -46,9 +52,11 @@ const methods = {
// and that seems okay.
image : config.image.url(this.image),
audience : this.audience,
- tinytitle : this.tinytitle,
+ // when editing, includePrivate is true, and tinytitle can be null
+ // when displaying, includePrivate is false, and tinytitle is generated from title if needed,
+ tinytitle : getTinyTitle(this, includePrivate),
printdescr : this.printdescr,
- datestype : this.datestype,
+ datestype : "O", // doesn't exist in data any more; can doubtless be removed.
area : this.area,
featured : !!this.highlight, // false if never set ( null )
printemail : !!this.printemail, // false if never set ( null )
@@ -57,7 +65,9 @@ const methods = {
printcontact: !!this.printcontact, // false if never set ( null )
published : this.isPublished(),
safetyplan : !!this.safetyplan, // false if never set ( null )
- // note: (null==0) is false, so this wont include email, etc. by default.
+
+ // these checks are kept for extra safety.
+ // the queries themselves only exposes private info when allowed by the author.
email: (this.hideemail == 0 || includePrivate) ? this.email : null,
phone: (this.hidephone == 0 || includePrivate) ? this.phone : null,
contact: (this.hidecontact == 0 || includePrivate) ? this.contact : null,
@@ -66,18 +76,6 @@ const methods = {
// for now, therefore this is in CalDaily
// endtime: getEndTime()
};
-
- },
-
- // similar to "fromArray" in php
- updateFromJSON(data) {
- // ugly: assumes the data is validated and adjusted already.
- Object.assign(this, data);
-
- // default highlight to zero; but if it's already set, leave as-is
- // fix: add a default to mysql? could there be db entries with null in there already
- // and why is this happening in "updateFromJSON"?
- this.highlight = this.highlight ?? 0;
},
// return the starting time as a dayjs object;
@@ -111,52 +109,18 @@ const methods = {
return endTime = (len > 0) ? start.add(len, 'minute') : start.add(1, 'hour');
},
- // remove this record and any associated caldaily(s) from the database.
- // promises the total number of erased items when done.
- // NOTE: prefer softDelete() so ical subscribers can see that something has changed.
- eraseEvent() {
- return knex.del('calevent', 'id', this).then((acnt) => {
- return knex.del('caldaily', 'id', this).then((bcnt) => {
- return acnt + bcnt;
- });
- });
- },
-
- // cancel all occurrences of this event, and make it inaccessible to the organizer.
- softDelete() {
- return CalDaily.getByEventID(this.id).then((dailies) => {
- return Promise.all( dailies.map(at => at.delistOccurrence()) ).then(()=> {
- this.review = Review.Excluded; // excludes from getRangeVisible
- this.password = ""; // hides it from future editing
- return this.storeChange();
- });
- });
- },
-
// promise a summary of the CalEvent and all its CalDaily(s)
// in the php version, the statuses are optional; it's cleaner here to require them.
getDetails(statuses, {includePrivate} = {}) {
// we either have actual times or promises of them:
return Promise.resolve(statuses).then((statuses) => {
- const details = this.getJSON({includePrivate});
+ const details = this.getSummary({includePrivate});
details['datestatuses']= statuses;
return details;
});
},
- // was this record read from the database?
- exists() {
- return !!this.id;
- },
-
- // store to the db if force is true.
- // promises this object when done.
- _store(force= true) {
- return force ? knex.store('calevent', 'id', this) : Promise.resolve(this);
- },
-
// if the secret is valid and matches the password of this CalEvent.
- // ( the password of the event is set at creation time, and cleared when 'deleted' )
isSecretValid(secret) {
return secret && (secret === this.password);
},
@@ -166,33 +130,6 @@ const methods = {
// note: legacy events have null for the hidden field
// zero and null are considered published.
return !this.hidden;
- },
-
- // make this event visible to all
- // requires a call to storeChange()
- setPublished() {
- this.hidden = 0;
- },
-
- // soft deleted events are marked as 'E'
- // that makes them inaccessible to the front-end
- // while still showing them on the ical feed ( as canceled. )
- isDeleted() {
- return this.review == Review.Excluded;
- },
-
- // store to the db, updating the change counter.
- // the counter helps subscribers to the ical feed detect changes.
- // promises to return this record with a valid id.
- storeChange() {
- // update the change counter
- this.changes = this.nextChange();
- return this._store();
- },
-
- // return the change counter of the next call to store()
- nextChange() { // watch out for if it never existed.
- return 1 + (this.changes || 0);
}
};
@@ -201,22 +138,17 @@ function addMethods(res) {
}
class CalEvent {
- // promises one CalEvent ( null if not found. )
- // tbd: could also fail on not found, but the code seems to read better this way.
+ // promises the *private* data of an event series
+ // or null, if no such event was found
+ // revoked rides are excluded via this query.
static getByID(id) {
- return knex.query('calevent').where('id', id).first().
- then(function(evt) {
+ return knex.query('view_private_events')
+ .where('id', id)
+ .first()
+ .then(function(evt) {
return evt ? addMethods(evt) : null;
});
}
- // returns one event.
- static newEvent() {
- // uuid4 is 36 chars including hyphens 123e4567-e89b-12d3-a456-426614174000
- // the secret format has been 32 chars no hyphens.
- const password = crypto.randomUUID().replaceAll("-","")
- const hidden = 1; // fix: change sql default?
- return addMethods({ password, hidden });
- }
// for testing:
static wrap(res) {
return addMethods(res);
diff --git a/app/models/calEventValidator.js b/app/models/calEventValidator.js
deleted file mode 100644
index 201a16ee3..000000000
--- a/app/models/calEventValidator.js
+++ /dev/null
@@ -1,267 +0,0 @@
-const dt = require("../util/dateTime");
-const validator = require('validator');
-const { Area, Audience, DatesType, RideLength } = require("./calConst");
-
-class ErrorCollector {
- constructor() {
- this.errors = {};
- this.count = 0;
- }
- addError(field, msg) {
- this.errors[field] = msg ?? `Please enter a value for ${field}`;
- this.count++;
- }
- getErrors() {
- return this.errors;
- }
-}
-
-// ensure the email, title, etc. submitted by the organizer seem valid.
-// input is json, errors is of type ErrorCollector
-function makeValidator(input, errors) {
- // the validator package requires strings and only strings
- // the various functions below convert strings to the desired output types.
- function getString(field) {
- // coalecese nulls and undefined into a blank string
- // ensure all numbers and booleans are strings
- // trim all strings
- return ((input[field] ?? '') + '').trim();
- }
- function smallerThan(str, field, maxLen) {
- const okay = validator.isByteLength(str, { min: 0, max: maxLen });
- if (!okay) {
- errors.addError(field, "Field is too long");
- }
- return okay;
- }
- return {
- requireString(field, msg, maxLen = 255) {
- const str = getString(field);
- if (validator.isEmpty(str)) {
- errors.addError(field, msg);
- } else if (smallerThan(str, field, maxLen)) {
- return str;
- }
- },
- // the real valid email spec is 320;
- // our db only supports 255
- requireEmail(field, maxLen = 255) {
- const str = getString(field);
- if (validator.isEmpty(str)) {
- errors.addError('email', "Email missing");
- } else if (!smallerThan(str, field, maxLen)) {
- // already set errors
- } else if (!validator.isEmail(str)) {
- errors.addError('email', "Email is invalid");
- } else {
- return str; // write the trimmed value.
- }
- },
- // only checks the value; doesnt return anything
- requireTrue(field, msg) {
- const str = getString(field);
- // is the boolean value missing?
- if (!validator.isBoolean(str)) {
- errors.addError(field, msg);
- } else {
- // is the boolean value false?
- if (!validator.toBoolean(str)) {
- errors.addError(field, msg);
- }
- }
- },
- // validate the event time
- // the php doesnt do this, but it feels like a good idea.
- // ( and CalEvent.updateFromJSON() relies on it. )
- requiredTime(field) {
- // interestingly: the upload is in AM/PM style
- // but the server stores and reports in 24 hour style.
- // mysql stores 'time' fields as 'hh:mm:ss' with no meridian,
- // so we convert to 24 hour here.
- // https://dev.mysql.com/doc/refman/8.0/en/time.html
- const str = getString(field);
- if (validator.isEmpty(str)) {
- errors.addError('time');
- } else {
- let t = dt.from12HourString(str);
- if (!t.isValid()) {
- t = dt.from24HourString(str);
- }
- if (!t.isValid()) {
- errors.addError('time');
- } else {
- return dt.to24HourString(t);
- }
- }
- },
- // empty strings are converted to null, to stay consistent with existing data.
- nullString(field, maxLen = 255) {
- const str = getString(field);
- if (validator.isEmpty(str)) {
- return null;
- } else if (smallerThan(str, field, maxLen)) {
- return str;
- }
- },
- // if not specified, returns 0
- // otherwise expects 0, 1, true, or false
- // then returns a 0 or 1 value
- optionalFlag(field) {
- const str = getString(field);
- if (validator.isEmpty(str)) {
- return 0;
- } else if (!validator.isBoolean(str)) {
- errors.addError(field);
- } else {
- return validator.toBoolean(str) ? 1 : 0;
- }
- },
- // requires a 0, 1, true, or false value; empty is an error
- requiredFlag(field, msg) {
- const str = getString(field);
- if (validator.isEmpty(str)) {
- errors.addError(field, msg);
- } else if (!validator.isBoolean(str)) {
- errors.addError(field);
- } else {
- return validator.toBoolean(str) ? 1 : 0;
- }
- },
- // if not specified, returns 0
- // otherwise expects an int-like value
- // greater than or equal to 0
- zeroInt(field) {
- const str = getString(field);
- if (validator.isEmpty(str)) {
- return 0;
- } else if (!validator.isInt(str)) {
- errors.addError(field);
- } else {
- // validator returns NaN if it cant convert
- const val = validator.toInt(str);
- if (isNaN(val) || val < 0) {
- errors.addError(field);
- } else {
- return val;
- }
- }
- },
- // if not specified, returns the defaultVal
- // otherwise must be a single letter
- optionalChar(field, defaultVal) {
- const str = getString(field);
- if (validator.isEmpty(str)) {
- return defaultVal;
- } else if (!validator.matches(str, /^[A-Z]$/)) {
- errors.addError(field);
- } else {
- return str;
- }
- },
- // munge print title:
- // if print title (aka tinytitle) isn't set,
- // use the first 24 chars of the regular title
- mungeTinyTitle(title) {
- const str = getString('tinytitle');
- // fix? cut at words? ( could use the wordwrapjs )
- return (validator.isEmpty(str) && title) ? title.substring(0, 24) : str;
- },
-
- /**
- * Ensures that the 'datestatuses' in 'data' (if any) are valid.
- * Allows an empty list ( which cancels all existing occurrences. )
- *
- * @param statusList:A list of data status objects sent by the organizer.
- * [{ id, date, status, newsflash }, ...]
- */
- validateStatus(statusList) {
- const invalidDateStrings = [];
- const validStatus = [];
- if (statusList) {
- if (!Array.isArray(statusList)) {
- invalidDateStrings.push("expected an array");
- } else {
- statusList.forEach(status => {
- const validDate = status.date && dt.fromYMDString(status.date).isValid();
- if (!validDate) {
- invalidDateStrings.push(status.date);
- } else {
- validStatus.push(status);
- }
- });
- }
- }
- if (invalidDateStrings.length) {
- const msg = "Invalid dates: " + invalidDateStrings.join(', ');
- errors.addError('dates', msg);
- }
- return validStatus;
- },
-
- // if not specified ( or not one of the known lengths ) returns null.
- validateRideLength(field) {
- const value = getString(field);
- return Object.hasOwn(RideLength, value) ? value : null;
- },
- };
-}
-
-
-function validateEvent(input) {
- const errors = new ErrorCollector();
- const v = makeValidator(input, errors);
- // these don't get stored; but are still required on initial submission for a new event
- if (!input.id) {
- v.requireTrue('code_of_conduct', "You must agree to the Code of Conduct");
- v.requireTrue('read_comic', "You must have read the Ride Leading Comic");
- }
- const title = v.requireString('title', 'Title missing');
- let values = {
- title: title,
- locname: v.requireString('venue', 'Venue missing'),
- address: v.requireString('address', 'Address missing'),
- name: v.requireString('organizer', 'Organizer missing'),
- email: v.requireEmail('email'),
- hideemail: v.requiredFlag('hideemail', 'Please select whether or not to publish your email address'),
- phone: v.nullString('phone'),
- hidephone: v.optionalFlag('hidephone'),
- contact: v.nullString('contact'),
- hidecontact: v.optionalFlag('hidecontact'),
- descr: v.requireString('details', 'Details missing', 16*1024),
- eventtime: v.requiredTime('time'),
- timedetails: v.nullString('timedetails'),
- locdetails: v.nullString('locdetails'),
- loopride: v.optionalFlag('loopride'),
- locend: v.nullString('locend'),
- ridelength: v.validateRideLength('ridelength'),
- eventduration: v.zeroInt('eventduration'),
- weburl: v.nullString('weburl', 512), // fix? validate this is url-like?
- webname: v.nullString('webname'),
- audience: v.optionalChar('audience', Audience.General),
- tinytitle: v.mungeTinyTitle(title),
- printdescr: v.nullString('printdescr', 1024),
- dates: v.nullString('datestring'), // string field 'dates' needed for legacy admin calendar
- datestype: v.optionalChar('datestype', DatesType.OneDay),
- area: v.optionalChar('area', Area.Portland),
- printemail: v.optionalFlag('printemail'),
- printphone: v.optionalFlag('printphone'),
- printweburl: v.optionalFlag('printweburl'),
- printcontact: v.optionalFlag('printcontact'),
- safetyplan: v.optionalFlag('safetyplan'),
- };
- const statusList = v.validateStatus(input.datestatuses);
- return {
- id: input.id,
- secret: input.secret,
- values,
- statusList,
- errors,
- };
-}
-
-module.exports = {
- validateEvent,
- // exported for testing:
- makeValidator,
- ErrorCollector
-}
diff --git a/app/models/newConst.js b/app/models/newConst.js
index 8745af73e..405c07a5f 100644
--- a/app/models/newConst.js
+++ b/app/models/newConst.js
@@ -4,43 +4,48 @@
const { Shorthand } = require('shift-docs/util/shorthand.js');
// example usage:
-// Area.Portland.key = "P" - sent b/t client and server.
-// Area.Portland.label = "Portland" - used for display and logging.
-// Area.Portland.value = "Portland" - what it looks like in the database.
+// Area.Portland.key = "P" - sent b/t client and server.
+// Area.Portland.value = "Portland" - stored in the db.
+// Area.Portland.label = "Portland" - displayed to the user ( and for logging. )
class Area {
+ // key is first letter; the first word is both the value and label.
static Portland = Shorthand.Enum('Portland');
static Clackamas = Shorthand.Enum('Clackamas');
static Eastside = Shorthand.Enum('Eastside');
static Vancouver = Shorthand.Enum('Vancouver');
static Westside = Shorthand.Enum('Westside');
- // ex. transform 'P' into "Portland"
+ // ex. transform 'P' into the Portland Shorthand object.
static findShorthand(key) { return Shorthand.findShorthand(Area, key); }
}
class Audience {
+ // key is first letter; value is the first word; finally, the label.
static General = Shorthand.Enum('General', "General - For adults, but kids welcome");
static Family = Shorthand.Enum('Family', "Family Friendly - Adults bring children");
static Adult = Shorthand.Enum('Adult', "21+ Only - Adults only");
- // ex. transform 'G' into "General"
+ // ex. transform 'G' into the General Shorthand object.
static findShorthand(key) { return Shorthand.findShorthand(Audience, key); }
}
// the status of a single instance of an event on a given day.
-// generated by the view_public_schedule view for backwards compatibility with existing queries.
class EventStatus {
- static Active = new Shorthand('A', 'Active', 1);
- static Cancelled = new Shorthand('C', 'Cancelled', 0);
+ // key, value, label.
+ static Active = new Shorthand('A', 1, 'Active');
+ static Cancelled = new Shorthand('C', 0, 'Cancelled');
+ static Delisted = new Shorthand('D', null, 'Delisted');
- // transform 'A' (active) into 1, and 'C' (cancelled) into 0
- // if neither, returns undefined
+ // transform 'A', 'C', 'D' into the Active, Cancelled, Delisted Shorthand object.
static findShorthand(key) { return Shorthand.findShorthand(EventStatus, key); }
}
// an array of common ride distances
const Distances = [
- new Shorthand('', 'Unspecified', null),
+ // key, value. label.
+ new Shorthand('--', null, 'Unspecified'),
+
+ // the key and value are the same, followed by the label.
new Shorthand('0-3', '0-3 miles'),
new Shorthand('3-8', '3-8 miles'),
new Shorthand('8-15','8-15 miles'),
@@ -92,6 +97,7 @@ class Showable {
}
}
+ // ex. transform 'private' into the Showable.Private object.
static findShorthand(key) { return Shorthand.findShorthand(Showable, key); }
}
diff --git a/app/models/tables.js b/app/models/tables.js
deleted file mode 100644
index 3cf87d437..000000000
--- a/app/models/tables.js
+++ /dev/null
@@ -1,192 +0,0 @@
-const db = require("shift-docs/db");
-const config = require("shift-docs/config");
-
-// create tables if they dont already exist
-// fix? modified time doesn't work for sqlite
-// ( maybe manually set the time in knex.js store()? )
-module.exports = {
- dropTables: async () => {
- if (!config.isTesting) {
- throw new Error("probably don't want to be dropping production tables");
- }
- await db.query.schema.dropTableIfExists('caldaily');
- await db.query.schema.dropTableIfExists('calevent');
- },
- //
- createTables: async () => {
- const knex = db.query;
- const mysql = db.config.type === 'mysql';
- if (!knex) {
- throw new Error("db not initialized");
- }
-
- const hasCalDaily = await knex.schema.hasTable('caldaily');
- if (!hasCalDaily) {
- await knex.schema.createTable('caldaily', (table) => {
- createCalDaily( newTableMaker(db.query, mysql, table) );
- table.index(['eventdate'], 'eventdate');
- });
- }
- const hasCalEvent = await knex.schema.hasTable('calevent');
- if (!hasCalEvent) {
- await knex.schema.createTable('calevent', (table) => {
- createCalEvent( newTableMaker(db.query, mysql, table) );
- });
- }
- }
-};
-
-// table is a tableMaker
-// order based on existing tables to support reading dumps
-// ( see also: setup.sql )
-function createCalDaily(table) {
- table.modifiedTime(); // note: caldaily doesnt use createdTime
- table.integer('id'); // a reference to CalEvent.id
- table.text('newsflash', "mediumtext"); // medium text supports up to 16 MiB(!)
- table.date('eventdate');
- table.varchar('eventstatus', 1); // see: EventStatus (calConst.js); varchar, not char.
- table.integer('exceptionid'); // legacy
- table.primaryKey('pkid');
-}
-
-// table is a tableMaker
-// order based on existing tables to support reading dumps
-// ( see also: setup.sql )
-function createCalEvent(table) {
- table.createdTime();
- table.modifiedTime();
- table.integer('changes', 0);
- table.primaryKey('id');
- table.varchar('name');
- table.varchar('email');
- table.flag('hideemail');
- table.flag('emailforum'); // legacy
- table.flag('printemail');
- table.varchar('phone');
- table.flag('hidephone');
- table.flag('printphone');
- table.varchar('weburl', 512);
- table.varchar('webname');
- table.flag('printweburl');
- table.varchar('contact'); // arbitrary organizer contact information
- table.flag('hidecontact');
- table.flag('printcontact');
- table.varchar('title');
- table.varchar('tinytitle', 255, true); // true: not nullable but no explicit default
- table.enum('audience'); // see: Audience (calConst.js)
- table.text('descr');
- table.text('printdescr');
- table.varchar('image'); // currently, always "id.ext", some older (2016) rides have a name.
- table.integer('imageheight'); // legacy, not used since 2019
- table.integer('imagewidth');
- table.varchar('dates'); // legacy, unused since 2019 ( includes text like "Monday, June 23" )
- table.enum('datestype'); // see: DatesType (calConst.js)
- table.time('eventtime'); // rides occur on one or more days, all at the same time.
- table.integer('eventduration'); // number of minutes; usually treated as 60 minutes when not specified.
- table.varchar('timedetails');
- table.varchar('locname');
- table.varchar('address');
- table.enum('addressverified'); // legacy, was: Y/N/V/X/A
- table.varchar('locdetails');
- table.varchar('locend');
- table.flag('loopride');
- table.enum('area'); // see: Area (calConst.js)
- table.varchar('external', 250); // legacy, appears completely unused.
- table.varchar('source', 250); // legacy, appears completely unused.
- table.integer('nestid'); // legacy, appears completely unused.
- table.varchar('nestflag', 1); // legacy, appears completely unused; note varchar, not char.
- table.enum('review', 'I'); // see: Review (calConst.js)
- table.flag('highlight', true); // true: not nullable but no explicit default
- table.tinyflag('hidden'); // 0 until published by the organizer
- table.varchar('password', 50); // aka secret; autogenerated guid (mostly)
- table.varchar('ridelength'); // legacy, set to 12 for 2019's TNR; otherwise null.
- table.flag('safetyplan');
-};
-
-// wrapper to provide a simpler version of creating tables.
-function newTableMaker(knex, mysql, table) {
- if (mysql) {
- table.engine("MyISAM");
- }
- return {
- primaryKey(name) {
- // knex creates these as unsigned; the original tables were signed
- // it should be fine; that's a lot of ids.
- table.increments(name);
- },
- // add a column for row created time
- createdTime() {
- table.timestamp('created')
- .notNullable()
- .defaultTo(knex.fn.now());
- },
- // add a column for row modified time
- // fix? modified time doesn't work for sqlite;
- // ( maybe manually set the time in knex.js store()? )
- modifiedTime() {
- const ts= table.timestamp('modified').notNullable();
- if (!mysql) {
- ts.defaultTo(knex.fn.now());
- } else {
- ts.defaultTo(knex.raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
- }
- },
- text(name, kind= "mediumtext") {
- // medium text supports up to 16 MiB(!)
- // in the shift db, they don't have any sort of default.
- table.text(name, kind);
- },
- date(name) {
- table.date(name).defaultTo(null);
- },
- time(name) {
- table.time(name).defaultTo(null);
- },
- // semantically meant for 0/1 true/false values.
- // the shift db uses integers for these.
- flag(name, hasDefaultValue=false) {
- const column = table.integer(name);
- setDefaults(column, hasDefaultValue);
- },
- // meant for 0/1 true/false values.
- // a 1 byte signed value ranging from -128 to 127
- tinyflag(name, hasDefaultValue=false) {
- const column = table.tinyint(name);
- setDefaults(column, hasDefaultValue);
- },
- // 4 byte signed value (-2147483648 to 2147483647)
- integer(name, hasDefaultValue=false) {
- // note: in the original tables `int(11)` is a *display* size
- // and its deprecated as of mysql 8.0.17
- // https://dev.mysql.com/doc/refman/8.0/en/numeric-type-attributes.html
- const column = table.integer(name);
- setDefaults(column, hasDefaultValue);
- },
- // a single character value ( A-Z )
- enum(name, hasDefaultValue=false) {
- const column = table.specificType(name, "char(1)");
- setDefaults(column, hasDefaultValue);
- },
- // a string containing no more than 'width' characters.
- varchar(name, width=255, hasDefaultValue=false) {
- // note: knex string is mysql varchar(255)
- const column = table.string(name, width);
- setDefaults(column, hasDefaultValue);
- },
- };
-}
-
-// when hasDefaultValue is:
-// . explicitly false, default to null
-// . explicitly true, set as "not nullable"
-// . otherwise: set "not nullable" and give it that default
-function setDefaults(column, hasDefaultValue) {
- if (hasDefaultValue === false) {
- column.defaultTo(null);
- } else {
- column.notNullable();
- if (hasDefaultValue !== true) {
- column.defaultTo(hasDefaultValue)
- }
- }
-}
\ No newline at end of file
diff --git a/app/package.json b/app/package.json
index 2ecd76057..638bbaebe 100644
--- a/app/package.json
+++ b/app/package.json
@@ -9,22 +9,23 @@
"dayjs": "~1.11.23",
"express": "~4.22.0",
"knex": "~2.5.1",
- "lodash": "~4.17.21",
"multer": "~2.1.1",
"mysql2": "~3.14.0",
"nodemailer": "~7.0.11",
"nunjucks": "~3.2.4",
- "sqlite3": "~5.1.7",
"validator": "~13.15.22",
"wordwrapjs": "~5.1.0"
},
"scripts": {
"start": "node app.js",
- "test": "node --test --trace-warnings --test-global-setup=test/db_setup.js --test-concurrency=1 **/*_test.js --"
+ "test": "node ./testing/testRunner.js"
},
"devDependencies": {
+ "@faker-js/faker": "~8.4.1",
+ "http-proxy-middleware": "~3.0.5",
"shelljs": "^0.10.0",
"sinon": "^15.0.3",
+ "sqlite3": "~5.1.7",
"supertest": "^7.1.4"
}
}
diff --git a/app/schema/allTables.js b/app/schema/allTables.js
index ee5365cdb..5898545fe 100644
--- a/app/schema/allTables.js
+++ b/app/schema/allTables.js
@@ -6,12 +6,12 @@ const make = TableMaker.prototype;
const allTables = {
cal_series: {
id: make.primaryKey,
- published: make.integer, // a counter of the number of times published
+ published: make.integer, // a counter of the number of times published; or null for revoked
+ revoked: [make.string, {width: 64}], // string is a short reason; 'true' if revoked by the organizer, null if valid
title: [make.string, {width: 256}],
organizer: [make.string, {width: 256}],
details: [make.string, {width: 6500}], // there's a few in the 6k range
- start_time: [make.string, {width: 10, required: true}],
- scheduled_duration: make.integer, // in minutes
+ start_time: [make.string, {width: 10}],
scheduled_duration: make.integer, // in minutes
tiny_title: [make.string, {width: 50}],
summary: [make.string, {width: 812} ], // was: printdescr
@@ -42,7 +42,7 @@ const allTables = {
// note: some people have a short description here in phone
private_phone: [make.string, {width: 64} ],
private_contact: [make.string, {width: 256}],
- // one of the calConst.Showable(s)
+ // one of the newConst.Showable(s)
show_email: [make.string, {width: 12}],
show_phone: [make.string, {width: 12}],
show_contact: [make.string, {width: 12}],
diff --git a/app/schema/allViews.js b/app/schema/allViews.js
index ced1f4e51..ef9d5b950 100644
--- a/app/schema/allViews.js
+++ b/app/schema/allViews.js
@@ -8,146 +8,121 @@
// "in_" names are internal, partial views: helpers to build other view(s).
const allViews = {
- // a summary of all events whether published or not.
- view_private_events: `
-select
- *,
- secret as password,
-
- -- private fields are left as "private_email", etc.
- -- the intent was to prevent accidental leakage
- -- tbd if that's really useful or not.
-
- -- legacy visibility settings
- show_email not in ('public', 'visible') as hideemail,
- show_phone not in ('public', 'visible') as hidephone,
- show_contact not in ('public', 'visible') as hidecontact,
- show_weburl not in ('public', 'visible') as hideweburl,
-
- -- legacy printing settings
- show_email in ('public', 'printable') as printemail,
- show_phone in ('public', 'printable') as printphone,
- show_contact in ('public', 'printable') as printcontact,
- show_weburl in ('public', 'printable') as printweburl
-
+ // provides a summary of events for editing them.
+ // includes rides with no days
+ // includes unpublished rides ( they can be edited )
+ // excludes revoked rides ( they cannot be edited )
+ view_private_events: () => `
+select *, secret as password
from
in_event_series
- join cal_private using(id)
- join in_event_place using(id)
- join in_event_tags using(id)
+ join in_event_place using(id) -- always generates rows; left and inner join are the same
+ join in_event_tags using(id) -- same.
+ left join in_private_legacy using(id)
left join view_all_days using(id)
left join in_event_images using(id)
- left join in_private_web using(id);
-`,
-
- // the limited set of data needed for the ics feed.
- // only includes published events.
- view_ical_feed: `
-select *
-from view_all_days
-left join in_event_series using (id)
-left join in_event_place using (id)
-where not hidden;
+ left join in_private_web using(id)
+where not deleted;
`,
// produces a summary of published events and their days.
- // excludes events that have yet to be published;
- // excludes events with no days;
- // excludes revoked events.
- view_event_schedule: `
-select * from
- view_public_series
- join view_all_days using(id);
+ // excludes: revoked (deleted) events and events with no days.
+ // implicitly excludes: events which havent been published.
+ view_event_schedule: () => `
+select *
+ from view_published_series
+ join view_all_days using(id)
+where not deleted;
`,
// approximately the calevent data from the original db:
- // returns public data about published event series.
- view_public_series: `
-select * from
- in_event_series
- join in_permitted using(id)
+ // returns public data about published series even if revoked.
+ view_published_series: () => `
+select *
+ from in_event_series
join in_event_place using(id)
join in_event_tags using(id)
+ left join in_permitted using(id)
left join in_event_images using(id)
left join in_public_web using(id)
where not hidden;
`,
// the caldaily equivalent:
- // returns the schedule of events
- // regardless of publication status.
- view_all_days: `
+ // returns *all* known days
+ // ( ex. whether published, revoked, or delisted. )
+ view_all_days: (dbType) => {
+ // views which generate strings derive their collation from the connection.
+ // ( and not the collation of the server, database, or even the other tables in the query. )
+ // the connection collation can vary based on how you connect to the db
+ // adding an explicit collation to the view works around that. /sigh.
+ collate = dbType === "mysql" ? "collate utf8mb4_general_ci" : ""
+ return `
select
id,
pkid,
- added,
- changed,
ymd as eventdate,
case is_scheduled
when 1 then 'A'
when 0 then 'C'
- end as eventstatus,
+ when null then 'D'
+ end ${collate} as eventstatus,
news as newsflash
-from cal_schedule
--- is_scheduled can become null when an organizer deletes a particular day.
-where is_scheduled is not null;
-`,
+from cal_schedule;
+`},
- // used to validate events ( ex. for update event )
- view_private_checks: `
+ // used to validate events for event editing.
+ // revoked rides can't be edited, so they are excluded.
+ view_private_checks: () => `
select id, secret, published
-from cal_private
-join cal_series using(id)
--- revoked rides are indicated by a null published status.
-where published is not null;
-`,
-
- // use to find series and date from a pkid
- view_reverse_lookup: `
-select id, pkid, ymd as eventdate
-from cal_series
-join cal_schedule using(id)
-where published is not null
-and is_scheduled is not null
+ from cal_series
+ join cal_private using(id)
+where revoked is null;
`,
// series data with legacy field names
// excludes revoked rides ( those with null published data )
- in_event_series: `
+ in_event_series: () => `
select
id,
title,
tiny_title as tinytitle,
organizer as name,
+ start_time as eventtime,
+ scheduled_duration as eventduration,
details as descr,
summary as printdescr,
created,
modified,
published as changes,
- not published as hidden
-from cal_series
-where published is not null;
+ not published as hidden,
+ revoked is not null as deleted
+from cal_series;
`,
// generate public web contact info.
// the event endpoints currently only look for web urls.
// note: reduces empty strings to null
- in_public_web: `
+ in_public_web: () => `
select id,
- case when show_weburl in ('public', 'visible') and web_link != '' then web_link end as weburl,
- case when show_weburl in ('public', 'visible') and web_link != '' then web_text end as webname
+ show_weburl not in ('public', 'visible') as hideweburl,
+ show_weburl in ('public', 'printable') as printweburl,
+ case when show_weburl in ('public', 'visible') and web_link != '' then web_text end as webname,
+ case when show_weburl in ('public', 'visible') and web_link != '' then web_link end as weburl
+
from cal_web
where web_type = 'url';
`,
// generate private web contact info.
- // produces the 'printweburl' field to match the old code
- in_private_web: `
+ in_private_web: () => `
select id,
-- share settings
show_weburl,
-- legacy data names
+ show_weburl not in ('public', 'visible') as hideweburl,
+ show_weburl in ('public', 'printable') as printweburl,
web_text as webname,
web_link as weburl
@@ -161,8 +136,8 @@ where web_type = 'url';
// the "is" expressions turn boolean tests to 0 or 1, and handle null values automatically
// substring(str,y,z) chops str at char y, for z characters, where y=1 is the first character;
// if str is empty, it returns empty. if str is null, it returns null. ( so coalesce is needed. )
- in_event_tags: `
- select
+ in_event_tags: () => `
+select
cal_series.id,
coalesce( substring(tag_audience.tag_value, 1, 1), 'G' ) as audience,
coalesce( substring(tag_area.tag_value, 1, 1), 'P' ) as area,
@@ -182,39 +157,63 @@ from cal_series
// the public view of private contact info.
// only includes email, phone, and contact when allowed.
// never includes the secret.
- in_permitted: `
+ // fix? theoretically, we don't need serve the print fields
+ // they are info for shift and the organizer (when editing).
+ in_permitted: () => `
select
id,
- case when show_email in ('public', 'visible') then private_email end as email,
- case when show_phone in ('public', 'visible') then private_phone end as phone,
- case when show_contact in ('public', 'visible') then private_contact end as contact
+ hideemail, hidephone, hidecontact,
+ printemail, printphone, printcontact,
+
+ case when not hideemail then email end as email,
+ case when not hidephone then phone end as phone,
+ case when not hidecontact then contact end as contact
+from in_private_legacy;
+`,
+
+ // renames and expands the cal_private fields to match legacy
+ in_private_legacy: () => `
+select
+ id,
+ secret,
+
+ private_email as email,
+ private_phone as phone,
+ private_contact as contact,
+
+ -- legacy visibility settings
+ show_email not in ('public', 'visible') as hideemail,
+ show_phone not in ('public', 'visible') as hidephone,
+ show_contact not in ('public', 'visible') as hidecontact,
+
+ -- legacy printing settings
+ show_email in ('public', 'printable') as printemail,
+ show_phone in ('public', 'printable') as printphone,
+ show_contact in ('public', 'printable') as printcontact
+
from cal_private;
`,
// the combined starting and ending location data for every series
- in_event_place: `
+ in_event_place: () => `
select
cal_series.id as id,
loc_start.place_name as locname,
loc_start.address as address,
- cal_series.start_time as eventtime,
loc_start.time_info as timedetails,
loc_start.place_info as locdetails,
- loc_end.place_name as locend,
- cal_series.scheduled_duration as eventduration
-from
- cal_series
+ loc_end.place_name as locend
+from cal_series
left join cal_location as loc_start on (cal_series.id = loc_start.id and loc_start.loc_type='start')
left join cal_location as loc_end on (cal_series.id = loc_end.id and loc_end.loc_type='finish');
`,
-
// the pairing of id and image cache ( or image override ) filename.
// the *real* filename is the cache name without the trailing dash.
// the image override is used for images manually assigned by the calendar crew.
// example returned data:
// 123, 123-1.png
// 738, some_override.png
- in_event_images: `
+ in_event_images: () => `
select
cal_image.id,
coalesce(
@@ -243,7 +242,8 @@ module.exports = {
// the names of the views from bottom to top.
const viewNames = Object.keys(allViews).reverse();
return viewNames.map((n) => db.query.schema.createView(n, (view) => {
- const statement = allViews[n];
+ //
+ const statement = allViews[n](db.config.type);
return view.as(db.raw(statement));
}));
},
@@ -258,7 +258,8 @@ module.exports = {
const viewNames = Object.keys(allViews).reverse();
for (const name of viewNames) {
await db.query.schema.createView(name, (view) => {
- const statement = allViews[name];
+ //
+ const statement = allViews[name](db.config.type);
return view.as(db.raw(statement));
});
}
diff --git a/app/schema/index.js b/app/schema/index.js
index 407215541..2c56f5c60 100644
--- a/app/schema/index.js
+++ b/app/schema/index.js
@@ -7,4 +7,4 @@ module.exports = {
extraKeys,
setupTables,
setupStatements,
-}
+}
\ No newline at end of file
diff --git a/app/support/eventValidator.js b/app/support/eventValidator.js
new file mode 100644
index 000000000..d80306b5c
--- /dev/null
+++ b/app/support/eventValidator.js
@@ -0,0 +1,227 @@
+const assert = require('node:assert/strict');
+const FormValidator = require('shift-docs/support/formValidator');
+const { ErrorStrings } = require('shift-docs/util/errors');
+const { Shorthand } = require('shift-docs/util/shorthand');
+const { Area, Audience, Distance, EventStatus, LocType, Showable, TagName, WebType } = require('shift-docs/models/newConst');
+const dt = require('shift-docs/util/dateTime');
+
+class EventValidator extends FormValidator {
+ // for validating from an object containing shorthand constants
+ // ex. shorthands.Area
+ // assumes default value is a shorthand instance: ex. Area.Portland
+ shorthand(cls, fallback) {
+ const str = this.nullString(); // an empty string returns null
+ const enumEntry = cls.findShorthand(str); // looks up the specified value
+ if (enumEntry !== undefined) {
+ return enumEntry.value; // found it, perfect.
+ } else if (str !== null) {
+ this.addError(ErrorStrings.Invalid);
+ } else if (fallback instanceof Shorthand) {
+ return fallback.value;
+ } else if (fallback === null) {
+ return null; // ex. detects null for an unspecified distance.
+ } else if (fallback === undefined) {
+ this.addError(ErrorStrings.Missing);
+ } else {
+ // some coding error:
+ throw new Error(`unexpected fallback ${fallback} for ${this.currentField}`);
+ }
+ }
+
+ /**
+ * combines a hide* and print* field into a "showable" enum
+ * the fallback is expected to be one of the Showable enums or 'undefined'
+ */
+ printField(field, fallback) {
+ // the rest endpoints use a single value; see if it exists
+ const showable = 'show_' + field;
+ if (Object.hasOwn(this.input, showable)) {
+ return this.select(showable).shorthand(Showable, fallback);
+ } else {
+ // the php endpoints uses a pair of values
+ const defaultPrint = (fallback === Showable.Public || fallback === Showable.Printable);
+ const defaultVisible = (fallback === Showable.Public || fallback === Showable.Visible);
+ const hide = this.select(`hide${field}`).simpleFlag(!defaultVisible);
+ const print = this.select(`print${field}`).simpleFlag(defaultPrint);
+ const show = Showable.combine(!hide, print);
+ return show.value;
+ }
+ }
+ // given the index being parsed, and a value containing a date
+ // returns the date as a yyyy-mm-dd string;
+ // or, undefined on error.
+ requireDate() {
+ const date = this.nullString();
+ if (!dt.fromYMDString(date).isValid()) {
+ this.addError("Dates requires the format YYYY-MM-DD");
+ } else {
+ return date;
+ }
+ }
+ // returns the db value to store: 0, 1, or null
+ optionalStatus() {
+ // if someone didn't specify a status; but did include a date
+ // assume they meant a cancelled date.
+ return this.shorthand(EventStatus, EventStatus.Cancelled);
+ }
+ // returns the validated string to store
+ // or, undefined on error.
+ optionalNews() {
+ return this.nullString(1024);
+ }
+ // return 0 or 1
+ // or, undefined on error.
+ parseSelect() {
+ const val = this.nullString();
+ switch (val) {
+ case '':
+ return 0;
+ case '1':
+ return 1;
+ case null:
+ this.addError(ErrorStrings.Missing);
+ default:
+ this.addError(ErrorStrings.Unknown);
+ };
+ }
+}
+
+// report errors as "date-field"
+// fallback to using the index of the element if the date is missing.
+// note: v2 html uses the date-field format for its inputs
+// so this prefixing directs errors to the right html display.
+function selectDate(v, at, field) {
+ const prefix = at.date || `index-${at}`;
+ return v.setFieldValue(`${prefix}-${field}`, at[field]);
+}
+
+function readSchedule(v) {
+ // an array of {date, status, newsflash}
+ const dates = v.input.datestatuses;
+ if (!dates) {
+ // in v2 this is the date selector control
+ v.errors.addError('datestatuses', "At least one day is required");
+ } else {
+ return dates.map((at, i) => {
+ const date = selectDate(v, at, 'date').requireDate();
+ assert.ok(typeof(date) === 'string', "unexpected date");
+ const status = selectDate(v, at, 'status').optionalStatus();
+ assert.ok([0, 1, null].includes(status), "unexpected status");
+ const newsflash = selectDate(v, at, 'newsflash').optionalNews();
+ return {
+ ymd: date, // string
+ is_scheduled: status, // 0, 1, or null
+ news: newsflash, // string or null
+ };
+ });
+ }
+}
+
+function newValidator(input, errors) {
+ const v = new EventValidator(input, errors);
+ return {
+ // these required when trying to publish a ride.
+ // ( they don't get stored into the db. )
+ validateTerms() {
+ v.select('code_of_conduct').requireTrue("You must agree to the Code of Conduct");
+ v.select('read_comic').requireTrue("You must have read the Ride Leading Comic");
+ },
+
+ // validate the input, transforming from client field names into db column names.
+ // returns a mapping of database table name to their column names and values
+ // the third parameters are the options and fallbacks
+ // if a fallback is undefined, the value is required.
+ validateInput({
+ defaultArea = undefined, // can be one of the Area shorthands: ex. Area.Portland
+ defaultAudience = undefined, // an Audience shorthand
+ defaultDistance = undefined, // a Distance shorthand, or null for unspecified
+ defaultLoop = undefined, // true, false, or undefined for required.
+ defaultSaftey = undefined, // true, false, or undefined for required.
+ defaultShow = { // these all default to hidden expect for the website
+ email: Showable.Private,
+ phone: Showable.Private,
+ contact: Showable.Private,
+ website: Showable.Visible, // by default: seen online but not printed
+ }
+ }) {
+ return {
+ schedule: readSchedule(v),
+ event: {
+ // image: ... image data is handled separately (via multi-part form data)
+ series: {
+ title: v.select('title').requireString(256),
+ organizer: v.select('organizer').requireString(),
+ start_time: v.select('time').requiredTime(), // can return something falsy on error.
+ scheduled_duration: v.select('eventduration').zeroInt() || null, // client defaults this to "" if not specified.
+ tiny_title: v.select('tinytitle').nullString(48), // client caps to 24, but some are longer already
+ summary: v.select('printdescr').nullString(1024),
+ details: v.select('details').requireString(16*1024),
+ modified: dt.toTimestamp(), // for the sake of sqlite, set this manually.
+ },
+ location: [{
+ // start should be listed in the location array first.
+ loc_type: LocType.Start,
+ place_name: v.select('venue').requireString(),
+ address: v.select('address').requireString(),
+ place_info: v.select('locdetails').nullString(),
+ time_info: v.select('timedetails').nullString(),
+ }, {
+ // finish should in the location list somewhere after start
+ loc_type: LocType.Finish,
+ place_name: v.select('locend').nullString(),
+ }],
+ private: {
+ // fix? only allow email to set on create; never update.
+ private_email: v.select('email').requireEmail(),
+ private_phone: v.select('phone').nullString(),
+ private_contact: v.select('contact').nullString(),
+ //
+ show_email: v.printField('email', defaultShow.email),
+ show_phone: v.printField('phone', defaultShow.phone),
+ show_contact: v.printField('contact', defaultShow.contact),
+ },
+ tag: [
+ tag(TagName.Area, v.select('area').shorthand(Area, defaultArea)),
+ tag(TagName.Audience, v.select('audience').shorthand(Audience, defaultAudience)),
+ tag(TagName.Distance, v.select('ridelength').shorthand(Distance, defaultDistance)),
+ tag(TagName.LoopRide, v.select('loopride').simpleBool(defaultLoop)),
+ tag(TagName.SafetyPlan, v.select('safetyplan').simpleBool(defaultSaftey)),
+ ].filter(t => !!t.tag_value),
+ web: [{
+ web_type: WebType.Url,
+ web_link: v.select('weburl').nullString(512),
+ web_text: v.select('webname').nullString(),
+ // to support the php endpoint, this one defaults to show true if not specified
+ show_weburl: v.printField('weburl', defaultShow.website),
+ }],
+ }
+ };
+ }
+ };
+}
+
+// note: tags with falsey values get filtered on insertion and update
+function tag(a, b) {
+ if (!!b) {
+ const t = typeof(b);
+ if (t === 'boolean') {
+ // true gets stored as 'true'
+ b = String(b);
+ } else if (t !== 'string') {
+ // these are supposed to be validated values
+ // so an error here is likely a server coding error
+ throw new Error(`unexpected tag value ${typeof(b)}(${b})`);
+ }
+ }
+ return {
+ tag_type: a,
+ tag_value: b,
+ }
+}
+
+
+// --------------------------
+module.exports = {
+ newValidator,
+ EventValidator, // exposed for testing
+};
diff --git a/app/support/formValidator.js b/app/support/formValidator.js
new file mode 100644
index 000000000..d5bb50030
--- /dev/null
+++ b/app/support/formValidator.js
@@ -0,0 +1,178 @@
+/**
+ * User input validator helper specific to events.
+ * ensure the email, title, etc. submitted by the organizer seem valid.
+ */
+const dt = require('shift-docs/util/dateTime');
+const validator = require('validator');
+const { ErrorStrings } = require('shift-docs/util/errors');
+
+class FormValidator {
+
+ // the validator package *requires* strings
+ // but not all data sent by the client are strings.
+ // example usage: validator.isBoolean(asString(userData)).
+ static asString(value) {
+ // coalesce nulls and undefined into a blank string
+ // ensure all numbers and booleans are strings
+ // trim all strings
+ return ((value ?? '') + '').trim();
+ }
+ static smallerThan(str, maxLen) {
+ return validator.isByteLength(str, { min: 0, max: maxLen });
+ }
+ // input is json, assumes errors is of type ErrorCollector
+ constructor(input, errors) {
+ this.input = input;
+ this.errors = errors;
+ this.currentField = null;
+ this.currentValue = null;
+ }
+ // set the current value to the named field
+ // from the input data given to the constructor.
+ select(field) {
+ this.setFieldValue(field, this.input[field]);
+ return this;
+ }
+
+ setFieldValue(field, value) {
+ this.currentField = field;
+ this.currentValue = FormValidator.asString(value);
+ return this;
+ }
+ // record an error for the current field with optional custom message.
+ // if msg is null or undefined, it records a default error message.
+ addError(msg) {
+ const field = this.currentField;
+ this.errors.addError(field, msg);
+ return this;
+ }
+ // ensure the selected value is string-ish and smaller than maxLen.
+ // returns the value if so, adds an error if not.
+ requireString(maxLen = 255) {
+ const str = this.currentValue;
+ if (validator.isEmpty(str)) {
+ this.addError(ErrorStrings.Missing);
+ } else if (!FormValidator.smallerThan(str, maxLen)) {
+ this.addError(ErrorStrings.Overflow);
+ } else {
+ return str;
+ }
+ }
+ // ensure the selected value looks like an email, smaller than maxLen.
+ // returns the value if so, adds an error if not.
+ requireEmail(maxLen = 256) {
+ const str = this.currentValue;
+ if (validator.isEmpty(str)) {
+ this.addError(ErrorStrings.Missing);
+ } else if (!FormValidator.smallerThan(str, maxLen)) {
+ this.addError(ErrorStrings.Overflow);
+ } else if (!validator.isEmail(str)) {
+ this.addError(ErrorStrings.Invalid);
+ } else {
+ return str;
+ }
+ }
+ // ensure the selected value exists.
+ // returns true if so, adds an error if not.
+ requireTrue(msg) {
+ const str = this.currentValue;
+ if (validator.isEmpty(str)) {
+ this.addError(msg || ErrorStrings.Missing);
+ } else if (!validator.isBoolean(str)) {
+ this.addError(msg || ErrorStrings.Invalid);
+ } else if (!validator.toBoolean(str)) {
+ this.addError(msg || ErrorStrings.Unknown);
+ } else {
+ return true;
+ }
+ }
+ // validate and transform the event time
+ // the client mostly sends AM/PM style
+ // the database stores 24 times 'hh:mm:ss'
+ // https://dev.mysql.com/doc/refman/8.0/en/time.html
+ requiredTime() {
+ const str = this.currentValue;
+ if (validator.isEmpty(str)) {
+ this.addError("A time must be specified");
+ } else {
+ // input prefers 12 hour time; fallsback to 24 when needed.
+ const t = dt.fromTimeString(str);
+ if (!t.isValid()) {
+ this.addError(ErrorStrings.Invalid);
+ } else {
+ return dt.to24HourString(t);
+ }
+ }
+ }
+ // to mimic php/flourish empty strings are converted to null.
+ // https://flourishlib.com/docs/fActiveRecord.html#ColumnOperations
+ nullString(maxLen = 255) {
+ const str = this.currentValue;
+ if (validator.isEmpty(str)) {
+ return null;
+ } else if (!FormValidator.smallerThan(str, maxLen)) {
+ this.addError(ErrorStrings.Overflow);
+ } else {
+ return str;
+ }
+ }
+ // if fallback is 'undefined',
+ // the value is required.
+ simpleBool(fallback = undefined) {
+ const str = this.currentValue;
+ if (validator.isBoolean(str)) {
+ return validator.toBoolean(str);
+ } else if (!validator.isEmpty(str)) {
+ this.addError(ErrorStrings.Unknown);
+ } else if (fallback === undefined) {
+ this.addError(ErrorStrings.Missing);
+ } else {
+ return fallback;
+ }
+ }
+ // same as simpleBool but returns a 0/1 value instead of true/false
+ simpleFlag(fallback = undefined) {
+ const v = this.simpleBool(fallback);
+ if (v === true) {
+ return 1;
+ } else if (v === false) {
+ return 0;
+ } else if (v !== undefined) {
+ return fallback;
+ }
+ }
+ // if not specified, returns 0
+ // otherwise expects an int-like value
+ // greater than or equal to 0
+ zeroInt(msg) {
+ const str = this.currentValue;
+ if (validator.isEmpty(str)) {
+ return 0;
+ } else if (!validator.isInt(str)) {
+ this.addError(msg);
+ } else {
+ // validator returns NaN if it cant convert
+ const val = validator.toInt(str);
+ if (isNaN(val) || val < 0) {
+ this.addError(ErrorStrings.Invalid);
+ } else {
+ return val;
+ }
+ }
+ }
+ // if not specified, returns the defaultVal
+ // otherwise must be a single letter
+ optionalChar(defaultVal) {
+ const str = this.currentValue;
+ if (validator.isEmpty(str)) {
+ return defaultVal;
+ } else if (!validator.matches(str, /^[A-Z]$/)) {
+ this.addError(ErrorStrings.Unknown);
+ } else {
+ return str;
+ }
+ }
+}
+
+module.exports = FormValidator;
+
diff --git a/app/support/reconcile.js b/app/support/reconcile.js
new file mode 100644
index 000000000..8f642ee3e
--- /dev/null
+++ b/app/support/reconcile.js
@@ -0,0 +1,290 @@
+/**
+ * high level functions for creating and updating events in the db.
+ * note: all of these assume the incoming data has been validated
+ * except for the correct id/password pairing.
+ */
+const db = require('shift-docs/db');
+const { allTables, extraKeys } = require('shift-docs/schema/allTables');
+const dt = require('shift-docs/util/dateTime');
+const misc = require('shift-docs/util/misc');
+const assert = require('node:assert/strict');
+
+module.exports = {
+ newEventData, // generates a new secret
+ updateEventData, // validates the secret
+ updateImageData, // does not validate the secret
+ deleteEntireSeries, // validates the secret
+ revokeEntireSeries,
+ // exported for testing
+ isEmptyRow,
+ insertNewData // overwrites the secret
+};
+
+// ex. "series" -> "cal_series"
+function getTableName(group) {
+ return "cal_" + group;
+}
+
+const table = {
+ series: "cal_series",
+ image: "cal_image",
+ schedule: 'cal_schedule',
+ privateData: 'cal_private',
+}
+
+// after creating a new event and adding all its info
+// promises an object containing { id, secret }.
+// tx can be either 'db.query' or a knex transaction.
+function newEventData(tx, eventData, dayData) {
+ const secret = misc.newSecret();
+ eventData.private.secret = secret;
+ return insertNewData(tx, eventData).then(id => {
+ assert.ok(id, "expected a valid series id");
+ return writeSchedule(tx, id, dayData).then(_ => ({
+ id,
+ secret
+ }));
+ });
+}
+
+// after validating the id and secret
+// updates all event and scheduling info then
+// promises an object containing { seriesId, published }
+// where 'published' is a counter of the next change.
+function updateEventData(tx, seriesId, eventData, dayData) {
+ // eventData is a map of table to row data
+ // for each table, generate an update statement
+ const updates = Object.keys(eventData).flatMap(group => {
+ const vals = eventData[group];
+ // normalize into an array. most of the time there will be only one row
+ // except for something like tag(s) which might have multiple rows to write.
+ const rows = [].concat(vals);
+ const tableName = getTableName(group);
+ return rows.map(row => updateOrDelete(tx, tableName, seriesId, row));
+ }).concat(writeSchedule(tx, seriesId, dayData));
+ // wait on all the table updates including the schedule.
+ // after everything: return the id and predicted next published counter
+ return Promise.all(updates);
+}
+
+// insert or update image data for the indicated series.
+// where img = { name, ext: '.ext' }
+// does not validate the secret!
+function updateImageData(tx, seriesId, name, ext) {
+ assert.equal(name, '' + seriesId,
+ `Image for series '${seriesId}' had an unexpected name: '${name}'`);
+ assert.ok('' + ext.startsWith('.'),
+ `Image for series '${seriesId}' had an unexpected extension: '${ext}'`);
+ const data = {
+ id: seriesId,
+ img_version: 1,
+ img_ext: ext.slice(1),
+ }
+ return tx(table.image)
+ .insert(data)
+ .onConflict('id')
+ // undocumented merge raw:
+ // https://github.com/knex/knex/issues/5652#issuecomment-3368670719
+ .merge({
+ img_version: db.raw(`img_version + 1`),
+ img_ext: data.img_ext,
+ // doesn't clear override
+ // b/c that's set by us
+ // if it's set, there's probably a reason.
+ // img_override: null,
+ });
+}
+
+async function writeSchedule(tx, seriesId, dayData) {
+ // create an array of just the dates. ex ["YYYY-MM-DD", "YYYY-MM-DD", ...]
+ const ymds = dayData.map(el => el.ymd);
+ // assuming there are some....
+ if (ymds.length) {
+ // first insert any missing dates to the db
+ await addMissingDays(tx, seriesId, ymds);
+ // and then update the data for all the requested days
+ await updateDailyStatus(tx, seriesId, dayData);
+ }
+ // finally, remove any entries *not* in the requested dates
+ await delistMissingDays(tx, seriesId, ymds);
+}
+
+// create days for any dates not in the db.
+// assumes newDates is an array of validated YYYY-MM-DD strings.
+function addMissingDays(tx, seriesId, ymds) {
+ const now = dt.toTimestamp(); // for sqlite, set the change time manually
+ // generates ? placeholders for the query
+ const withIdDates = withTable("idDates", ['id', 'ymd', 'changed'], ymds.length);
+ // those places holders are filled with these matching values:
+ const idDates = ymds.flatMap(ymd => [seriesId, ymd, now]);
+ const raw = `
+ insert into ${table.schedule}(id, ymd, changed)
+ ${withIdDates}
+ select * from idDates
+ where not exists (
+ select 1 from ${table.schedule}
+ where ${table.schedule}.id = idDates.id
+ and ${table.schedule}.ymd = idDates.ymd
+ )`;
+ return tx.raw(raw, idDates);
+}
+
+// delist any days not listed in ymds
+// ( by filling the schedule with nulls )
+function delistMissingDays(tx, seriesId, ymds) {
+ assert.ok(seriesId, "missing id");
+ const now = dt.toTimestamp(); // for sqlite, set the change time manually
+ return tx(table.schedule)
+ .where('id', seriesId)
+ .whereNotIn('ymd', ymds)
+ .update({
+ is_scheduled: null,
+ changed: now,
+ });
+}
+
+// overwrite an existing schedule with dayData
+// dayData is an array of: { ymd, is_scheduled, news }
+// the validated data from the client.
+function updateDailyStatus(tx, seriesId, dayData) {
+ const now = dt.toTimestamp(); // for sqlite, set the change time manually
+ const usingSqlite = db.config.type === 'sqlite';
+ const withValues = withTable("vals", ['ymd', 'new_status', 'new_news', 'new_changed'], dayData.length);
+ const vals = dayData.flatMap(sched => [sched.ymd, sched.is_scheduled, sched.news, now])
+ return tx.raw(withValues + `
+ update ${table.schedule}
+ ${!usingSqlite ? `join vals`: ""}
+ set
+ is_scheduled = vals.new_status,
+ news = vals.new_news,
+ changed = vals.new_changed
+ ${usingSqlite ? `from vals`: ""}
+ where ${table.schedule}.id = ${seriesId}
+ and ${table.schedule}.ymd = vals.ymd`, vals);
+}
+
+// hides the series; ensures id and secret are valid and match before revoking.
+// reason is an optional string describing why the ride has been revoked.
+function revokeEntireSeries(tx, id, secret, reason = null) {
+ assert.ok(id, "missing id");
+ assert.ok(secret, "missing secret");
+ // match the id and secret
+ return tx(table.privateData)
+ .where({id, secret})
+ .then(async(rows) => {
+ // mismatch or missing, return false
+ if (!rows.length) {
+ return false;
+ } else {
+ // otherwise, record the revocated reason
+ // ( defaulting to the string 'true' if there's no specific reason )
+ await tx(table.series)
+ .where({id})
+ .update("revoked", reason ?? "true");
+ // and delist the individual days
+ await tx(table.schedule)
+ .where({id})
+ .update("is_scheduled", null);
+ // done!
+ return true;
+ }
+ }); // end query
+}
+
+// promises the total number of removed items when done:
+// zero if the series didn't exist or the secret didn't match.
+function deleteEntireSeries(tx, id, secret) {
+ assert.ok(id, "missing id");
+ assert.ok(secret, "missing secret");
+ //
+ return tx(table.privateData)
+ .where({id, secret})
+ .then(rows => {
+ if (!rows.length) {
+ return 0;
+ } else {
+ // ex. "cal_series"
+ const tableNames = Object.keys(allTables);
+ // each del() results in a count of the rows deleted
+ const ps = tableNames.map(name => tx(name).del().where('id', id));
+ // after deleting everything, sum all of the counts.
+ return Promise.all(ps).then(cnt => cnt.reduce((sum, val) => sum + val, 0));
+ }
+ }); // end query
+}
+
+// generate a transient table with the named columns, and a certain number of rows.
+// return a string with enough placeholder parameters to hold row * column values.
+// ex. `with name(col0, col1, col2) as ( select ?, ? union all select ?, ? )`
+// generates a 3 column, 2 row table, and '?' is a placeholder which gets filled elsewhere.
+function withTable(name, cols, rowcount) {
+ const select = "select " + Array(cols.length).fill("?").join(",");
+ return `with ${name} (${cols}) as ( ${
+ Array(rowcount).fill(select).join(" union all ")
+ } )`;
+}
+
+// adds new event data to the db, promising the generated seriesId.
+// eventData is a map of group name to table data.
+// the tableData can be one row of data, or multiple rows ( an array. )
+// each row of data is an object { column_name: column_data }.
+// NOTE: this doesn't do any data validation nor any secret testing.
+function insertNewData(tx, eventData) {
+ // first: insert the series table first
+ return tx(table.series).insert(eventData.series).then(row => {
+ // the series id is auto-generated by the series table
+ const seriesId = row[0];
+ // then: insert the other tables other than series.
+ const groups = Object.keys(eventData).filter(group => group !== 'series');
+ // visit all of them, insert returns a promise
+ // make a list of all the promised inserts
+ const promises = groups.flatMap(group => {
+ // ex. "series" -> "cal_series"
+ const tableName = getTableName(group);
+ // concat normalizes one or more rows in the data to a list of rows.
+ const rows = [].concat(eventData[group]).filter(rowData => {
+ // filter out any empty rows out of the inserted data.
+ return !isEmptyRow(tableName, rowData);
+ });
+ return rows.map(rowData => {
+ rowData.id = seriesId; // every row in every table references the series
+ return tx(tableName).insert(rowData);
+ });
+ });
+ return Promise.all(promises).then(_ => seriesId);
+ });
+}
+
+function updateOrDelete(tx, tableName, seriesId, rowData) {
+ assert.ok(seriesId, "missing id");
+ const q = tx(tableName).where('id', seriesId);
+ const extraKey = extraKeys[tableName];
+ if (extraKey) {
+ q.where(extraKey, rowData[extraKey]);
+ }
+ // for safety, lets only try to delete rows from the special type tables
+ if (extraKey && isEmptyData(rowData, [extraKey])) {
+ q.del();
+ } else {
+ q.update(rowData);
+ }
+ return q;
+}
+
+// returns `true` if every value in the passed row data is empty
+// where `tableName` is a known table, and `rowData` is a map of { column_name: column_data }.
+// used by insertNewData() to skip inserting data which would generate a row of empty data.
+// ( ex. a tag with a false value, or an empty ending location. )
+function isEmptyRow(tableName, rowData) {
+ const extraKey = extraKeys[tableName];
+ return isEmptyData(rowData, ['id', extraKey]);
+}
+
+// helper for isEmptyRow
+// if the named column isnt in the ignore list,
+// and it has a non-zero value; the row has data.
+function isEmptyData(rowData, ignore = []) {
+ const first = Object.keys(rowData).findIndex(colName =>
+ !ignore.includes(colName) && !!rowData[colName]);
+ return first < 0;
+}
diff --git a/app/support/requestedEvent.js b/app/support/requestedEvent.js
new file mode 100644
index 000000000..6e2f93629
--- /dev/null
+++ b/app/support/requestedEvent.js
@@ -0,0 +1,59 @@
+const db = require('shift-docs/db');
+const config = require('shift-docs/config');
+const { uploader } = require('shift-docs/uploader');
+const reconcile = require('shift-docs/support/reconcile');
+const dt = require('shift-docs/util/dateTime');
+const assert = require('node:assert/strict');
+
+module.exports = {
+ checkEventSecret,
+ writeEvent,
+ writeImage
+};
+
+// validate the passed id and secret
+// promises the publication count
+// returns null the id or secret is mismatched or invalid.
+function checkEventSecret(id, secret) {
+ // knex has the annoying behavior that an undefined parameter value
+ // and the calling code expects null when id or secret is missing or unmatched.
+ if (!id || !secret) {
+ return Promise.resolve(null);
+ }
+ return db
+ .query("view_private_checks")
+ .where({ id, secret })
+ .then((rows) => rows.length ? rows[0].published : null);
+}
+
+function writeEvent(id, event, schedule, nextPublished) {
+ return db.query.transaction((tx) => {
+ // we've read the current published value from the db:
+ // increment it, and store it into the new series data.
+ // not sure if the transaction saves us from race conditions on the counter.
+ // (ex. if two requests to update the same event are being processed at the same time)
+ // but i don't think it matters much.
+ event.series.published = nextPublished;
+ return reconcile.updateEventData(tx, id, event, schedule);
+ });
+}
+
+// returns the url of the new image, or throws on error.
+// fix? this results in errors saving the event even if all the other data was okay.
+// todo: a way of clearing the image?
+async function writeImage(id, fileData) {
+ // first write the image to disk as (id.ext)
+ // returns { name, ext }
+ const f = await uploader.write(fileData, id.toString());
+ // then write that to the the db
+ await reconcile.updateImageData(db.query, id, f.name, f.ext);
+ // we use the image sequence number to disrupt the browser's cache
+ // but here, the client just needs to know something changed.
+ // we stamp the image with the time b/c its guaranteed to be unique.
+ // when someone asks for the event data next, it'll return real, the updated sequence id.
+ // [ doing this saves us from having to query the results of the update here ]
+ const unique = dt.icalFormat(dt.getNow());
+ const filename = `${f.name}-${unique}${f.ext}`;
+ // return the url
+ return config.image.url(filename);
+}
\ No newline at end of file
diff --git a/app/test/delete_test.js b/app/test/delete_test.js
index 7284885e5..6449a528f 100644
--- a/app/test/delete_test.js
+++ b/app/test/delete_test.js
@@ -1,180 +1,118 @@
-const app = require("../appEndpoints");
-const testdb = require("./testdb");
-const testData = require("./testData");
-
-const { CalEvent } = require("../models/calEvent");
-const { CalDaily } = require("../models/calDaily");
-//
-const sinon = require('sinon');
-const { describe, it, before, after } = require("node:test");
-const assert = require("node:assert/strict");
+const assert = require('node:assert/strict');
+const { describe, it, beforeEach, afterEach } = require('node:test');
const request = require('supertest');
//
-const delete_api = '/api/delete_event.php';
+const app = require('shift-docs/appEndpoints');
+const testing = require('shift-docs/testing');
+// delete using forms
describe("deleting using a form", () => {
- // spies on data storage:
- let spy;
- // runs before the first test in this block.
- before(() => {
- spy = testData.stubData(sinon);
- return testdb.setupTestData("del");
- });
- // runs once after the last test in this block
- after(() => {
- sinon.restore();
- return testdb.destroy();
- });
- // test:
- it("reports 404 on an invalid id", () => {
- return request(app)
- .post(delete_api)
- .type('form')
- .send({
- json: JSON.stringify({
- id: 999,
- })
- })
- .then(res => testData.expectError(res, null, 404));
- });
- it("reports 403 on an incorrect password", () => {
- return request(app)
- .post(delete_api)
- .type('form')
- .send({
- json: JSON.stringify({
- id: 2,
- secret: "to life, etc.",
- })
- })
- .then(res => testData.expectError(res, null, 403));
+ beforeEach(() => {
+ testing.configure("json");
+ return testing.setupTestData("deleting with forms");
});
- it("delists a published event", async () => {
- const e0 = await CalEvent.getByID(2);
- const d1 = await CalDaily.getForTesting(201);
- const d2 = await CalDaily.getForTesting(202);
-
- assert.notEqual(e0.review, 'E'); // anything is fine other than excluded
- assert.ok(e0.password);
- assert.equal(d1.eventstatus, 'A');
- assert.equal(d2.eventstatus, 'A');
-
- return request(app)
- .post(delete_api)
- .type('form')
- .send({
- json: JSON.stringify({
- id: 2,
- secret: testData.secret,
- })
- })
- .expect(200)
- .expect('Content-Type', /json/)
- .expect('Api-Version', /^3\./)
- .then(async (res) => {
- assert.equal(spy.eventStore.callCount, 1);
- assert.equal(spy.dailyStore.callCount, 2);
- spy.resetHistory();
-
- const e0 = await CalEvent.getByID(2);
- const d1 = await CalDaily.getForTesting(201);
- const d2 = await CalDaily.getForTesting(202);
-
- // the days of deleted events are marked with D
- // to distinguish them from individually canceled events.
- assert.equal(e0.review, 'E');
- assert.ok(!e0.password);
- assert.equal(d1.eventstatus, 'D');
- assert.equal(d2.eventstatus, 'D');
- });
+ afterEach(() => {
+ return testing.reset();
});
+ handleDeleteWithForms(true);
});
-// do the same things again,but post json ( ala curl )
+// delete without forms
describe("deleting using json", () => {
- let spy;
- before(() => {
- spy = testData.stubData(sinon);
- return testdb.setupTestData("del json");
+ beforeEach(() => {
+ testing.configure("json");
+ return testing.setupTestData("deleting with json");
});
- after(() => {
- sinon.restore();
- return testdb.destroy();
+ afterEach(() => {
+ return testing.reset();
});
- it("delists a published event", async () => {
- const e0 = await CalEvent.getByID(2);
- const d1 = await CalDaily.getForTesting(201);
- const d2 = await CalDaily.getForTesting(202);
+ handleDeleteWithForms(false);
+});
- assert.notEqual(e0.review, 'E');
- assert.ok(e0.password);
- assert.equal(d1.eventstatus, 'A');
- assert.equal(d2.eventstatus, 'A');
+// ---------------------------------------
+// support functions:
+// ---------------------------------------
- return request(app)
- .post(delete_api)
- // .type('form') ... intentionally send not as a form
- .send({
+// run a series of deletion tests with or without forms
+function handleDeleteWithForms(useForm) {
+ it("reports 403 on an invalid id", () => {
+ return Promise.resolve()
+ .then(_ => requestDelete({
+ id: 999,
+ secret: testing.user.secret,
+ useForm
+ }))
+ .then(res => testing.expectError(res, null, 403));
+ });
+ it("reports 403 on an invalid password", () => {
+ // fix? maybe this should return "okay"
+ // so that someone couldn't spam searching for the correct secret.
+ return Promise.resolve()
+ .then(_ => requestDelete({
id: 2,
- secret: testData.secret,
- })
- .expect(200)
- .expect('Content-Type', /json/)
- .expect('Api-Version', /^3\./)
- .then(async (res) => {
- assert.equal(spy.eventStore.callCount, 1);
- assert.equal(spy.dailyStore.callCount, 2);
- assert.equal(spy.eventErasures.callCount, 0);
- spy.resetHistory();
-
- const e0 = await CalEvent.getByID(2);
- const d1 = await CalDaily.getForTesting(201);
- const d2 = await CalDaily.getForTesting(202);
+ secret: "invalid password",
+ useForm
+ }))
+ .then(res => testing.expectError(res, null, 403))
+ .then(_ => verifyDataExists(2));
+ });
- // the days of deleted events are marked with D
- // to distinguish them from individually canceled events.
- assert.equal(e0.review, 'E');
- assert.ok(!e0.password);
- assert.equal(d1.eventstatus, 'D');
- assert.equal(d2.eventstatus, 'D');
- });
+ it("deletes published events", () => {
+ return Promise.resolve()
+ .then(_ => verifyDataExists(2))
+ .then(_ => requestDelete({
+ id: 2,
+ secret: testing.user.secret,
+ useForm
+ }))
+ .then(testing.expectOkay)
+ .then(_ => verifyDataDeleted(2));
});
it("deletes unpublished events", () => {
- return request(app)
- .post(delete_api)
- .send({
+ return Promise.resolve()
+ .then(_ => verifyDataExists(3))
+ .then(_ => requestDelete({
id: 3,
- secret: testData.secret,
- })
- .expect(200)
- .expect('Content-Type', /json/)
- .expect('Api-Version', /^3\./)
- .then(res => {
- assert.equal(spy.eventErasures.callCount, 1);
- spy.resetHistory();
- });
+ secret: testing.user.secret,
+ useForm
+ }))
+ .then(testing.expectOkay)
+ .then(_ => verifyDataDeleted(3));
});
- it("deletes a legacy event", async () => {
- const e0 = await CalEvent.getByID(1);
- assert.notEqual(e0.review, 'E');
- assert.ok(e0.password);
- //
+}
+
+async function verifyDataExists(id) {
+ // fix -- can this ve reverted here?
+ const events = await testing.findSeries(id);
+ if (id !== 2) {
+ assert.ok(events.length > 0);
+ } else {
+ assert.equal(events.length, 2);
+ const [d1, d2] = events;
+ assert.equal(d1.hidden, 0);
+ assert.equal(d1.eventstatus, 'A');
+ assert.equal(d2.eventstatus, 'A');
+ }
+}
+
+async function verifyDataDeleted(id) {
+ const events = await testing.findSeries(id);
+ assert.equal(events.length, 0);
+}
+
+// helper to send a delete request using forms or raw json post
+function requestDelete({id, secret, useForm}) {
+ const path = '/api/delete_event.php';
+ const data = { id, secret };
+ if (!useForm) {
+ return request(app).post(path).send(data);
+ } else {
return request(app)
- .post(delete_api)
+ .post(path)
+ .type('form')
.send({
- id: 1, // id 1 is hidden null
- secret: testData.secret,
- })
- .expect(200)
- .expect('Content-Type', /json/)
- .expect('Api-Version', /^3\./)
- .then(async (res) => {
- assert.equal(spy.eventErasures.callCount, 0);
- spy.resetHistory();
- const e0 = await CalEvent.getByID(1);
- assert.equal(e0.review, 'E');
- assert.ok(!e0.password);
+ json: JSON.stringify(data)
});
- });
-});
+ }
+}
\ No newline at end of file
diff --git a/app/test/events_test.js b/app/test/events_test.js
index 9e5d7dd5f..c26977393 100644
--- a/app/test/events_test.js
+++ b/app/test/events_test.js
@@ -1,36 +1,32 @@
-const app = require("../appEndpoints");
-const testdb = require("./testdb");
-const testData = require("./testData");
-//
-const { describe, it, before, after } = require("node:test");
-const assert = require("node:assert/strict");
+const assert = require('node:assert/strict');
+const { describe, it, beforeEach, afterEach } = require('node:test');
const request = require('supertest');
+//
+const app = require('shift-docs/appEndpoints');
+const testing = require('shift-docs/testing');
describe("getting events", () => {
- // runs before the evt test in this block.
- before(() => {
- return testdb.setupTestData("events");
+ beforeEach(() => {
+ testing.configure("json");
+ return testing.setupTestData("getting events");
});
- // runs once after the last test in this block
- after(() => {
- return testdb.destroy();
+ afterEach(() => {
+ return testing.reset();
});
- it("errors with no parameters", () => {
+ it("reports error 400 with no parameters", () => {
return request(app)
.get('/api/events.php')
- .then(testData.expectError);
+ .then(testing.expectError);
});
it("reports 404 for an id that doesn't match a visible event", () => {
return request(app)
.get('/api/events.php')
- .query({
- id:999
- })
+ .query({ id: 999 })
// an id that matches nothing is "not found", not a bad request.
// re: #566
- .then(res => testData.expectError(res, null, 404));
+ .then(res => testing.expectError(res, null, 404));
});
- it("errors on an invalid date", () => {
+ it("reports error 400 on an invalid date", () => {
return request(app)
.get('/api/events.php')
.query({
@@ -41,7 +37,7 @@ describe("getting events", () => {
// startdate: "2002/05/06",
// enddate : "2002/05/06",
})
- .then(testData.expectError);
+ .then(testing.expectError);
});
it("reports 413 on too large a range", () => {
return request(app)
@@ -50,7 +46,7 @@ describe("getting events", () => {
startdate: "2002-01-01",
enddate : "2003-01-01",
})
- .then(res => testData.expectError(res, null, 413));
+ .then(res => testing.expectError(res, null, 413));
});
it("reports 422 on a negative range", () => {
return request(app)
@@ -60,17 +56,13 @@ describe("getting events", () => {
enddate : "2002-01-01",
})
// parseable, but not a range we can answer. re: #559
- .then(res => testData.expectError(res, null, 422));
+ .then(res => testing.expectError(res, null, 422));
});
- it("succeeds with a valid id", () => {
+ it("succeeds with a valid pkid", () => {
return request(app)
.get('/api/events.php')
- .query({
- id: 201, // a daily id
- })
- .expect(200)
- .expect('Content-Type', /json/)
- .expect('Api-Version', /^3\./)
+ .query({ id: 201 })
+ .then(testing.expectOkay)
.then(res => {
assert.equal(res.body?.events?.length, 1);
const evt = res.body.events[0];
@@ -78,10 +70,11 @@ describe("getting events", () => {
assert.equal(evt.caldaily_id, '201');
assert.equal(evt.hideemail, true, "the test data has the email hidden");
assert.equal(evt.email, null, "with no secret the email should be nil");
+ assert.equal(evt.endtime, '20:00:00');
assert.equal(res.body.pagination, undefined, "only ranges should have pagination");
});
});
- it("returns an empty array for a range with no events", () => {
+ it("succeeds with an empty array for a range with no events", () => {
// unlike a missing id, an empty range is a meaningful answer: nothing is
// scheduled then. it must stay a 200, not become a 404.
return request(app)
@@ -90,8 +83,7 @@ describe("getting events", () => {
startdate: "2002-09-01",
enddate : "2002-09-02",
})
- .expect(200)
- .expect('Content-Type', /json/)
+ .then(testing.expectOkay)
.then(res => {
assert.deepEqual(res.body.events, []);
assert.equal(res.body.pagination.events, 0);
@@ -104,9 +96,7 @@ describe("getting events", () => {
startdate: "2002-08-01",
enddate : "2002-08-02",
})
- .expect(200)
- .expect('Content-Type', /json/)
- .expect('Api-Version', /^3\./)
+ .then(testing.expectOkay)
.then(res => {
// console.log(res.body);
assert.equal(res.body?.events?.length, 2);
@@ -123,15 +113,6 @@ describe("getting events", () => {
assert.match(page.next, /\?startdate=2002-08-03&enddate=2002-08-04$/);
});
});
-});
-
-describe("event order", () => {
- before(() => {
- return testdb.setupFakeData("events-order");
- });
- after(() => {
- return testdb.destroy();
- });
it("sorts events by date and then by time", () => {
return request(app)
.get('/api/events.php')
diff --git a/app/test/facade_test.js b/app/test/facade_test.js
index 4650b3338..73b18fcc9 100644
--- a/app/test/facade_test.js
+++ b/app/test/facade_test.js
@@ -4,11 +4,15 @@ const facade = require("../facade");
//
const { describe, it } = require("node:test");
const assert = require("node:assert/strict");
+const path = require('node:path');
const request = require("supertest");
// exercises the real (development only) image handler.
describe("facade event images", () => {
const app = express();
+ // during unit testing, config sets the image dir to the temp directory
+ // but, this test wants to check for the existing image files
+ config.image.dir = path.resolve(config.appPath, "images");
facade.serveImages(app, config);
it("serves an existing image", () => {
diff --git a/app/test/fakeData.js b/app/test/fakeData.js
deleted file mode 100644
index 9563b77e3..000000000
--- a/app/test/fakeData.js
+++ /dev/null
@@ -1,204 +0,0 @@
-const { faker } = require('@faker-js/faker');
-const db = require("shift-docs/db");
-const config = require("shift-docs/config");
-const dt = require("shift-docs/util/dateTime");
-const { Area, Audience, DatesType, EventStatus, RideLength } = require('shift-docs/models/calConst');
-
-// password shared for all fake events
-const password = "supersecret";
-// number of days within which to create fake events
-const fakeRange = 7;
-
-// promise an array of events
-// - firstDay is a dt day
-// - numEvents: a number of events to create
-// - seed: an optional random seed
-function makeFakeData(firstDay, lastDay, numEvents) {
- const fakeData = generateFakeData(firstDay, lastDay, numEvents);
- const promisedEvents = fakeData.map(data => {
- return db.query('calevent')
- .insert(data.event).then(row => {
- const id = row[0]; // the magic to get the event id.
- // when passing a seed (ex. for tests); silence the output.
- if (!config.isTesting) {
- logData(id, data);
- }
- const promisedDays = data.days.map(at => {
- at.id = id; // assign the id before adding to the db
- return db.query('caldaily').insert(at);
- });
- return Promise.all(promisedDays);
- });
- });
- // wait on all the days to finish.
- return Promise.all(promisedEvents);
-}
-
-//
-function logData(id, data) {
- const url = config.site.url("addevent", `edit-${id}-${password}`);
- const start = dt.friendlyDate(data.days[0].eventdate);
- console.log(`created "${data.event.title}" with ${data.days.length} days starting on ${start}\n ${url}`);
-}
-
-// build the data before inserting into the db
-// this avoids race conditions influencing the data generated.
-function generateFakeData(firstDay, lastDay, numEvents) {
- const out = [];
- for (let i = 0; i< numEvents; i++) {
- // always have one event on the day specified;
- // on subsequent events, pick from a range of days.
- const start = !i ? firstDay:
- dt.convert(faker.date.between({
- from: firstDay.toDate(),
- to: lastDay.toDate(),
- }));
- const title = faker.music.songName();
- const event = makeCalEvent(title);
- const numDays = randomDayCount();
- const days = makeCalDailies(start, numDays);
- out.push({
- event,
- days,
- });
- }
- return out;
-}
-
-// export!
-module.exports = { makeFakeData };
-
-function randomDayCount() {
- // some dumb weighted random
- const pick = [1,1,1,1,1,2,2,2,3,3,4,5];
- return faker.helpers.arrayElement(pick);
-}
-
-function randomRideLength() {
- const pick = Object.keys(RideLength);
- return faker.helpers.arrayElement(pick);
-}
-
-// days is the range of possible days in the future
-// returns javascript date
-function nextDay(days, refDate) {
- return !refDate? faker.date.soon(days) : faker.date.soon({
- days,
- refDate,
- });
-}
-
-function makeCalDailies(start, numDays) {
- const out = [];
- const active = faker.datatype.boolean(0.8);
- const flash = faker.datatype.boolean(!active? 0.8: 0.3);
- const msg = flash ? (active? faker.vehicle.bicycle() :
- faker.word.adverb() + " cancelled"): null;
- for (let i=0; i0) {
- // 1 to 3 days from the first day
- start = start.add(1 + faker.number.int(3), 'days');
- }
- out.push({
- eventdate : db.toDate(start),
- eventstatus : active? EventStatus.Active : EventStatus.Cancelled,
- newsflash : msg,
- });
- }
- return out;
-}
-
-function capitalize(str, yes= true) {
- const first = str.charAt(0);
- return (yes? first.toUpperCase() : first.toLowerCase() ) + str.slice(1);
-}
-
-function makeCalEvent(title) {
- const firstName = faker.person.firstName();
- const lastName = faker.person.lastName();
- const organizer = faker.person.fullName({firstName, lastName});
- const email = faker.internet.exampleEmail({firstName, lastName});
- const hideemail = faker.datatype.boolean(0.75) ? 1: 0;
- const printInfo = faker.datatype.boolean(0.5) ? 1: 0;
- const phone = faker.phone.number();
- const weburl = faker.datatype.boolean(0.5) ? null:
- faker.internet.url();
- const webname = faker.datatype.boolean(0.5) ? null:
- faker.word.verb() + " " +
- faker.word.adjective() + " " +
- faker.word.noun();
- const audience = faker.helpers.objectValue( Audience );
- const descr = faker.hacker.phrase();
- // this would normally be the event id; remove if this is a problem.
- const image = faker.datatype.boolean(0.75) ? null: "bike.jpg";
- // "19:00:00"
- const eventtime = String(6+ faker.number.int(12)).padStart(2, "0") + ":" +
- String(15* faker.number.int(3)).padStart(2, "0") + ":00";
- // in minutes, but sometimes not specified at all
- const eventduration = faker.datatype.boolean(0.25) ? null :
- (6* faker.number.int(3) * 15);
- const timedetails = faker.datatype.boolean(0.75) ? null :
- faker.lorem.sentences({ min: 1, max: 3 });
- // mostly portland:
- const area = faker.datatype.boolean(0.25) ?
- faker.helpers.objectValue( Area ) :
- Area.Portland;
- const highlight = faker.datatype.boolean(0.1) ? 1: 0;
- const safetyplan = faker.datatype.boolean(0.75) ? 1: 0;
- const loopride = faker.datatype.boolean(0.5) ? 1: 0;
- const locname = capitalize(faker.word.adverb()) + " " +
- capitalize(faker.animal.fish(), false);
-
- const address = faker.location.streetAddress();
- const locdetails = faker.datatype.boolean(0.25) ? null :
- faker.company.catchPhrase();
- const locend = faker.datatype.boolean(0.50) ? null :
- faker.location.streetAddress();
- const ridelength = randomRideLength();
-
- // constants:
- const changes = 1;
- const hidden = 0; // never hidden
- const datestype = DatesType.OneDay;
-
- return {
- // created,
- // modified,
- changes,
- // id: eventId,
- name: organizer,
- email,
- hideemail,
- printemail: printInfo,
- phone,
- hidephone: hideemail,
- printphone: printInfo,
- weburl,
- webname,
- printweburl: printInfo,
- contact: organizer,
- hidecontact : 1,
- printcontact : printInfo,
- title: title,
- tinytitle: title,
- audience,
- descr,
- printdescr: descr,
- image,
- datestype,
- eventtime,
- eventduration,
- timedetails,
- locname,
- address,
- locdetails,
- locend,
- loopride,
- ridelength,
- area,
- highlight,
- hidden,
- password,
- safetyplan,
- };
-}
diff --git a/app/test/ical_test.js b/app/test/ical_test.js
index 8fee5f5c7..c0b89ded8 100644
--- a/app/test/ical_test.js
+++ b/app/test/ical_test.js
@@ -1,28 +1,22 @@
-const sinon = require('sinon');
-const app = require("../appEndpoints");
-const testData = require("./testData");
-const testdb = require("./testdb");
-
-const { CalEvent } = require("../models/calEvent");
-const { CalDaily } = require("../models/calDaily");
-const { EventStatus } = require("../models/calConst");
-//
-const { describe, it, before, after } = require("node:test");
-const assert = require("node:assert/strict");
+const assert = require('node:assert/strict');
+const { describe, it, beforeEach, afterEach } = require('node:test');
const request = require('supertest');
-
-const CalendarType = /^text\/calendar/;
+const sandbox = require('sinon').createSandbox();
+//
+const app = require('shift-docs/appEndpoints');
+const testing = require('shift-docs/testing');
+const db = require('shift-docs/db');
describe("ical feed", () => {
- // runs before the evt test in this block.
- before(() => {
- testData.stubData(sinon);
- return testdb.setupTestData("ical");
+ beforeEach(() => {
+ testing.fakeNow(sandbox);
+ testing.fakeSiteUrl(sandbox, "https://shift2bikes.org");
+ testing.configure("ical");
+ return testing.setupTestData("ical");
});
- // runs once after the last test in this block
- after(() => {
- sinon.restore();
- return testdb.destroy();
+ afterEach(() => {
+ sandbox.restore();
+ return testing.reset();
});
it("reports 404 for a series id that matches no events", () => {
// 'id' aliases to series_id. an id that matches nothing is
@@ -86,11 +80,7 @@ describe("ical feed", () => {
it("supports an 'all events' feed", () => {
return request(app)
.get('/api/ical.php')
- // test that it also has an api version
- // that exists everywhere, but only tested here.
- .expect('Api-Version', /^3\./)
- .expect(200)
- .expect('content-type', CalendarType);
+ .then(testing.expectOkay);
});
it("provides the days of a single event", () => {
return request(app)
@@ -98,11 +88,8 @@ describe("ical feed", () => {
.query({
id: 2, // an event id
})
- .expect(200)
- .expect('content-type', CalendarType)
- .then(res => {
- assert.equal(res.text, allEvents);
- });
+ .then(testing.expectOkay)
+ .then(res => assert.equal(res.text, testing.expectedResults.allEvents));
});
it("provides a range of days", () => {
return request(app)
@@ -111,8 +98,7 @@ describe("ical feed", () => {
startdate: "2002-08-01",
enddate : "2002-08-02",
})
- .expect(200)
- .expect('content-type', CalendarType);
+ .then(testing.expectOkay);
});
it("can return an empty range", () => {
return request(app)
@@ -121,11 +107,8 @@ describe("ical feed", () => {
startdate: "2002-01-01",
enddate : "2002-01-02",
})
- .expect(200)
- .expect('content-type', CalendarType)
- .then(res => {
- assert.equal(res.text, emptyRange);
- });
+ .then(testing.expectOkay)
+ .then(res => assert.equal(res.text, testing.expectedResults.emptyRange));
});
it("has a special pedalpalooza feed", () => {
return request(app)
@@ -135,45 +118,25 @@ describe("ical feed", () => {
enddate : "2002-08-02",
filename : "pedalpalooza-2024.ics",
})
- .expect(200)
- .expect('content-type', CalendarType)
- .then(res => {
- assert.equal(res.text, pedalpaloozaFeed);
- });
+ .then(testing.expectOkay)
+ .then(res => assert.equal(res.text, testing.expectedResults.pedalpaloozaFeed));
});
it("can handle a canceled event", () => {
- return CalEvent.getByID(2).then(evt => {
- // todo: create a separate test where these values are nil and zero.
- // that had caused a bad feed at one point; its fixed but still good to test.
- // evt.eventtime = null;
- // evt.eventduration = 0;
- return evt._store().then(() => {
- return CalDaily.getForTesting(201).then(d => {
- d.eventstatus = EventStatus.Cancelled;
- return d._store().then(_ => {
- return request(app)
- .get('/api/ical.php')
- .query({
- id: 2, // an event id
- })
- .expect(200)
- .expect('content-type', CalendarType)
- .then(res => {
- assert.equal(res.text, cancelledDay);
- });
- });
- });
- });
- });
- });
-});
-
-describe("ical feed order", () => {
- before(() => {
- return testdb.setupFakeData("ical-order");
- });
- after(() => {
- return testdb.destroy();
+ return Promise.resolve()
+ // cancel all events on the indicated day.
+ .then(_ => {
+ return db.query.table('cal_schedule')
+ .update({is_scheduled: 0})
+ .where('ymd', '2002-08-01')
+ })
+ // query an event that contains that day.
+ .then(_ => {
+ return request(app)
+ .get('/api/ical.php')
+ .query({id: 2})
+ })
+ .then(testing.expectOkay)
+ .then(res => assert.equal(res.text, testing.expectedResults.cancelledDay));
});
it("sorts events by start time", () => {
return request(app)
@@ -190,139 +153,3 @@ describe("ical feed order", () => {
});
});
});
-
-const shiftHeader = [
-String.raw`VERSION:2.0`,
-String.raw`PRODID:-//shift2bikes.org//NONSGML shiftcal v2.1//EN`,
-String.raw`METHOD:PUBLISH`,
-String.raw`X-WR-CALNAME:Shift Community Calendar`,
-String.raw`X-WR-CALDESC:Find fun bike events all year round.`,
-String.raw`X-WR-RELCALID:community@shift2bikes.org`,
-String.raw`X-WR-TIMEZONE:America/Los_Angeles`,
-];
-
-const pedalpHeader = [
-String.raw`VERSION:2.0`,
-String.raw`PRODID:-//shift2bikes.org//NONSGML shiftcal v2.1//EN`,
-String.raw`METHOD:PUBLISH`,
-String.raw`X-WR-CALNAME:Pedalpalooza Bike Calendar`,
-String.raw`X-WR-CALDESC:Find fun Pedalpalooza bike events!`,
-String.raw`X-WR-RELCALID:shift@shift2bikes.org`,
-String.raw`X-WR-TIMEZONE:America/Los_Angeles`,
-];
-
-const event1 = [
-String.raw`BEGIN:VEVENT`,
-String.raw`UID:event-201@shift2bikes.org`,
-String.raw`SUMMARY:ride 2 title`,
-String.raw`CONTACT:organizer`,
-String.raw`DESCRIPTION:news flash\nPeior amicitia texo. Delectus sequi qui temporibus`,
-String.raw` clibanus. Creber creo adamo aedificium creta bardus aegre.\ntime `,
-String.raw` details\nEnds at location\; end.\nhttp://localhost:3080/calendar/event-201`,
-String.raw`LOCATION:location\, name.\n\nlocation && details`,
-String.raw`STATUS:CONFIRMED`,
-String.raw`DTSTART:20020802T020000Z`,
-String.raw`DTEND:20020802T030000Z`,
-String.raw`CREATED:19930728T213900Z`,
-String.raw`DTSTAMP:19930828T090700Z`,
-String.raw`SEQUENCE:2`,
-String.raw`URL:http://localhost:3080/calendar/event-201`,
-String.raw`END:VEVENT`,
-];
-const event2 = [
-String.raw`BEGIN:VEVENT`,
-String.raw`UID:event-202@shift2bikes.org`,
-String.raw`SUMMARY:ride 2 title`,
-String.raw`CONTACT:organizer`,
-String.raw`DESCRIPTION:news flash\nPeior amicitia texo. Delectus sequi qui temporibus`,
-String.raw` clibanus. Creber creo adamo aedificium creta bardus aegre.\ntime `,
-String.raw` details\nEnds at location\; end.\nhttp://localhost:3080/calendar/event-202`,
-String.raw`LOCATION:location\, name.\n\nlocation && details`,
-String.raw`STATUS:CONFIRMED`,
-String.raw`DTSTART:20020803T020000Z`,
-String.raw`DTEND:20020803T030000Z`,
-String.raw`CREATED:19930728T213900Z`,
-String.raw`DTSTAMP:19930828T090700Z`,
-String.raw`SEQUENCE:2`,
-String.raw`URL:http://localhost:3080/calendar/event-202`,
-String.raw`END:VEVENT`,
-];
-
-
-const allEvents = [
-String.raw`BEGIN:VCALENDAR`,
-...shiftHeader,
-...event1,
-...event2,
-String.raw`END:VCALENDAR`,
-"" // trailing new line. i think.
-].join("\r\n");
-
-const emptyRange = [
-String.raw`BEGIN:VCALENDAR`,
-...shiftHeader,
-String.raw`END:VCALENDAR`,
-"" // trailing new line. i think.
-].join("\r\n");
-
-const canceled1= [
-String.raw`BEGIN:VEVENT`,
-String.raw`UID:event-201@shift2bikes.org`,
-String.raw`SUMMARY:CANCELLED: ride 2 title`,
-String.raw`CONTACT:organizer`,
-String.raw`DESCRIPTION:news flash\nPeior amicitia texo. Delectus sequi qui temporibus`,
-String.raw` clibanus. Creber creo adamo aedificium creta bardus aegre.\ntime `,
-String.raw` details\nEnds at location\; end.\nhttp://localhost:3080/calendar/event-201`,
-String.raw`LOCATION:location\, name.\n\nlocation && details`,
-String.raw`STATUS:CANCELLED`,
-String.raw`DTSTART:20020802T020000Z`,
-String.raw`DTEND:20020802T030000Z`,
-String.raw`CREATED:19930728T213900Z`,
-String.raw`DTSTAMP:19930828T090700Z`,
-String.raw`SEQUENCE:2`,
-String.raw`URL:http://localhost:3080/calendar/event-201`,
-String.raw`END:VEVENT`,
-];
-
-const cancelledDay = [
-String.raw`BEGIN:VCALENDAR`,
-...shiftHeader,
-...canceled1,
-...event2,
-String.raw`END:VCALENDAR`,
-"" // trailing new line. i think.
-].join("\r\n");
-
-const pedalEnd = [
-String.raw`BEGIN:VEVENT`,
-String.raw`UID:pedalpalooza-2002-end@shift2bikes.org`,
-String.raw`SUMMARY:Pedalpalooza 2002 is over!`,
-String.raw`CONTACT:bikecal@shift2bikes.org`,
-String.raw`DESCRIPTION:We hope you've had a great bike summer and a great `,
-String.raw` Pedalpalooza!!!\nWhile Pedalpalooza is done\, there is still plenty of `,
-String.raw` bike fun to be found on the Shift2bikes website. And you can also `,
-String.raw` subscribe to the Shift community calendar to see those rides.\nVisit `,
-String.raw` https://shift2bikes.org/calendar/ for more details.`,
-String.raw`LOCATION:Portland\, and beyond!`,
-String.raw`STATUS:CONFIRMED`,
-// midnight on the last day
-String.raw`DTSTART:20020803T070000Z`,
-// the end of the next day
-String.raw`DTEND:20020804T065959Z`,
-// fake created date ( in the same year )
-String.raw`CREATED:20020601T070000Z`,
-String.raw`DTSTAMP:20020601T070000Z`,
-String.raw`SEQUENCE:1`,
-String.raw`URL:https://shift2bikes.org/calendar/`,
-String.raw`END:VEVENT`,
-];
-
-const pedalpaloozaFeed = [
-String.raw`BEGIN:VCALENDAR`,
-...pedalpHeader,
-...event1,
-...event2,
-...pedalEnd,
-String.raw`END:VCALENDAR`,
-"" // trailing new line. i think.
-].join("\r\n");
diff --git a/app/test/manage_test.js b/app/test/manage_test.js
index fd9bd113c..a15d8bc5a 100644
--- a/app/test/manage_test.js
+++ b/app/test/manage_test.js
@@ -11,36 +11,56 @@
// x add / cancel dates from a published event
// x raw json ( curl ) vs body json ( forms )
// x multi-part form ( attach image )
-const fs = require('fs');
-const fsp = fs.promises;
+const assert = require('node:assert/strict');
+const {
+ describe,
+ it,
+ beforeEach,
+ afterEach,
+} = require('node:test');
+const fsp = require('node:fs').promises;
const path = require('node:path');
-const sinon = require('sinon');
-const app = require("../appEndpoints");
-const config = require("../config");
-const testdb = require("./testdb");
-const testData = require("./testData");
-
+const sandbox = require('sinon').createSandbox();
+const { faker } = require('@faker-js/faker');
+const request = require('supertest');
+const app = require('shift-docs/appEndpoints');
+const config = require('shift-docs/config');
+const emailer = require('shift-docs/emailer');
+const testing = require('shift-docs/testing');
+const misc = require('shift-docs/util/misc');
+const images = require('shift-docs/images');
const { CalEvent } = require("../models/calEvent");
const { CalDaily } = require("../models/calDaily");
//
-const { describe, it, beforeEach, afterEach } = require("node:test");
-const assert = require("node:assert/strict");
-const request = require('supertest');
-//
const manage_api = '/api/manage_event.php';
describe("managing events", () => {
- let spy;
+ let getMostRecentlySentEmail;
// reset after each one.
beforeEach(() => {
- spy = testData.stubData(sinon);
- return testdb.setupTestData("manage");
+ testing.configure("json");
+ testing.fakeSiteUrl(sandbox);
+ // listen to email sending.
+ // requires callers to use emailer.sendMail()
+ sandbox
+ .stub(emailer, "sendMail")
+ .callsFake((email) => {
+ getMostRecentlySentEmail = email;
+ return Promise.resolve(true);
+ })
+ .callThrough();
+ // temporarily replace the secret generation
+ // requires callers to use misc.newSecret()
+ sandbox.stub(misc, "newSecret").callsFake((_) => {
+ return faker.string.uuid().replaceAll("-", "");
+ });
+ return testing.initTestDB("manage");
});
afterEach(() => {
- sinon.restore();
- return testdb.destroy();
+ sandbox.restore();
+ return testing.reset();
});
- it("errors on an invalid id", () => {
+ it("reports error 400 on an invalid id", () => {
return request(app)
.post(manage_api)
.type('form')
@@ -49,27 +69,32 @@ describe("managing events", () => {
id: 999,
})
})
- .then(testData.expectError);
+ .then(testing.expectError);
});
- it("creates a new event, using raw json", () => {
+ it("creates a new event, using raw json", async () => {
+ await testing.createTestData([1, 2, 3]);
return request(app)
.post(manage_api)
- .send(eventData)
- .expect(200)
- .expect('Content-Type', /json/)
- .expect('Api-Version', /^3\./)
+ .send(requestedSeries)
+ .expect(testing.expectOkay)
.then(async (res) => {
- assert.equal(spy.eventStore.callCount, 1, "event stores");
- assert.equal(spy.dailyStore.callCount, 2, "daily store");
- spy.resetHistory();
-
const id = res.body.id;
- const evt = await CalEvent.getByID(id);
- assert.equal(evt.hidden, 1, "the initial event should be hidden by default");
+ const series = await CalEvent.getByID(id);
+ assert.equal(series.hidden, 1,
+ "the initial event should be hidden by default",
+ );
+ const schedule = await CalDaily.getStatusesByEventId(id);
+ assert.equal(schedule.length, 2,
+ "there are two days in the posted data");
+ // console.log(res.body);
+ assert.deepStrictEqual(
+ getMostRecentlySentEmail,
+ testing.expectedResults.createdEmail,
+ );
// console.log(res.body);
});
});
- it("fail creation when missing required fields", () => {
+ it("reports error 400 on creation when missing required fields", () => {
// each time substitute a field value that should fail
const pairs = [
"title", "",
@@ -84,25 +109,22 @@ describe("managing events", () => {
"email", null,
"email", "i am not an email",
];
- let seq = Promise.resolve(""); // something to kick off the sequences.
+ let seq = Promise.resolve(); // something to kick off the sequences.
for (let i=0; i< pairs.length; i+=2) {
const key = pairs[i];
const value = pairs[i+1];
- const post = Object.assign({}, eventData);
+ const post = Object.assign({}, requestedSeries);
post[key] = value;
seq = seq.then(_ => {
return request(app)
.post(manage_api)
.send(post)
- .expect(400)
- .then(res => {
- assert.ok(res.body.error.fields[key]);
- });
+ .then((res) => testing.expectError(res, key));
})
}
return seq;
});
- it("fails creation when fields have invalid values", () => {
+ it("reports error 400 on creation when fields have invalid values", async () => {
const pairs = [
"eventduration", "i am not a number, i am a man!",
// converting directly toInt will ignore trailing text
@@ -113,79 +135,109 @@ describe("managing events", () => {
"hideemail", "wants bool",
"hidephone", 42,
"loopride", "wants bool",
- "datestype", "a long string",
+ "ridelength", 800,
"area", 23,
+ // ridelength very specifically turns invalid results into null values
+ // datestype is no longer used
];
- let seq = Promise.resolve(""); // something to kick off the sequences.
+ let seq = Promise.resolve(); // something to kick off the sequences.
for (let i=0; i< pairs.length; i+=2) {
const key = pairs[i];
const value = pairs[i+1];
- const post = Object.assign({}, eventData);
+ const post = Object.assign({}, requestedSeries);
post[key] = value;
seq = seq.then(_ => {
return request(app)
.post(manage_api)
.send(post)
- .expect(400)
- .then(res => {
- assert.ok(res.body.error.fields[key]);
- });
+ .then((res) => testing.expectError(res, key));
})
}
return seq;
});
- it("publishes an event", () => {
- // id three is unpublished
- return CalEvent.getByID(3).then(evt => {
- assert.equal(evt.isPublished(), false);
+ it("succeeds when ridelength is the client default value", async () => {
+ const post = Object.assign({}, requestedSeries, {ridelength: "--"});
+ return request(app)
+ .post(manage_api)
+ .send(post)
+ .then(testing.expectOkay);
+ });
+ it("publishes an event", async () => {
+ // in the predetermined test data, id three winds up being unpublished
+ await testing.createTestData([1, 2, 3]);
+ return CalEvent.getByID(3).then((initialEvent) => {
+ assert.equal(initialEvent.hidden, 1);
return request(app)
.post(manage_api)
// by adding the id and posting to it, we should be able to publish it.
.send(Object.assign({
id: 3,
- secret: testData.secret,
- }, eventData))
- .expect(200)
+ secret: testing.user.secret,
+ }, requestedSeries))
+ .expect(testing.expectOkay)
.then(async (res) => {
- assert.equal(spy.eventStore.callCount, 1, "event stores");
- spy.resetHistory();
- const evt = await CalEvent.getByID(3);
- assert.equal(evt.isPublished(), true);
+ assert.equal(res.body?.id, "3"); // the ids are sent back as strings.
+ assert.equal(res.body?.published, true);
+
+ const publishedEvent = await CalEvent.getByID(3);
+ assert.equal(publishedEvent.isPublished(), true);
+ // look over the days we requested
+ // to validate what wound up in the db.
+ const schedule = await CalDaily.getStatusesByEventId(3);
+ assert.equal(schedule.length, 2);
+ //
+ requestedSchedule.forEach((requested, i) => {
+ const at = schedule[i]; // raw db data
+ // one of our requested newsflashes is missing;
+ // that should result in a null database value.
+ assert.equal(
+ at.newsflash,
+ requested.newsflash || null,
+ `news mismatch ${i}`,
+ );
+ assert.equal(at.date, requested.date, `date mismatch ${i}`);
+ assert.equal(
+ at.status,
+ requested.status,
+ `status mismatch ${i}`,
+ );
+ });
});
});
});
- it("reports 403 for an empty secret", () => {
+ it("reports 403 for a missing secret", async () => {
+ await testing.createTestData([1, 2, 3]);
return request(app)
.post(manage_api)
.send(Object.assign({
id: 3,
// not sending any secret
- }, eventData))
- .then(res => testData.expectError(res, null, 403));
+ }, requestedSeries))
+ .then(res => testing.expectError(res, null, 403));
});
- it("reports 403 for an invalid secret", () => {
+ it("reports 403 for a mismatched secret", () => {
return request(app)
.post(manage_api)
.send(Object.assign({
id: 3, // reverses the secret:
- secret: testData.secret.split("").reverse().join(""),
- }, eventData))
- .then(res => testData.expectError(res, null, 403));
+ secret: testing.user.secret.split("").reverse().join(""),
+ }, requestedSeries))
+ .then(res => testing.expectError(res, null, 403));
});
- it("adds one date and removes another", () => {
+ it("adds one date and removes another", async () => {
+ await testing.createTestData([1, 2, 3]);
return CalEvent.getByID(2).then(evt => {
- //
- const post = Object.assign( {
- secret: testData.secret,
+ const post = Object.assign({
+ secret: testing.user.secret,
code_of_conduct: "1",
read_comic: "1",
datestatuses : [
// keep the first date;
{ "date": "2002-08-01", status: 'A' },
- // implicitly cancel the second;
+ // implicitly remove the second;
// add a third.
{ "date": "2002-08-03", status: 'A' }
- ]}, evt.getJSON({includePrivate:true}));
+ ]}, evt.getSummary({includePrivate:true}));
//
return request(app)
.post(manage_api)
@@ -193,87 +245,54 @@ describe("managing events", () => {
.send({
json: JSON.stringify(post)
})
- .expect(200)
+ .expect(testing.expectOkay)
.then(async (res) => {
- assert.equal(spy.eventStore.callCount, 1, "event stores");
- assert.equal(spy.dailyStore.callCount, 3, "daily store");
- spy.resetHistory();
- // three dailies for our event are in the db:
- const dailies = await CalDaily.getByEventID(2);
- assert.equal(dailies.length, 3);
- assert.equal(dailies[0].isUnscheduled(), false);
- assert.equal(dailies[1].isUnscheduled(), true);
- assert.equal(dailies[2].isUnscheduled(), false);
+ assert.equal(res.body.id, "2");
+ assert.equal(res.body.published, true);
+ // two dailies for our event are in the db:
+ const dailies = await CalDaily.getStatusesByEventId(2);
// only two should be in the returned data
// ( the second one is delisted; filtered by reconcile )
// fix: should add a test for an explicitly canceled day.
- assert.deepEqual(res.body.datestatuses, [{
- "id": "201",
- "date": "2002-08-01",
- "status": "A",
- "newsflash": null,
- }, {
- "id": "203", // the new id is one after the last one
- "date": "2002-08-03",
- "status": "A",
- "newsflash": null,
- }
- ]);
+ assert.deepEqual(dailies[0], {
+ "id": "201",
+ "date": "2002-08-01",
+ "status": "A",
+ "newsflash": null,
+ });
+ assert.deepEqual(dailies[1], {
+ "id": "303", // the auto-generated id is after the last manually created one
+ "date": "2002-08-03",
+ "status": "A",
+ "newsflash": null,
+ });
});
});
});
- function getImageTarget(id, imageSource) {
- return path.join( config.image.dir, id + path.extname(imageSource) );
- };
- function postImage(id, imageSource, imageTarget) {
- // remove any image from earlier tests:
- return fsp.rm(imageTarget, {force:true}).then(_ => {
- // act as if we are a client who just created an event
- // and is posting it back up again, along with the new image.
- return CalEvent.getByID(3).then(evt => {
- const statuses = CalDaily.getStatusesByEventId(evt.id);
- return evt.getDetails(statuses, {includePrivate:true}).then(eventData => {
- const post = Object.assign( {
- secret: testData.secret,
- code_of_conduct: "1",
- read_comic: "1",
- }, eventData);
- return request(app)
- .post(manage_api)
- .type('form')
- .field({
- json: JSON.stringify(post)
- })
- // the tests originally based a filepath here
- // but that started generating EPIPE errors for reasons.
- .attach('file', fs.readFileSync(imageSource), path.basename(imageSource));
- });
- });
- });
- }
- it("attaches an image", () => {
- const imageSource = path.join( config.image.dir, "bike.jpg" );
+ it("attaches an image", async () => {
+ await testing.createTestData([1, 2, 3]);
+ const imageSource = images.bike.jpg;
const imageTarget = getImageTarget(3, imageSource);
return postImage(3, imageSource, imageTarget)
+ .then(testing.expectOkay)
.then(res => {
- assert.equal(res.status, 200);
return CalEvent.getByID(3).then(evt => {
- // event creation is change 1,
- // the image post is change 2,
- // the event id is 3.
- assert.equal(evt.image, "3-2.jpg", "image names should have a sequence number");
+ // FIX: the number changed here.
+ // could there be a bug?
+ assert.equal(evt.image, "3-1.jpg", "image names should have a sequence number");
//
const imageTarget = getImageTarget(3, imageSource);
return fsp.stat(imageTarget); // rejects if it doesn't exist on disk.
});
});
});
- it("fails too large", () => {
- const imageSource = path.join( config.image.dir, "bike-big.png" );
+ it("reports error 400 on an oversized image", async () => {
+ await testing.createTestData([1, 2, 3]);
+ const imageSource = images.bike.big;
const imageTarget = getImageTarget(3, imageSource);
return postImage(3, imageSource, imageTarget)
.then(res => {
- testData.expectError(res, 'image');
+ testing.expectError(res, 'image');
return fsp.stat(imageTarget)
.then(_ => {
assert.fail(`didn't expect ${imageTarget} to exists`);
@@ -283,11 +302,12 @@ describe("managing events", () => {
});
});
});
- it("fails bad format", () => {
- const imageSource = path.join( config.image.dir, "bike-bad.tiff" );
+ it("reports error 400 on an unsupported image format", async () => {
+ await testing.createTestData([1, 2, 3]);
+ const imageSource = images.bike.unsupportedFormat;
const imageTarget = getImageTarget(3, imageSource);
return postImage(3, imageSource, imageTarget).then(res => {
- testData.expectError(res, 'image');
+ testing.expectError(res, 'image');
return fsp.stat(imageTarget)
.then(_ => {
assert.fail(`didn't expect ${imageTarget} to exists`);
@@ -297,41 +317,79 @@ describe("managing events", () => {
});
});
});
- it("prevents image upload on new events", () => {
- const imageSource = path.join( config.image.dir, "bike.jpg" );
+ it("reports error 400 when attaching images on new events", async () => {
+ const imageSource = images.bike.jpg;
// follows from "creates a new event" which would normally succeed
// only we attach an image and it should fail because that's diallowed.
- return request(app)
- .post(manage_api)
- .type('form')
- .field({
- json: JSON.stringify(eventData)
- })
- .attach('file', fs.readFileSync(imageSource), path.basename(imageSource))
- .then(res => {
- testData.expectError(res, 'image');
- });
+ return fsp.readFile(imageSource).then((fileData) => {
+ return request(app)
+ .post(manage_api)
+ .type('form')
+ .field({
+ json: JSON.stringify(requestedSeries)
+ })
+ .attach('file', fileData, path.basename(imageSource))
+ .then(res => testing.expectError(res, 'image'));
+ });
});
});
-// minimal json for pushing a new event
-const eventData = {
- "title": "new event",
- "details": "some details",
- "venue": "the secret hideout",
- "address": "the location of the secret hideout",
- "organizer": "js test",
- "email": "test@example.com",
- "code_of_conduct": "1",
- "read_comic": "1",
- "time": "3:15 PM", // time is sent with meridian for some reason.
- // currently, these aren't actually needed...
- // tbd: maybe it should require at least one when creating an event?
- "datestatuses": [{
- "date": "2023-05-24",
- },{
- "date": "2023-05-26",
- "newsflash": "not the news",
- }]
+function getImageTarget(id, imageSource) {
+ return path.join( config.image.dir, id + path.extname(imageSource) );
}
+function postImage(id, imageSource, imageTarget) {
+ // remove any image from earlier tests:
+ return fsp.rm(imageTarget, {force:true}).then(_ => {
+ // act as if we are a client who just created an event
+ // and is posting it back up again, along with the new image.
+ return CalEvent.getByID(3).then(evt => {
+ const statuses = CalDaily.getStatusesByEventId(evt.id);
+ return evt.getDetails(statuses, {includePrivate:true}).then(requestedSeries => {
+ const eventData = Object.assign({
+ secret: testing.user.secret,
+ code_of_conduct: "1",
+ read_comic: "1",
+ }, requestedSeries);
+ return fsp.readFile(imageSource).then((fileData) => {
+ return request(app)
+ .post(manage_api)
+ .type('form')
+ .field({
+ json: JSON.stringify(eventData)
+ })
+ // the tests originally based a filepath here
+ // but that started generating EPIPE errors for reasons.
+ .attach('file', fileData, path.basename(imageSource));
+ });
+ });
+ });
+ });
+}
+
+// some days to request
+const requestedSchedule = [
+ {
+ date: "2023-05-24",
+ status: "A",
+ },
+ {
+ date: "2023-05-26",
+ status: "C",
+ newsflash: "not the news",
+ },
+];
+
+// minimal json for pushing a new event
+const requestedSeries = {
+ title: "new event",
+ details: "some details",
+ venue: "the secret hideout",
+ address: "the location of the secret hideout",
+ organizer: "js test",
+ email: "test@example.com",
+ code_of_conduct: "1",
+ read_comic: "1",
+ time: "3:15 PM", // time is sent with meridian for some reason.
+ datestatuses: requestedSchedule,
+};
diff --git a/app/test/retrieve_test.js b/app/test/retrieve_test.js
index ec25814d5..0ce0d2a46 100644
--- a/app/test/retrieve_test.js
+++ b/app/test/retrieve_test.js
@@ -1,21 +1,21 @@
+const assert = require('node:assert/strict');
+const { describe, it, beforeEach, afterEach } = require('node:test');
+const request = require('supertest');
+//
+const app = require('shift-docs/appEndpoints')
+const testing = require('shift-docs/testing');
+const testdb = require('shift-docs/testing/testdb');
+// for timeout testing
const sinon = require('sinon');
-const app = require("../appEndpoints");
-const testData = require("./testData");
-const testdb = require("./testdb");
const { CalDaily } = require("../models/calDaily");
-//
-const { describe, it, before, after } = require("node:test");
-const assert = require("node:assert/strict");
-const request = require('supertest');
describe("retrieving event data for editing", () => {
- // runs before the first test in this block.
- before(() => {
- return testdb.setupTestData("retrieve");
+ beforeEach(() => {
+ testing.configure("json");
+ return testing.setupTestData("retrieve");
});
- // runs once after the last test in this block
- after(() => {
- return testdb.destroy();
+ afterEach(() => {
+ return testing.reset();
});
it("reports 404 on an invalid id", () => {
return request(app)
@@ -23,7 +23,7 @@ describe("retrieving event data for editing", () => {
.query({
id: 999
})
- .then(res => testData.expectError(res, null, 404));
+ .then(res => testing.expectError(res, null, 404));
});
it("private data requires the correct secret", () => {
return request(app)
@@ -32,9 +32,7 @@ describe("retrieving event data for editing", () => {
id: 2,
secret: "the incorrect answer to life, the universe, and this event.",
})
- .expect(200)
- .expect('Content-Type', /json/)
- .expect('Api-Version', /^3\./)
+ .then(testing.expectOkay)
.then(res => {
assert.equal(res.body.id, '2'); // a string
assert.equal(res.body.email, null, "email should be private");
@@ -47,11 +45,9 @@ describe("retrieving event data for editing", () => {
.get('/api/retrieve_event.php')
.query({
id: 2,
- secret: testData.secret,
+ secret: testing.user.secret,
})
- .expect(200)
- .expect('Content-Type', /json/)
- .expect('Api-Version', /^3\./)
+ .then(testing.expectOkay)
.then(res => {
// console.dir(res.body);
assert.equal(res.body.id, '2'); // a string
@@ -59,7 +55,7 @@ describe("retrieving event data for editing", () => {
assert.ok(res.body.phone, "b/c of the secret, phone should be present");
assert.ok(res.body.contact, "b/c of the secret, contact should be present");
assert.equal(res.body.hideemail, true, "the test data has the email hidden");
- assert.equal(res.body.email, testData.email, "b/c of the secret, we should see the email");
+ assert.equal(res.body.email, testing.user.email, "b/c of the secret, we should see the email");
assert.deepEqual(res.body.datestatuses, [{
id: '201',
date: '2002-08-01',
@@ -80,17 +76,16 @@ describe("retrieving event data for editing", () => {
.query({
id: 3
})
- .then(res => testData.expectError(res, null, 404));
+ .then(res => testing.expectError(res, null, 404));
});
it("errors on a hidden event, unless given the secret", () => {
return request(app)
.get('/api/retrieve_event.php')
.query({
id: 3,
- secret: testData.secret,
+ secret: testing.user.secret,
})
- .expect(200)
- .expect('Content-Type', /json/);
+ .then(testing.expectOkay);
});
it("reports a server error when reading the event times fails", async () => {
const stub = sinon.stub(CalDaily, 'getStatusesByEventId')
@@ -108,4 +103,3 @@ describe("retrieving event data for editing", () => {
}
});
});
-
diff --git a/app/test/ride_count_test.js b/app/test/ride_count_test.js
index 41b66df42..706b18f04 100644
--- a/app/test/ride_count_test.js
+++ b/app/test/ride_count_test.js
@@ -1,29 +1,28 @@
-const app = require("../appEndpoints");
-const testdb = require("./testdb");
-//
-const { describe, it, before, after } = require("node:test");
-const assert = require("node:assert/strict");
+const assert = require('node:assert/strict');
+const { describe, it, beforeEach, afterEach } = require('node:test');
const request = require('supertest');
+//
+const app = require('shift-docs/appEndpoints');
+const testing = require('shift-docs/testing');
describe("ride count testing", () => {
- // runs before the first test in this block.
- before(() => {
- return testdb.setupFakeData("count");
+ beforeEach(() => {
+ testing.configure("json");
+ return testing.setupFakeData("count");
});
- // runs once after the last test in this block
- after(() => {
- return testdb.destroy();
+ afterEach(() => {
+ return testing.reset();
});
it("handles an all encompassing range", () => {
return request(app)
.get('/api/ride_count.php')
.query({s: "1900-01-01", e: "2012-12-21"})
- .expect(200)
- .expect('Content-Type', /json/)
+ .then(testing.expectOkay)
.then(res => {
// past and upcoming are based on today's date
// all the test dates are earlier
- // TODO? set a global fake date, or fake some events in the future?
+ // TODO? set a global fake date,
+ // testing.fakeNow(sandbox);
assert.equal(res.body?.total, 75);
assert.equal(res.body?.past, 75);
assert.equal(res.body?.upcoming, 0);
@@ -33,23 +32,20 @@ describe("ride count testing", () => {
return request(app)
.get('/api/ride_count.php')
.query({s: "2002-08-10", e: "2002-08-11"})
- .expect(200)
- .expect('Content-Type', /json/)
+ .then(testing.expectOkay)
.then(res => {
assert.equal(res.body?.total, 4);
});
});
- it("errors on a missing time", () => {
+ it("reports error 400 on a missing time", () => {
return request(app)
.get('/api/ride_count.php')
- .expect(400)
- .expect('Content-Type', /json/);
+ .then(testing.expectError);
});
- it("errors on an invalid time", () => {
+ it("reports error 400 on an invalid time", () => {
return request(app)
.get('/api/ride_count.php')
.query({s: "yesterday", e: "tomorrow"})
- .expect(400)
- .expect('Content-Type', /json/);
+ .then(testing.expectError);
});
});
diff --git a/app/test/search_test.js b/app/test/search_test.js
index c78fcdfc4..a8f6de09e 100644
--- a/app/test/search_test.js
+++ b/app/test/search_test.js
@@ -1,27 +1,25 @@
-const app = require("../appEndpoints");
-const testdb = require("./testdb");
-const { EventSearch } = require("../models/calConst");
-//
-const { describe, it, before, after } = require("node:test");
-const assert = require("node:assert/strict");
+const assert = require('node:assert/strict');
+const { describe, it, beforeEach, afterEach } = require('node:test');
const request = require('supertest');
+//
+const app = require('shift-docs/appEndpoints');
+const testing = require('shift-docs/testing');
+const { EventSearch } = require('shift-docs/models/newConst');
describe("searching for events", () => {
- // runs before the first test in this block.
- before(() => {
- return testdb.setupFakeData("search");
+ beforeEach(() => {
+ testing.configure("json");
+ return testing.setupFakeData("search");
});
- // runs once after the last test in this block
- after(() => {
- return testdb.destroy();
+ afterEach(() => {
+ return testing.reset();
});
// test:
- it("errors on an empty search term", () => {
+ it("reports error 400 on an empty search term", () => {
return request(app)
.get('/api/search.php')
// .query({q: "events"})
- .expect(400)
- .expect('Content-Type', /json/)
+ .then(testing.expectError)
.then(res => {
assert.ok(res.body?.error, "expects an error string");
});
@@ -30,8 +28,7 @@ describe("searching for events", () => {
return request(app)
.get('/api/search.php')
.query({q: "go", all: true})
- .expect(200)
- .expect('Content-Type', /json/)
+ .then(testing.expectOkay)
.then(res => {
assert.equal(res.body?.pagination?.fullcount, 14);
assert.equal(res.body?.events?.length, 14);
@@ -45,8 +42,7 @@ describe("searching for events", () => {
l: 1000000,
all: true
})
- .expect(200)
- .expect('Content-Type', /json/)
+ .then(testing.expectOkay)
.then(res => {
// we've been capped to the internal limits
assert.equal(res.body?.pagination?.limit, EventSearch.Limit);
@@ -63,8 +59,7 @@ describe("searching for events", () => {
l: 2,
all: true
})
- .expect(200)
- .expect('Content-Type', /json/)
+ .then(testing.expectOkay)
.then(res => {
assert.equal(res.body?.pagination.fullcount, 14);
assert.equal(res.body?.pagination.offset, 0);
@@ -84,8 +79,7 @@ describe("searching for events", () => {
l: 2,
all: true
})
- .expect(200)
- .expect('Content-Type', /json/)
+ .then(testing.expectOkay)
.then(res => {
assert.equal(res.body?.pagination?.fullcount, 14);
assert.equal(res.body?.pagination?.offset, 2);
diff --git a/app/test/testData.js b/app/test/testData.js
deleted file mode 100644
index 742237dd5..000000000
--- a/app/test/testData.js
+++ /dev/null
@@ -1,43 +0,0 @@
-const dt = require("../util/dateTime");
-const { CalEvent } = require("../models/calEvent");
-const { CalDaily } = require("../models/calDaily");
-//
-const assert = require("node:assert/strict");
-
-const secret = "12e1c433836d6c92431ac71f1ff6dd97";
-const email ="email@example.com";
-
-module.exports = {
- secret,
- email,
- // helper for testing the calendar's custom error message format.
- expectError(res, field, status = 400) {
- assert.equal(res.status, status);
- assert.match(res.header['content-type'], /json/);
- assert.match(res.header['api-version'], /^3\./);
- assert.ok(res.body?.error?.message);
- assert.ok(!field || res.body.error.fields[field]);
- },
- // create a fake database of cal events and dailies:
- stubData(sinon) {
- // fake now to be 5th day of august 2002
- sinon.stub(dt, 'getNow').callsFake(() => {
- return dt.fromYMDString('2002-08-05');
- });
-
- const dailyStore = sinon.spy(CalDaily.methods, '_store')
- const eventStore = sinon.spy(CalEvent.methods, '_store')
- const eventErasures = sinon.spy(CalEvent.methods, 'eraseEvent');
-
- return {
- dailyStore,
- eventStore,
- eventErasures,
- resetHistory() {
- dailyStore.resetHistory();
- eventStore.resetHistory();
- eventErasures.resetHistory();
- }
- }
- },
-};
diff --git a/app/test/testdb.js b/app/test/testdb.js
deleted file mode 100644
index c9b318c24..000000000
--- a/app/test/testdb.js
+++ /dev/null
@@ -1,122 +0,0 @@
-const { Area, Audience, DatesType, EventStatus, Review } = require("../models/calConst");
-const tables = require("../models/tables");
-const dt = require("../util/dateTime");
-const { faker } = require('@faker-js/faker');
-const testData = require("./testData");
-const db = require("../db");
-const { makeFakeData } = require("./fakeData");
-
-module.exports = {
- // generates a hand rolled set of data
- setupTestData: async (name) => {
- await db.initialize();
- await tables.dropTables();
- await tables.createTables();
- faker.seed(23204); // uses lorem generator
- await createTestData();
- },
- // uses faker to generate a good amount of fake data
- setupFakeData: async (name) => {
- await db.initialize();
- await tables.dropTables();
- await tables.createTables();
- const firstDay = dt.fromYMDString("2002-08-01");
- const lastDay = dt.fromYMDString("2002-08-31");
- const numEvents = 46;
- faker.seed(23204); // keeps the generated data stable.
- await makeFakeData(firstDay, lastDay, numEvents);
- },
- destroy() {
- // leaves the tables in place; lets create drop them when needed.
- return db.destroy();
- }
-}
-
-async function createTestData() {
- await db.query('calevent').insert(fakeCalEvent(1));
- await db.query('calevent').insert(fakeCalEvent(2));
- await db.query('calevent').insert(fakeCalEvent(3));
- //
- await db.query('caldaily').insert(fakeCalDaily(1, 2));
- await db.query('caldaily').insert(fakeCalDaily(2, 2));
-};
-
-function fakeCalDaily(order, eventId) {
- if (!eventId) { throw new Error("expects a valid id"); }
- const pkid = order + (eventId * 100);
- const ymd = "2002-08-" + order.toString().padStart(2, "0");
- return {
- id : eventId,
- pkid : pkid,
- eventdate : db.toDate(dt.fromYMDString(ymd)),
- eventstatus : EventStatus.Active,
- newsflash : "news flash",
- };
-}
-
-function fakeCalEvent(eventId) {
- if (!eventId) { throw new Error("expects a valid id"); }
- // an arbitrary created, modified time.
- const created = new Date(1993, 6, 28, 14, 39);
- const modified = new Date(1993, 7, 28, 2, 7);
- const contacturl = "http://example.com";
- const title = `ride ${eventId} title`;
- const organizer = "organizer";
- const descr = faker.lorem.text();
- return {
- created,
- modified,
- changes: 1,
- id: eventId,
- name: organizer,
- email: testData.email,
- hideemail: 1,
- printemail: 0,
- phone: "555-503-5055",
- hidephone: 1,
- printphone: 0,
- weburl: contacturl,
- webname: "example.com",
- printweburl: 1,
- contact: organizer,
- hidecontact : 1,
- printcontact : 1,
- title: title,
- tinytitle: title,
- audience: Audience.General,
- descr,
- printdescr: descr,
- image: `${eventId}.png`,
- datestype: DatesType.OneDay,
- eventtime: "19:00:00",
- eventduration: 60,
- timedetails: "time details",
- // use some ical sensitive chars
- locname: "location, name.",
- // use some html characters
- address: "",
- // use some more html chars
- locdetails: "location && details",
- // use some more ical sensitive chars
- locend: "location; end.",
- loopride : 1,
- area: Area.Portland,
- highlight: 0,
- hidden: hidden(eventId),
- password: testData.secret,
- safetyplan : 1,
- };
-}
-
-function hidden(eventId) {
- switch (eventId) {
- case 3:
- return 1; // #3 is hidden/unpublished.
- case 2:
- return 0;
- case 1:
- return null; // use a legacy hidden code.
- default:
- throw new Error("unexpected event id", eventId);
- }
-}
\ No newline at end of file
diff --git a/app/test/testing.html b/app/test/testing.html
deleted file mode 100644
index 9e7c15ef1..000000000
--- a/app/test/testing.html
+++ /dev/null
@@ -1,1191 +0,0 @@
-testing
\ No newline at end of file
diff --git a/app/test/validator_test.js b/app/test/validator_test.js
index e5684f621..d97ac62e3 100644
--- a/app/test/validator_test.js
+++ b/app/test/validator_test.js
@@ -1,26 +1,33 @@
-const { ErrorCollector, makeValidator } = require("../models/calEventValidator");
-const { RideLength } = require("../models/calConst");
+const { ErrorCollector } = require('shift-docs/util/errors');
+const { EventValidator } = require('shift-docs/support/eventValidator');
+const { Area, Audience, Distance, Distances, LocType, Showable, TagName, WebType } = require('shift-docs/models/newConst');
const { describe, it } = require("node:test");
const assert = require("node:assert/strict");
+function validateRideLength(testValue, errors) {
+ // the validator throws if not a string
+ const input = { ridelength: String(testValue) };
+ const v = new EventValidator(input, errors || new ErrorCollector());
+ const got = v.select('ridelength').shorthand(Distance);
+ return { value: got, errors: v.errors };
+}
+
+function validateInt(testValue, errors) {
+ // the validator throws if not a string
+ const input = { key: String(testValue) };
+ const v = new EventValidator(input, errors || new ErrorCollector());
+ const got = v.select("key").zeroInt();
+ return { value: got, errors: v.errors };
+}
+
describe('event field validation', () => {
it('int validator should succeed', () => {
- const pairs = [
- // value, expectation
- 12, 12,
- "12", 12,
- null, 0,
- 0, 0,
- ];
- for (let i = 0; i < pairs.length; i += 2) {
- const key = "key";
- const value = pairs[i + 0];
- const want = pairs[i + 1];
- const errors = new ErrorCollector();
- const v = makeValidator({ key: value }, errors);
- const got = v.zeroInt(key);
- assert.equal(got, want, `test ${i / 2}`);
- assert.equal(errors.count, 0);
+ const list = [ 12, 0 ];
+ for (let i = 0; i < list.length; i ++) {
+ const value = list[i];
+ const res = validateInt(value);
+ assert.equal(res.value, value, `test ${i / 2}`);
+ assert.equal(res.errors.count, 0);
}
});
it('int validator should fail', () => {
@@ -36,11 +43,9 @@ describe('event field validation', () => {
// collect all of the errors
const errors = new ErrorCollector();
for (let i = 0; i < list.length; i++) {
- const key = "key";
- const v = makeValidator({ key: list[i] }, errors);
- const got = v.zeroInt(key);
- assert.equal(got, undefined, `test ${i / 2}`);
- assert.equal(errors.count, i + 1, `count ${i}`);
+ const res = validateInt(list[i], errors);
+ assert.equal(res.value, undefined, `test ${i / 2}`);
+ assert.equal(res.errors.count, i + 1, `count ${i}`);
}
assert.equal(errors.count, list.length, "final length");
const msg = errors.getErrors();
@@ -50,11 +55,10 @@ describe('event field validation', () => {
assert.equal(msg.key, `Please enter a value for key`);
});
it('ride length validator should accept the known lengths', () => {
- for (const want of Object.keys(RideLength)) {
- const errors = new ErrorCollector();
- const v = makeValidator({ ridelength: want }, errors);
- assert.equal(v.validateRideLength('ridelength'), want);
- assert.equal(errors.count, 0);
+ for (const dist of Distances) {
+ const res = validateRideLength(dist.key);
+ assert.equal(res.value, dist.value);
+ assert.equal(res.errors.count, 0);
}
});
it('ride length validator should reject anything else', () => {
@@ -70,16 +74,17 @@ describe('event field validation', () => {
"__proto__",
];
for (const bad of list) {
- const errors = new ErrorCollector();
- const v = makeValidator({ ridelength: bad }, errors);
- assert.equal(v.validateRideLength('ridelength'), null, `for ${JSON.stringify(bad)}`);
+ const res = validateRideLength(bad);
+ assert.equal(res.value, undefined, `for ${JSON.stringify(bad)}`);
+ assert.notEqual(res.errors.count, 0);
}
});
+ // this is for an issue that has been fixed
+ // since we aren't testing all the code for leaking globals:
+ // we can nix this test now.
it('ride length validator should not leak a global', () => {
delete globalThis.value;
- const errors = new ErrorCollector();
- const v = makeValidator({ ridelength: '0-3' }, errors);
- v.validateRideLength('ridelength');
+ validateRideLength('0-3');
assert.equal(globalThis.value, undefined, "expected no implicit global");
});
});
diff --git a/app/test/db_setup.js b/app/testing/db_setup.js
similarity index 100%
rename from app/test/db_setup.js
rename to app/testing/db_setup.js
diff --git a/app/testing/expect.js b/app/testing/expect.js
new file mode 100644
index 000000000..4c7754a60
--- /dev/null
+++ b/app/testing/expect.js
@@ -0,0 +1,48 @@
+/**
+ * Helpers for common http test results.
+ */
+const assert = require('node:assert/strict');
+
+const expect = {
+ // a little ugly. poked in by testing.configure.
+ expectedFormat: "",
+ // dress for success.
+ okay(res) {
+ const types = {
+ json: /json/,
+ html: /html/,
+ ical: /^text\/calendar/,
+ };
+ const fmt = this.expectedFormat;
+ if (fmt) {
+ const expectedType = types[fmt];
+ if (!expectedType) {
+ throw new Error(`test requested an unknown format '${fmt}'`)
+ }
+ assert.match(res.header['content-type'], expectedType);
+ }
+ // note: crawl sends html and doesn't set the api version.
+ if (fmt !== 'html') {
+ assert.match(res.header['api-version'] || "missing api version", /^3\./);
+ }
+ const body = JSON.stringify(res.body);
+ assert.equal(res.status, 200, `got status ${res.status} with body ${body}`);
+ return res;
+ },
+ // checks for shift's custom error message format.
+ // field can check for a particular message field
+ error(res, field = null, status = 400) {
+ if (field) {
+ const errMsg = res.body?.error?.fields[field];
+ assert.ok(errMsg, `expected an error message for field "${field}"`);
+ }
+ assert.equal(res.status, status, `unexpected status`);
+ assert.match(res.header['content-type'], /json/);
+ assert.match(res.header['api-version'] || "missing api version", /^3\./);
+ assert.ok(res.body?.error?.message, "a general error message must exist");
+ return res;
+ }
+};
+
+module.exports = expect;
+
diff --git a/app/testing/expectedResults.js b/app/testing/expectedResults.js
new file mode 100644
index 000000000..f0ac9bd76
--- /dev/null
+++ b/app/testing/expectedResults.js
@@ -0,0 +1,179 @@
+const assert = require('node:assert/strict');
+
+/**
+ * The expected output of various tests
+ */
+const shiftHeader = [
+String.raw`VERSION:2.0`,
+String.raw`PRODID:-//shift2bikes.org//NONSGML shiftcal v2.1//EN`,
+String.raw`METHOD:PUBLISH`,
+String.raw`X-WR-CALNAME:Shift Community Calendar`,
+String.raw`X-WR-CALDESC:Find fun bike events all year round.`,
+String.raw`X-WR-RELCALID:community@shift2bikes.org`,
+String.raw`X-WR-TIMEZONE:America/Los_Angeles`
+];
+
+const pedalpHeader = [
+String.raw`VERSION:2.0`,
+String.raw`PRODID:-//shift2bikes.org//NONSGML shiftcal v2.1//EN`,
+String.raw`METHOD:PUBLISH`,
+String.raw`X-WR-CALNAME:Pedalpalooza Bike Calendar`,
+String.raw`X-WR-CALDESC:Find fun Pedalpalooza bike events!`,
+String.raw`X-WR-RELCALID:shift@shift2bikes.org`,
+String.raw`X-WR-TIMEZONE:America/Los_Angeles`
+];
+
+const event1 = [
+String.raw`BEGIN:VEVENT`,
+String.raw`UID:event-201@shift2bikes.org`,
+String.raw`SUMMARY:ride 2 title`,
+String.raw`CONTACT:organizer`,
+String.raw`DESCRIPTION:news flash\nPeior amicitia texo. Delectus sequi qui temporibus`,
+String.raw` clibanus. Creber creo adamo aedificium creta bardus aegre.\ntime `,
+String.raw` details\nEnds at location\; end.\nhttps://shift2bikes.org/calendar/event-`,
+String.raw` 201`,
+String.raw`LOCATION:location\, name.\n\nlocation && details`,
+String.raw`STATUS:CONFIRMED`,
+String.raw`DTSTART:20020802T020000Z`,
+String.raw`DTEND:20020802T030000Z`,
+String.raw`CREATED:19930728T213900Z`,
+String.raw`DTSTAMP:19930828T090700Z`,
+String.raw`SEQUENCE:2`,
+String.raw`URL:https://shift2bikes.org/calendar/event-201`,
+String.raw`END:VEVENT`,
+];
+
+const event2 = [
+String.raw`BEGIN:VEVENT`,
+String.raw`UID:event-202@shift2bikes.org`,
+String.raw`SUMMARY:ride 2 title`,
+String.raw`CONTACT:organizer`,
+String.raw`DESCRIPTION:news flash\nPeior amicitia texo. Delectus sequi qui temporibus`,
+String.raw` clibanus. Creber creo adamo aedificium creta bardus aegre.\ntime `,
+String.raw` details\nEnds at location\; end.\nhttps://shift2bikes.org/calendar/event-`,
+String.raw` 202`,
+String.raw`LOCATION:location\, name.\n\nlocation && details`,
+String.raw`STATUS:CONFIRMED`,
+String.raw`DTSTART:20020803T020000Z`,
+String.raw`DTEND:20020803T030000Z`,
+String.raw`CREATED:19930728T213900Z`,
+String.raw`DTSTAMP:19930828T090700Z`,
+String.raw`SEQUENCE:2`,
+String.raw`URL:https://shift2bikes.org/calendar/event-202`,
+String.raw`END:VEVENT`,
+];
+
+const allEvents = [
+String.raw`BEGIN:VCALENDAR`,
+...shiftHeader,
+...event1,
+...event2,
+String.raw`END:VCALENDAR`,
+"" // trailing new line. i think.
+].join("\r\n");
+
+const emptyRange = [
+String.raw`BEGIN:VCALENDAR`,
+...shiftHeader,
+String.raw`END:VCALENDAR`,
+"" // trailing new line. i think.
+].join("\r\n");
+
+const cancelled1 = [
+String.raw`BEGIN:VEVENT`,
+String.raw`UID:event-201@shift2bikes.org`,
+String.raw`SUMMARY:CANCELLED: ride 2 title`,
+String.raw`CONTACT:organizer`,
+String.raw`DESCRIPTION:news flash\nPeior amicitia texo. Delectus sequi qui temporibus`,
+String.raw` clibanus. Creber creo adamo aedificium creta bardus aegre.\ntime `,
+String.raw` details\nEnds at location\; end.\nhttps://shift2bikes.org/calendar/event-`,
+String.raw` 201`,
+String.raw`LOCATION:location\, name.\n\nlocation && details`,
+String.raw`STATUS:CANCELLED`,
+String.raw`DTSTART:20020802T020000Z`,
+String.raw`DTEND:20020802T030000Z`,
+String.raw`CREATED:19930728T213900Z`,
+String.raw`DTSTAMP:19930828T090700Z`,
+String.raw`SEQUENCE:2`,
+String.raw`URL:https://shift2bikes.org/calendar/event-201`,
+String.raw`END:VEVENT`,
+];
+
+const cancelledDay = [
+String.raw`BEGIN:VCALENDAR`,
+...shiftHeader,
+...cancelled1,
+...event2,
+String.raw`END:VCALENDAR`,
+"" // trailing new line. i think.
+].join("\r\n");
+
+const pedalEnd = [
+String.raw`BEGIN:VEVENT`,
+String.raw`UID:pedalpalooza-2002-end@shift2bikes.org`,
+String.raw`SUMMARY:Pedalpalooza 2002 is over!`,
+String.raw`CONTACT:bikecal@shift2bikes.org`,
+String.raw`DESCRIPTION:We hope you've had a great bike summer and a great `,
+String.raw` Pedalpalooza!!!\nWhile Pedalpalooza is done\, there is still plenty of `,
+String.raw` bike fun to be found on the Shift2bikes website. And you can also `,
+String.raw` subscribe to the Shift community calendar to see those rides.\nVisit `,
+String.raw` https://shift2bikes.org/calendar/ for more details.`,
+String.raw`LOCATION:Portland\, and beyond!`,
+String.raw`STATUS:CONFIRMED`,
+// midnight on the last day
+String.raw`DTSTART:20020803T070000Z`,
+// the end of the next day
+String.raw`DTEND:20020804T065959Z`,
+// fake created date ( in the same year )
+String.raw`CREATED:20020601T070000Z`,
+String.raw`DTSTAMP:20020601T070000Z`,
+String.raw`SEQUENCE:1`,
+String.raw`URL:https://shift2bikes.org/calendar/`,
+String.raw`END:VEVENT`,
+];
+
+const pedalpaloozaFeed = [
+String.raw`BEGIN:VCALENDAR`,
+...pedalpHeader,
+...event1,
+...event2,
+...pedalEnd,
+String.raw`END:VCALENDAR`,
+"" // trailing new line. i think.
+].join("\r\n");
+
+const createdEmail = {
+ subject: 'Shift2Bikes Secret URL for new event',
+ text:
+ `Dear js test,
+
+Thank you for adding your event, new event, to the Shift Calendar. To activate the event listing, you must visit the confirmation link below and publish it:
+
+https://shift2bikes.org/addevent/edit-4-ef23a887cdbd44f381275fa771a2d133
+
+Your scheduled event details are:
+Date(s): Wed, May 24th
+Time: 3:15 PM
+Location: the secret hideout, the location of the secret hideout
+
+This link is like a password. Anyone who has it can delete and change your event. Please keep this email so you can manage your event in the future.
+
+If you need help with your listing, please refer to https://shift2bikes.org/pages/calendar-faq/ or email bikecal@shift2bikes.org.
+
+Bike on!
+
+-Shift
+`,
+ to: { name: 'js test', address: 'test@example.com' },
+ from: { name: 'SHIFT to Bikes', address: 'bikefun@shift2bikes.org' },
+ replyTo: 'bikecal@shift2bikes.org',
+ bcc: 'shift-event-email-archives@googlegroups.com'
+}
+
+module.exports = {
+ pedalpaloozaFeed,
+ cancelledDay,
+ emptyRange,
+ allEvents,
+ createdEmail,
+};
\ No newline at end of file
diff --git a/app/testing/fakeData.js b/app/testing/fakeData.js
new file mode 100644
index 000000000..b0add8b60
--- /dev/null
+++ b/app/testing/fakeData.js
@@ -0,0 +1,237 @@
+const { faker } = require('@faker-js/faker');
+const config = require('shift-docs/config');
+const dt = require('shift-docs/util/dateTime');
+const { Area, Audience, Distance, Distances, LocType, Showable, TagName, WebType } = require('shift-docs/models/newConst');
+const { insertNewData } = require('shift-docs/support/reconcile');
+
+// password shared for all fake events
+const password = "supersecret";
+// number of days within which to create fake events
+const fakeRange = 7;
+
+// writes an array of the fake data format into the db.
+// separating generation from insertion prevents race conditions from influencing the generated data.
+function insertFakeData(db, fakeData) {
+ return db.query.transaction(tx => _insertData(tx, fakeData));
+}
+
+// where fakeData contains [{event, days}]
+function _insertData(tx, fakeData) {
+ const promisedEvents = fakeData.map(data => {
+ return insertNewData(tx, data.event).then(id => {
+ data.event.series.id = id;
+ const promisedDays = data.days.map(at => {
+ at.id = id; // assign the id before adding to the db
+ return tx('cal_schedule').insert(at).then(row => {
+ at.pkid = row[0]; // the value returend
+ return at;
+ });
+ });
+ return Promise.all(promisedDays);
+ });
+ });
+ // wait on all the events to finish.
+ return Promise.all(promisedEvents);
+}
+
+// prints an array of fake data format to console.
+function logFakeData(data) {
+ data.forEach(({event, days}) => {
+ const url = config.site.url("addevent", `edit-${event.series.id}-${event.private.secret}`);
+ const start = dt.friendlyDate(days[0].ymd);
+ console.log(`created ${url}\n "${event.series.title}" with ${days.length} days starting on ${start}`);
+ });
+}
+
+// promise an array of {event: {}, days: [{}]}
+// - firstDay is a dt day
+// - numEvents: a number of events to create
+// nextEventId can be a number, in which case it creates events starting with that id.
+function generateFakeData(firstDay, lastDay, numEvents, nextEventId = undefined) {
+ const out = [];
+ let nextDayId = nextEventId ? 1 : undefined;
+ for (let i = 0; i< numEvents; i++) {
+ // always have one event on the day specified;
+ // on subsequent events, pick from a range of days.
+ const start = !i ? firstDay :
+ dt.convert(faker.date.between({
+ from: firstDay.toDate(),
+ to: lastDay.toDate(),
+ }));
+ const title = faker.music.songName();
+ const event = makeCalEvent(title, nextEventId);
+ const numDays = randomDayCount();
+ const days = makeCalDailies(start, numDays, nextDayId);
+ if (nextEventId !== undefined) {
+ nextEventId += 1;
+ nextDayId += numDays;
+ }
+ out.push({
+ event,
+ days,
+ });
+ }
+ return out;
+}
+
+// export!
+module.exports = { insertFakeData, generateFakeData, logFakeData };
+
+function randomDayCount() {
+ // some dumb weighted random
+ const pick = [1,1,1,1,1,2,2,2,3,3,4,5];
+ return faker.helpers.arrayElement(pick);
+}
+
+function randomRideLength() {
+ return faker.helpers.arrayElement(Distances);
+}
+
+// days is the range of possible days in the future
+// returns javascript date
+function nextDay(days, refDate) {
+ return !refDate? faker.date.soon(days) : faker.date.soon({
+ days,
+ refDate,
+ });
+}
+
+function makeCalDailies(start, numDays, nextPkid = undefined) {
+ const out = [];
+ const active = faker.datatype.boolean(0.8);
+ const flash = faker.datatype.boolean(!active? 0.8: 0.3);
+ const msg = flash ? (active? faker.vehicle.bicycle() :
+ faker.word.adverb() + " cancelled"): null;
+ for (let i=0; i0) {
+ // 1 to 3 days from the first day
+ start = start.add(1 + faker.number.int(3), 'days');
+ }
+ out.push({
+ ymd : dt.toYMDString(start),
+ news : msg,
+ is_scheduled : active ? 1 : 0, // boolean as a number
+ pkid : (nextPkid !== undefined) ? (nextPkid++) : undefined,
+ });
+ }
+ return out;
+}
+
+function capitalize(str, yes= true) {
+ const first = str.charAt(0);
+ return (yes? first.toUpperCase() : first.toLowerCase() ) + str.slice(1);
+}
+
+function makeCalEvent(title) {
+ const firstName = faker.person.firstName();
+ const lastName = faker.person.lastName();
+ const organizer = faker.person.fullName({firstName, lastName});
+ const email = faker.internet.exampleEmail({firstName, lastName});
+ const visible = faker.datatype.boolean(0.75) ? 0: 1;
+ const printable = faker.datatype.boolean(0.5) ? 1: 0;
+ const phone = faker.phone.number();
+ const weburl = faker.datatype.boolean(0.5) ? null:
+ faker.internet.url();
+ const webname = faker.datatype.boolean(0.5) ? null:
+ faker.word.verb() + " " +
+ faker.word.adjective() + " " +
+ faker.word.noun();
+ const audience = faker.helpers.objectValue( Audience );
+ const descr = faker.hacker.phrase();
+ // this would normally be the event id; remove if this is a problem.
+ const image = faker.datatype.boolean(0.75) ? null: "bike.jpg";
+ // "19:00:00"
+ const eventtime = String(6+ faker.number.int(12)).padStart(2, '0') + ":" +
+ String(15* faker.number.int(3)).padStart(2, '0') + ":00";
+ // in minutes, but sometimes not specified at all
+ const eventduration = faker.datatype.boolean(0.25) ? null :
+ (6* faker.number.int(3) * 15);
+ const timedetails = faker.datatype.boolean(0.75) ? null :
+ faker.lorem.sentences({ min: 1, max: 3 });
+ const area = faker.datatype.boolean(0.25) ?
+ faker.helpers.objectValue( Area ) :
+ Area.Portland;
+ const highlight = faker.datatype.boolean(0.1);
+ const safetyplan = faker.datatype.boolean(0.75);
+ const loopride = faker.datatype.boolean(0.5);
+ const locname = capitalize(faker.word.adverb()) + " " +
+ capitalize(faker.animal.fish(), false);
+
+ const address = faker.location.streetAddress();
+ const locdetails = faker.datatype.boolean(0.25) ? null :
+ faker.company.catchPhrase();
+ const locend = faker.datatype.boolean(0.50) ? null :
+ faker.location.streetAddress();
+ const ridelength = randomRideLength();
+
+ const show = Showable.combine(visible, printable);
+
+ // constants:
+ return {
+ image: {
+ img_override: image,
+ },
+ series: {
+ // not directly set: id, created, modified
+ published: 1,
+ title,
+ organizer,
+ start_time: eventtime,
+ scheduled_duration: eventduration,
+ summary: descr,
+ details: descr,
+ },
+ location: [{
+ loc_type: LocType.Start,
+ place_name: locname,
+ address: address,
+ place_info: locdetails,
+ time_info: timedetails,
+ }, (locend || eventduration) ? filterObject({
+ loc_type: LocType.Finish,
+ place_name: locend,
+ }) : null].filter(el => el), // removes the finish if it was empty
+ private: {
+ secret: password,
+ private_email: email,
+ private_phone: phone,
+ private_contact: organizer,
+ show_email: show.value,
+ show_phone: show.value,
+ show_contact: Showable.Private.value, // test always uses private.
+ },
+ tag: [
+ tag(TagName.Area, area.value),
+ tag(TagName.Audience, audience.value),
+ tag(TagName.Distance, ridelength.value),
+ tag(TagName.LoopRide, loopride),
+ tag(TagName.SafetyPlan, safetyplan),
+ tag(TagName.Featured, highlight),
+ ].filter(t => !!t.tag_value),
+ web: {
+ web_type: WebType.Url,
+ web_link: weburl,
+ web_text: webname,
+ show_weburl: Showable.Public.value, // test always uses printable, visible.
+ }
+ };
+}
+
+// return a copy of a, omitting keys with empty values.
+function filterObject(a) {
+ const out = {};
+ Object.keys(a).forEach(k => {
+ const v = a[k];
+ if (v) {
+ out[k] = v;
+ }
+ });
+ return out;
+}
+
+function tag(a, b) {
+ return {
+ tag_type: a,
+ tag_value: b,
+ }
+}
\ No newline at end of file
diff --git a/app/testing/index.js b/app/testing/index.js
new file mode 100644
index 000000000..5ab1fcfe3
--- /dev/null
+++ b/app/testing/index.js
@@ -0,0 +1,90 @@
+const { faker } = require("@faker-js/faker");
+const db = require('shift-docs/db');
+const dt = require('shift-docs/util/dateTime');
+const config = require('shift-docs/config');
+const { setupTables } = require('shift-docs/schema');
+// internal testing files:
+const expect = require('./expect.js');
+const expectedResults = require('./expectedResults.js');
+const user = require('./user.js');
+const { createTestData } = require('./testdb.js');
+const { generateFakeData, insertFakeData } = require("shift-docs/testing/fakeData");
+
+const localExports = {
+
+ // dictionary of source data
+ user,
+ // dictionary of expected data
+ expectedResults,
+
+ // function to check for html errors
+ expectError: expect.error,
+
+ // function to check for html success
+ expectOkay: expect.okay,
+
+ // where format is the desired output, ex. "json"
+ configure(format) {
+ expect.expectedFormat = format;
+ },
+ reset() {
+ // todo: figure out how to link this into the sandbox/sandbox.reset
+ this.configure();
+ return db.destroy();
+ },
+
+ reset() {
+ // todo: figure out how to link this into the sandbox/sandbox.reset
+ this.configure();
+ return db.destroy();
+ },
+ // setup the test db without adding any data
+ async initTestDB(name) {
+ await db.initialize(name);
+ await setupTables(db, {drop: true});
+ },
+ // add small set of manually defined test data
+ async createTestData(scheduledEvents = false) {
+ faker.seed(23204); // uses lorem generator
+ await createTestData(db, scheduledEvents);
+ },
+ // initialize the test db and add a small set of manually defined test data
+ async setupTestData(name, scheduledEvents = false) {
+ await this.initTestDB(name);
+ await this.createTestData(scheduledEvents);
+ },
+ // uses faker to setup a large amount of test data
+ async setupFakeData(name) {
+ await this.initTestDB(name);
+ const firstDay = dt.fromYMDString("2002-08-01");
+ const lastDay = dt.fromYMDString("2002-08-31");
+ const numEvents = 46;
+ faker.seed(23204); // keeps the generated data stable.
+ const fakeData = generateFakeData(firstDay, lastDay, numEvents);
+ return insertFakeData(db, fakeData);
+ },
+
+ // return all data including private data
+ findSeries(seriesId) {
+ return db
+ .query('view_private_events')
+ .where('id', seriesId);
+ },
+
+ // create a fake database of cal events and dailies:
+ fakeNow(sinon) {
+ // fake now to be 5th day of august 2002
+ sinon.stub(dt, 'getNow').callsFake(() => {
+ return dt.fromYMDString('2002-08-05');
+ });
+ },
+ // changes how absolute urls are generated
+ // to prefix them with "shift.com" even though generated from localhost
+ fakeSiteUrl(sinon, path = "https://shift2bikes.org") {
+ sinon.stub(config.site, 'url').callsFake((...parts) => {
+ return [path, ...parts].join("/");
+ });
+ },
+};
+
+module.exports = localExports;
diff --git a/app/testing/testRunner.js b/app/testing/testRunner.js
new file mode 100644
index 000000000..d0f0faa88
--- /dev/null
+++ b/app/testing/testRunner.js
@@ -0,0 +1,88 @@
+//
+// A custom wrapper for running multiple tests.
+//
+// Rationale: node's test runner ignores multiple files when using its only and pattern filters.
+// https://github.com/nodejs/node/issues/51384
+//
+const fs = require('node:fs'); // search for test files
+const path = require('node:path'); // building paths to run those files
+const shell = require('shelljs'); // runs node from node; what could be simpler :sob:.
+const CommandLine = require('shift-docs/util/cmdLine');
+const { globalSetup, globalTeardown } = require('shift-docs/testing/db_setup');
+
+const cmdLine = new CommandLine({
+ f: `limit to a specific filename`,
+ p: `limit to a specific sub-directory`,
+ only: `flag to invoke --test-only`,
+ pattern: `regex for --test-name-pattern`,
+});
+
+// arguments to pass to node for each test
+const testArgs = [
+ `--trace-warnings`,
+ `--test-concurrency=1`,
+ // normally we'd let node handle the global setup ( ex. starting docker )
+ // however, when running multiple tests, the setup should only happen once.
+ //`--test-global-setup=test/db_setup.js`
+];
+
+// the custom test runner
+async function runTests() {
+ const startingDir = process.cwd(); // ex. /Users/ionous/dev/shift/real-shift/app
+ const originalCmdLine = process.argv.slice(2); // skip 0 (bin/node) and 1 (testRunner.js)
+ await globalSetup();
+ _runTests(startingDir, testArgs, originalCmdLine, cmdLine.options);
+ await globalTeardown();
+}
+runTests();
+
+// helpers:
+function _runTests(dir, args, orig, options) {
+ const { only, pattern, p: subdir, f: filename, v: version } = options;
+ if (only && pattern) {
+ throw new Error(`The test runner expects at most one option. Both only and pattern were specified.`);
+ }
+ // fix: only have to look in the test directory. probably.
+ const files = findTestFiles(dir, subdir, filename);
+ const testOnly = only === 'true';
+ if (!testOnly && !pattern) {
+ // the regular test command can handle processing multiple files
+ test(version, [`--test`].concat(args, files, '--', orig));
+
+ } else if (testOnly) {
+ // find will stop running tests when something returns a non-zero error code.
+ files.find(f => test(version, [`--test-only`].concat(args, f,'--', orig)) !== 0 );
+
+ } else if (pattern) {
+ // find will stop running tests when something returns a non-zero error code.
+ files.find(f => test(version, args.concat(`--test-name-pattern="${pattern}"`, f)) !== 0 );
+ }
+}
+
+// return an array of filenames to test
+// (returned paths are relative to dir)
+function findTestFiles(dir, subdir, filename) {
+ const entries = fs.readdirSync(dir, {
+ withFileTypes: true,
+ recursive: true,
+ });
+ return entries.filter(f => {
+ const rel = path.relative(dir, f.parentPath);
+ return f.name.endsWith("_test.js") &&
+ (!rel.includes("node_modules")) &&
+ (!subdir || rel.includes(subdir)) &&
+ (!filename || f.name.includes(filename));
+ }).map(f => path.relative(dir, path.resolve(f.parentPath, f.name)));
+}
+
+function test(version, parts, dbtype) {
+ const cmdLine = `node ${parts.join(' ')}`;
+ shell.echo(cmdLine);
+ if (version) {
+ // this sets a string: so don't set it to the string "undefined" or "false"
+ shell.env["SHIFT_VERSION"] = version;
+ }
+ // note: shell.cmd is safer, but i can't get it to work with quoted text options. :shrug:
+ // ex. --test-name-pattern="date time" looks correct when echo'd but doesn't pass the pattern to node.
+ return shell.exec(cmdLine).code;
+}
\ No newline at end of file
diff --git a/app/testing/testdb.js b/app/testing/testdb.js
new file mode 100644
index 000000000..f8332982d
--- /dev/null
+++ b/app/testing/testdb.js
@@ -0,0 +1,109 @@
+const { faker } = require('@faker-js/faker');
+const db = require('shift-docs/db');
+const dt = require('shift-docs/util/dateTime');
+const { setupTables } = require('shift-docs/schema');
+const { insertNewData } = require('shift-docs/support/reconcile');
+const { TagName, LocType, Showable, WebType } = require('shift-docs/models/newConst');
+const user = require('shift-docs/testing/user');
+
+module.exports = {
+ createTestData,
+}
+
+// a small set of manually defined test data
+async function createTestData(db, scheduledEvents) {
+ // create 3 separate series with ids 1, 2, 3
+ for (const id of [1, 2, 3]) {
+ const tableValues = fakeCalEvent(id);
+ await insertNewData(db.query, tableValues);
+ }
+ // schedules a few days to some of the events
+ const whichEvents = scheduledEvents || [2];
+ for (const id of whichEvents) {
+ for (const order of [1, 2]) {
+ const item = fakeCalDaily(order, id);
+ await db.query('cal_schedule').insert(item);
+ }
+ }
+}
+
+function fakeCalDaily(order, eventId) {
+ if (!eventId) { throw new Error("expects a valid id"); }
+ const pkid = order + (eventId * 100);
+ const ymd = "2002-08-" + order.toString().padStart(2, "0");
+ return {
+ id : eventId,
+ pkid : pkid,
+ ymd : ymd,
+ news : "news flash",
+ is_scheduled : 1 // boolean as a number
+ };
+}
+
+// returns data which gets written directly to the db
+function fakeCalEvent(eventId) {
+ if (!eventId) { throw new Error("expects a valid id"); }
+ // an arbitrary created, modified time.
+ const created = new Date(1993, 6, 28, 14, 39);
+ const modified = new Date(1993, 7, 28, 2, 7);
+ const contacturl = "http://example.com";
+ const title = `ride ${eventId} title`;
+ const organizer = "organizer";
+ // generate some consistent, arbitrary text:
+ const descr = faker.lorem.text();
+ return {
+ // image: {
+ // img_version: 1,
+ // img_ext: 'jpg',
+ // },
+ series: {
+ id: eventId,
+ created,
+ modified,
+ published: eventId === 3 ? 0 : 1,
+ organizer,
+ start_time: "19:00:00",
+ scheduled_duration: 60,
+ title,
+ tiny_title: title,
+ summary: descr,
+ details: descr,
+ },
+ location: [{
+ loc_type: LocType.Start,
+ place_name: "location, name.", // test ical handling: comma and full-stop are ical sensitive chars
+ address: "", // test html renedering: gt, lt are html sensitive chars
+ place_info: "location && details",
+ time_info: "time details",
+ },{
+ loc_type: LocType.Finish,
+ place_name: "location; end.", // semi-colon is another ical sensitive char
+ }],
+ private: {
+ secret: user.secret,
+ private_email: user.email,
+ private_phone: "555-503-5055",
+ private_contact: organizer,
+ show_contact: Showable.Printable.value,
+ show_email: Showable.Private.value,
+ show_phone: Showable.Private.value,
+ },
+ tag: [
+ tag(TagName.LoopRide),
+ tag(TagName.SafetyPlan),
+ ],
+ web: [{
+ web_type: WebType.Url,
+ web_link: contacturl,
+ web_text: "example.com",
+ show_weburl: Showable.Public.value, // test always uses printable, visible.
+ }],
+ }
+}
+
+function tag(a, b = 'true') {
+ return {
+ tag_type: a,
+ tag_value: b,
+ }
+}
diff --git a/app/testing/user.js b/app/testing/user.js
new file mode 100644
index 000000000..27ed87019
--- /dev/null
+++ b/app/testing/user.js
@@ -0,0 +1,10 @@
+/**
+ * end user constants for testing.
+ */
+const secret = "12e1c433836d6c92431ac71f1ff6dd97";
+const email = "email@example.com";
+
+module.exports = {
+ secret,
+ email,
+}
diff --git a/app/util/cmdLine.js b/app/util/cmdLine.js
new file mode 100644
index 000000000..a021a571f
--- /dev/null
+++ b/app/util/cmdLine.js
@@ -0,0 +1,65 @@
+
+// Parses key=value parameters passed via the command line.
+// Adds the results to its '.options' member.
+// ex. `-hello=world` becomes `this.options.hello === "world"`
+class CommandLine {
+ // pass valid command line options as a map of name to documentation.
+ // will throw if the user has specified an option that isn't part of the known set
+ constructor(known) {
+ const pairs = process.argv
+ .filter(arg => arg.startsWith('-') && !arg.startsWith('--'))
+ .map(arg => {
+ const match = arg.slice(1).match(/^(\w+)=(.+)|(\w+)$/);
+ // an argument '-a=b' gets split into key, value
+ // an argument '-c' gets assigned to flag
+ const [str, key, value, flag] = (match || [arg]);
+ // return them both as pairs to generate the options
+ return (key !== undefined) ? [key, value] :
+ (flag !== undefined) ? [flag, 'true'] :
+ [str];
+ });
+ // note: allows unknown options
+ // ( ex. so test runner and tests can have overlapping command lines )
+ this.known = known;
+ this.options = Object.fromEntries(pairs);
+ }
+ // read a true/false style option.
+ // or undefined if no such option was specified.
+ bool(key) {
+ if (!Object.hasOwn(this.known, key)) {
+ console.error(`unknown key ${key}.`);
+ this.printKnown(console.error);
+ throw new Error("unexpected boolean key");
+ }
+ const value = this.options[key];
+ if (value) {
+ const map = {
+ '0': false, 'false': false,
+ '1': true, 'true': true,
+ };
+ const res = map[value];
+ if (res === undefined) {
+ console.error(`CommandLine had '${value}' when a boolean value for '${key}' was expected`);
+ throw new Error("unexpected boolean value");
+ }
+ return res;
+ }
+ }
+ printKnown(out = console.info) {
+ const known = this.known;
+ out(`Command line options:`);
+ Object.keys(known).forEach(key => {
+ let value = known[key];
+ if (Array.isArray(value)) {
+ value = value.join("\n ");
+ }
+ out(`* ${key} = ${value}`);
+ });
+ }
+}
+
+function quote(list) {
+ return list.map(quote => `'${quote}'`).join(", ")
+}
+
+module.exports = CommandLine;
diff --git a/app/util/dateTime.js b/app/util/dateTime.js
index 5625b2777..e73a11af5 100644
--- a/app/util/dateTime.js
+++ b/app/util/dateTime.js
@@ -24,8 +24,9 @@ module.exports = {
toTimestamp, // out: mysql format
fromYMDString, // in : "YYYY-MM-DD"
- from24HourString, // in : "9:10 PM"
- from12HourString, // in : "21:10:00"
+ from12HourString, // in : "9:10 PM"
+ from24HourString, // in : "21:10:00"
+ fromTimeString, // in : prefers 12hr time; fallsback to 24hr time when needed.
combineDateAndTime,
getNow,
@@ -54,7 +55,7 @@ function friendlyDate(d) {
// note: dayjs theoretically takes into account daylight savings based on the day and month.
function icalFormat(d) {
const t = dayjs(d); // convert or clone as dayjs
- if (!t.isValid()) {
+ if (!t.isValid()) {
return null;
} else {
// tell dayjs that the time was specified using local time.
@@ -67,6 +68,7 @@ function icalFormat(d) {
// format a Date or dayjs in YYYY-MM-DD format ( ex. 2006-01-02 )
// returns "null" if the passed date is invalid.
+// ( passing undefined -- no parameter -- returns the current date )
function toYMDString(d) {
const out = dayjs(d);
return out.isValid() ? out.format('YYYY-MM-DD') : null;
@@ -115,6 +117,12 @@ function from24HourString(str) {
return dayjs(str || null, 'HH:mm:ss', false); // not strict parsing, in case seconds are missing.
}
+// browser time pickers don't match the original client format
+// ... attempt to handle them all?
+function fromTimeString(str) {
+ return dayjs(str || null, ['h:mm A', 'HH:mm:ss', 'HH:mm']);
+}
+
// expects two dayjs objects
// the second one is inspected for its time only and added to the first.
function combineDateAndTime(d, t) {
@@ -122,7 +130,6 @@ function combineDateAndTime(d, t) {
return out.add(t.hour(), 'h').add(t.minute(), 'm');
}
-
// https://stackoverflow.com/questions/13627308/add-st-nd-rd-and-th-ordinal-suffix-to-a-number
function daySuffix(i) {
let j = i % 10;
diff --git a/app/util/errors.js b/app/util/errors.js
index 597c2eb6b..1f73bf239 100644
--- a/app/util/errors.js
+++ b/app/util/errors.js
@@ -1,10 +1,124 @@
-const config = require("../config");
+const config = require('shift-docs/config');
-module.exports = {
- textError,
- fieldError
+// common error strings for parsing input
+const ErrorStrings = {
+ Missing: "Missing value",
+ Invalid: "Invalid value", // the wrong type of value
+ Unknown: "Unknown value", // the right type but not the right value.
+ Overflow: "Field is too long",
};
+// base class for errors intended to be shown to the end user.
+// for internal errors, prefer a regular Error.
+class StatusError extends Error {
+ // defaults to status code 400 Bad Request
+ // "The server cannot or will not process the request due to an apparent client error (e.g., malformed request syntax, size too large, invalid request message framing, or deceptive request routing)."
+ constructor(status, message) {
+ // becomes this.message
+ super(message ?? `status error ${status}`);
+ // console.log("StatusError:", message);
+ this.status = status;
+ }
+ // res is an express response
+ // used to report the error back to the client
+ // the client expects a json object containing status and message
+ sendError(res) {
+ const { status, message } = this;
+ res.status(status).json({
+ error: {
+ status,
+ message
+ }
+ });
+ }
+}
+
+// not really an error, forces a generic redirect.
+class ErrRedirect extends StatusError {
+ constructor(dest, message = undefined) {
+ super(302, message || `redirect to ${dest}`);
+ this.dest = dest;
+ }
+ sendError(res) {
+ res.redirect(this.status, this.dest);
+ }
+}
+
+// for response of post into get
+class ErrCommited extends StatusError {
+ constructor(dest, message = undefined) {
+ super(303, message || `commited to ${dest}`);
+ this.dest = dest;
+ }
+ sendError(res) {
+ res.redirect(this.status, this.dest);
+ }
+}
+
+class ErrBadRequest extends StatusError {
+ constructor(message = "bad request") {
+ super(400, message);
+ }
+}
+
+class ErrForbidden extends StatusError {
+ constructor(message = "not permitted") {
+ super(403, message);
+ }
+}
+
+class ErrNotFound extends StatusError {
+ constructor(message = "not found") {
+ super(404, message);
+ }
+}
+
+// a special version of bad request:
+// reports back to the user with one or more error messages
+// relating to specific fields of a form.
+class FormError extends ErrBadRequest {
+ // where errors is a map of field name to error message
+ // (ie. the result of ErrorCollector.getErrors())
+ constructor(errors) {
+ super("There were some errors in your fields.");
+ this.fields = errors;
+ }
+ // override to provide error fields
+ sendError(res) {
+ // fields can be undefined and that's okay.
+ const { status, message, fields } = this;
+ res.status(status).json({
+ error: {
+ status,
+ message,
+ fields,
+ }
+ });
+ }
+}
+
+// helper for generating field errors.
+class ErrorCollector {
+ constructor() {
+ this.errors = {};
+ this.count = 0;
+ }
+ hasErrors() {
+ return this.count > 0;
+ }
+ // a map of field name to message
+ getErrors() {
+ return this.errors;
+ }
+ // if message is missing, adds a generic error
+ // fix? messages can contain html.
+ addError(field, msg = undefined) {
+ this.errors[field] = msg ?? `Please enter a value for ${field}`;
+ this.count++;
+ return this;
+ }
+}
+
/**
* Triggers a 400 response, and formats the passed string into a json error message for the client.
* {
@@ -23,7 +137,6 @@ function textError(res, message="unknown error", status_code=400) {
}});
}
-
/**
* Triggers a 400 response, and formats the passed key,value array into json messages for the client.
* {
@@ -41,5 +154,26 @@ function fieldError(res, fields, message = "There were errors in your fields", s
error: {
message,
fields,
- }});
+ }
+ });
}
+
+// ------------------------------------------
+// exports
+// ------------------------------------------
+
+module.exports = {
+ textError,
+ fieldError,
+
+ FormError,
+ ErrRedirect,
+ ErrCommited,
+ ErrBadRequest,
+ ErrNotFound,
+ ErrForbidden,
+ StatusError,
+
+ ErrorCollector,
+ ErrorStrings,
+};
diff --git a/app/util/handleCall.js b/app/util/handleCall.js
new file mode 100644
index 000000000..bfc74900c
--- /dev/null
+++ b/app/util/handleCall.js
@@ -0,0 +1,39 @@
+const config = require('shift-docs/config');
+
+module.exports = handleCall;
+
+// wrapper for shift endpoints.
+// assumes 'cb' either generates a promise or throws an error,
+// and that 'send' can handle the result of that promise.
+function handleCall(cb, queryFilter) {
+ // express request, response, and chain handler
+ return function(req, res, next) {
+ // only handle this endpoint if the query expectations matched
+ if (queryFilter && !queryFilter(req.query)) {
+ next('route'); // tells express to move to the next router statement
+ } else {
+ // add the shift version info
+ res.set(config.api.header, config.api.version);
+ // express 4 doesn't directly support promises; so create our own promise chain.
+ // ( express is asynchronous: waiting until a result is generated, or until next() is called; ex. on error. )
+ return Promise.resolve()
+ .then(_ => cb(req))
+ .then(ret => {
+ if (ret !== undefined) {
+ // since most endpoints use json.... that's the default.
+ res.json(ret);
+ } else {
+ // or, send a generic okay.
+ res.status(200);
+ }
+ }).catch((err) => {
+ // was one of our built in error types thrown?
+ if (err.sendError) {
+ err.sendError(res);
+ } else {
+ next(err);
+ }
+ });
+ }
+ }
+}
diff --git a/app/util/parse.js b/app/util/parse.js
new file mode 100644
index 000000000..40254b31d
--- /dev/null
+++ b/app/util/parse.js
@@ -0,0 +1,89 @@
+/**
+ * reads query parameters and url parts
+ */
+const validator = require('validator');
+const dt = require('shift-docs/util/dateTime')
+const { ErrBadRequest } = require('shift-docs/util/errors');
+
+module.exports = {
+ parseBool,
+ parseInt,
+ parseJson,
+ parseRange,
+ parseString,
+ parseYmd,
+};
+
+function parseBool(str) {
+ return (str === undefined) ? undefined :
+ !validator.isBoolean(str) ? null :
+ validator.toBoolean(str);
+}
+
+// options are optional see https://www.npmjs.com/package/validator
+function parseInt(str, options) {
+ return (str === undefined) ? undefined :
+ !validator.isInt(str, options) ? null :
+ validator.toInt(str);
+}
+
+// trim the string if it exists
+function parseString(str) {
+ return (str === undefined) ? undefined :
+ str.trim();
+}
+
+// returns a dayjs date, or false if nothing specified.
+function parseYmd(str) {
+ return (str === undefined) ? undefined :
+ dt.fromYMDString(str, {strict: false});
+}
+
+// read a possible json request into a javascript object.
+// throws a ErrBadRequest if missing data or on any parsing error.
+function parseJson(data) {
+ // sometimes the client sends the data as a json like object;
+ // sometimes it sends it as a string in an object with a member called json.
+ let json;
+ try {
+ json = (data && data.json) ? JSON.parse(data.json) : data;
+ } catch (e) {
+ console.error("error parsing json data", e);
+ }
+ if (!json) {
+ throw new ErrBadRequest("Invalid request");
+ }
+ return json;
+}
+
+// supports two query params: 's' & 'e' in YYYY-MM-DD format.
+// throws a ErrBadRequest if the range was too large
+function parseRange(req, maxRange = 6) {
+ const start = parseYmd(req.query.s) || getDefaultStart();
+ const end = parseYmd(req.query.e) || getDefaultEnd(start);
+ //
+ if (!start.isValid() || !end.isValid()) {
+ throw new ErrBadRequest(`Requested invalid event range: start date was ${start} and end date was ${end}.`);
+ }
+ // FIX: for large ranges, pass as summarize.limit instead of erroring
+ // search allows 'l' & 'o' parameters; so either do that
+ // or support 'page=#' and multiply by max range.
+ const range = end.diff(start, 'month');
+ if ((range < 0) || (range > maxRange)) {
+ throw new ErrBadRequest(`Requested too large a range: start date was ${start} and end date was ${end}.`);
+ }
+ return {
+ start,
+ end
+ };
+}
+
+function getDefaultStart() {
+ const now = dt.getNow();
+ return now.subtract(1, 'month');
+}
+
+function getDefaultEnd(start) {
+ return start.add(6, 'month');
+}
+
diff --git a/app/util/shorthand.js b/app/util/shorthand.js
index 66ea62353..4324368ee 100644
--- a/app/util/shorthand.js
+++ b/app/util/shorthand.js
@@ -2,24 +2,25 @@
// helper for the constants
class Shorthand {
// key: sent b/t client and server.
- // label: human friendly display name
- // value: how it appears in the db
- // both label and value will use the key if left undefined.
- constructor(key, label, value) {
- this.key = key;
- this.label = label !== undefined ? label : key;
- this.value = value !== undefined ? value : key;
+ // value: how it appears in the db.
+ // label: human friendly display name.
+ constructor(...keyValueLabel) {
+ // key is always the first argument
+ this.key = keyValueLabel[0];
+ // value is the middle argument; otherwise the first.
+ this.value = keyValueLabel[keyValueLabel.length < 3 ? 0 : 1];
+ // label is always the last argument
+ this.label = keyValueLabel[keyValueLabel.length - 1];
}
toString() {
return this.key;
}
- // create an enum-like value.
- // the name gets stored directly in the db.
- // the first letter gets used as the shorthand key.
- // ex. "P", and "Portland"
- static Enum(name, label) {
- return new Shorthand(name[0], label || name, name);
+ // create a shorthand based on the first letter of the value.
+ // ex. the key of "Portland" becomes "P"
+ // a label can be specified, otherwise its also the value.
+ static Enum(value, label) {
+ return new Shorthand(value[0], value, label ?? value);
}
// given a class containing a bunch of shorthand members
@@ -34,4 +35,4 @@ class Shorthand {
}
}
-module.exports = { Shorthand };
+module.exports = { Shorthand };
\ No newline at end of file
diff --git a/app/util/tableMaker.js b/app/util/tableMaker.js
index bdce5ab28..7255b753f 100644
--- a/app/util/tableMaker.js
+++ b/app/util/tableMaker.js
@@ -62,6 +62,7 @@ class TableMaker {
setDefaults(column, opt);
}
// 4 byte signed value (-2147483648 to 2147483647)
+ // not required ( can be null ) by default.
integer(name, opt) {
opt = opt || { default: 0, required: false };
// note: in the original tables `int(11)` is a *display* size
@@ -88,4 +89,4 @@ function setDefaults(column, opt) {
}
}
-module.exports = TableMaker;
+module.exports = TableMaker;
\ No newline at end of file
diff --git a/docs/CALENDAR_API.md b/docs/CALENDAR_API.md
index 103532bcf..e712d09bd 100644
--- a/docs/CALENDAR_API.md
+++ b/docs/CALENDAR_API.md
@@ -1,18 +1,36 @@
# Calendar API
+Version: 3.66.0
+
## General
Base URL:
* production: `https://www.shift2bikes.org/api/`
-* local development: `https://localhost:4443/api/`
+* local docker development: `https://localhost:4443/api/`
+* local npm development: `https://localhost:3080/api/`
Most responses are in JSON format, except for:
-* event export returns vCalendar format
+both the ical endpoint and event export return the vCalendar format.
+
+For endpoints which accept POST ( ex. manage and delete ) you can post form data, or use curl to post json. For example:
+
+ > curl -k -H 'Content-Type: application/json' -X POST --data-binary "@delete_event.json" https://localhost:4443/api/delete_event.php
+
+where `delete_event.json` might contain:
+
+ {
+ "id": "6245",
+ "secret": "example"
+ }
+
+For all endpoints, unknown properties -- ex. unknown html form, json, or query parameters - get ignored.
## Viewing events
### Retrieving public event data
+Asks for data regarding a particular instance of an event, or all events happening during a provided date range.
+
Endpoint:
* GET `events`
@@ -21,31 +39,30 @@ Example requests:
* `/events.php?startdate=2019-06-01&enddate=2019-06-15`
URL parameters:
-* `id`:
- * `caldaily` event ID
- * if `id` is provided it takes precedence; all other params will be ignored
-* `startdate`:
- * first day of range, inclusive
- * `YYYY-MM-DD` format
- * if not provided, current date is used
-* `enddate`:
- * last day of range, inclusive
- * `YYYY-MM-DD` format
- * if not provided, current date is used
-* `all`:
- * for range requests only (no effect when `id` is provided)
- * if `true`, delisted events are included, with minimal information provided in their event object; useful for clients that cache results and need to reconcile events that have been removed
+* `id`: a `caldaily` id, containing a positive integer.
+* `startdate`: first day of an inclusive date range, in `YYYY-MM-DD` format.
+* `enddate`: last day of an inclusive date range, in `YYYY-MM-DD` format.
+* `all`: when requesting a date range, and `all=true`, deleted events and delisted days are included in the response (with minimal information). This is useful for clients that cache results and need to reconcile events that have been removed. ( ex. the Bike Fun app. )
+
+A valid request includes either a caldaily id, or a date range. It is invalid to request both an id and a date range.
-Unknown parameters are ignored.
+Responses include an http status code with a json body. The expected response codes are:
-It is recommended that you always provide either an event `id` or both `startdate` and `enddate`. Relying on default or inferred values may return unexpected results.
+* `200` (Success): The response body includes information about the requested event(s).
+* `400` (Bad Request): Either no parameters or too many were sent. Or, one of the parameters couldn't be interpreted as expected. For example, the id wasn't an integer, or one of the dates wasn't a date.
+* `404` (Not Found): The requested `id` doesn't exist, the event isn't published, or it was deleted or revoked.
+* `413` (Content Too Large): The requested date range was too large, 100 days is the maximum.
+* `422` (Unprocessable Content): The requested `enddate` was before the requested `startdate`.
-Success:
-* status code: `200`
-* `events`: array of event objects; array may be empty
-* each event object: key-value pairs of all available public fields; does not contain any private fields (use `manage_event` endpoint for those)
-* when using `id` parameter, array returns 1 object; if the ID does not match a visible event, you will receive a `404` (see Errors, below)
-* for a range request, an empty `events` array is a normal `200` result: it means nothing is scheduled in that range
+Success includes a json response body with:
+
+* `events`: An array of event objects. For a valid query by id, the array will contain exactly one entry. For a valid range query, it may contain zero or more entries. Zero entries -- an empty array -- means nothing is scheduled in that range.
+* each event entry contains key-value pairs of all available public fields. It does not contain any private fields. ( Only the `manage_event` endpoint returns private data. )
+
+Error responses include a json response body with:
+
+* `error`: object containing `message` key.
+* `message`: text string explaining the error.
Example response for a single event:
@@ -119,8 +136,7 @@ Example response for a range of events:
}
}
-
-Example response for a range of events, including delisted events:
+Example response for a range of events:
{
"events": [
@@ -137,36 +153,7 @@ Example response for a range of events, including delisted events:
"newsflash": null,
"status": "A",
"endtime": "10:00:00"
- },
- {
- // for delisted occurrences when there are still valid occurences in the series,
- // all event fields are provided
- "id": "9001",
- "title": "Late-Riser New Year's Day Ride",
- ... // all standard fields
- "date": "2025-01-01",
- "caldaily_id": "10102",
- "shareable": "https://shift2bikes.org/calendar/event-10102",
- "cancelled": true,
- "newsflash": "New start time",
- "status": "D", // D=delisted
- "endtime": null
- },
- {
- // for delisted occurences when the entire event series has been deleted,
- // only these limited fields are provided
- "id": "9002",
- "deleted": true,
- "date": "2025-01-01",
- "caldaily_id": "10103",
- "shareable": "https://shift2bikes.org/calendar/event-10103",
- "cancelled": true,
- "newsflash": null, // always null when delisted
- "status": "D", // D=delisted
- "endtime": null
}
- // if an event is deleted when it is unpublished (aka hidden),
- // the event is entirely gone and is not included here
],
"pagination": {
"start": "2025-01-01",
@@ -178,17 +165,7 @@ Example response for a range of events, including delisted events:
}
}
-Errors:
-* status code: `400`, `404`, `413`, or `422`
-* `error`: object containing `message` key
-* `message`: text string explaining the error
-* possible errors
- * `id` not found (`404`); this also covers events that are unpublished or deleted
- * `enddate` before `startdate` (`422`)
- * date range too large, 100 days maximum (`413`)
- * `startdate`/`enddate` not parseable, or both an `id` and a range were sent (`400`)
-
-Example error:
+Example error response:
{
"error": {
@@ -205,9 +182,10 @@ Some best practices for handling images:
* Clients should be able to handle image request failures (e.g. 404, 429, or 5xx errors).
* Caching is encouraged. The image file has change number after the event ID, e.g. `/eventimages/6245-3.jpg`. This number is incremented only when the image has changed. If the change number is the same, you can safely use a cached image.
-
### Counting events
+> **Note:** This endpoint is experimental. Its syntax and behavior may not be stable yet.
+
Endpoint:
* GET `ride_count`
@@ -215,24 +193,28 @@ Example request:
* `/ride_count.php?s=2025-06-01&e=2025-08-31`
URL parameters:
-* `s`:
- * start date of range, inclusive
- * `YYYY-MM-DD` format; required
-* `e`:
- * end date of range, inclusive
- * `YYYY-MM-DD` format; required
+* `s`: first day of an inclusive date range, in `YYYY-MM-DD` format.
+* `e`: last day of an inclusive date range, in `YYYY-MM-DD` format.
-Unknown parameters are ignored.
+Cancelled, and hidden events are excluded from all counts. `past` and `upcoming` are relative to the current date at the time of the request.
-Cancelled, delisted, and hidden events are excluded from all counts. `past` and `upcoming` are relative to the current date at the time of the request.
+Responses include an http status code with a json body. The expected response codes are:
-Success:
-* status code: `200`
-* `total`: total number of non-cancelled events in the given date range
-* `past`: number of those events whose date is before today
-* `upcoming`: number of those events whose date is today or later
+* `200` (Success):
+* `400`: `s` or `e` missing or not a valid date
-Example response:
+Success includes a json object containing:
+
+* `total`: total number of non-cancelled events in the given date range.
+* `past`: number of those events whose date is before today.
+* `upcoming`: number of those events whose date is today or later.
+
+Error responses include a json response body with:
+
+* `error`: object containing `message` key.
+* `message`: text string explaining the error.
+
+Example success response:
{
"total": 123,
@@ -240,12 +222,7 @@ Example response:
"upcoming": 43
}
-Errors:
-* status code: `400`
-* possible errors
- * `s` or `e` missing or not a valid date
-
-Example error:
+Example error response:
{
"error": {
@@ -253,9 +230,6 @@ Example error:
}
}
-> **Note:** This endpoint is experimental. Its syntax and behavior may not be stable yet.
-
-
### Searching events
Endpoint:
@@ -277,16 +251,23 @@ URL parameters:
* limit — maximum number of results to return
* integer; default and maximum `25`
* `all`:
- * if `true` or `1`, searches past events in descending order; otherwise searches today and future events in ascending order
+ * if `true` or `1`, searches past events in descending order; otherwise searches today and future events in ascending order. ( note: this is a different meaning of the all parameter than other endpoints. )
-Unknown parameters are ignored.
+Responses include an http status code with a json body. The expected response codes are:
-Success:
-* status code: `200`
-* `events`: array of event objects in the same format as the `events` endpoint; array may be empty
+* `200` (Success): The response body includes information about zero or more events matching the requested search parameters.
+* `400` (Bad Request): Missing the `q` parameter.
+
+Error responses include a json response body with:
+
+* `error`: object containing `message` key
+* `message`: text string explaining the error
+
+Success includes a json response body with:
+* `events`: array of zero or more event objects in the same format as the `events` endpoint.
* `pagination`: object with offset/limit/fullcount for fetching additional pages
-Example response:
+Example success response:
{
"events": [
@@ -302,12 +283,7 @@ Example response:
}
}
-Errors:
-* status code: `400`
-* possible errors
- * missing `q` parameter
-
-Example error:
+Example error response:
{
"error": {
@@ -315,7 +291,6 @@ Example error:
}
}
-
### Exporting an event
Endpoint:
@@ -328,7 +303,7 @@ Example request:
URL parameters:
* `event_id`:
- * `caldaily` ID (single occurrence)
+ * `caldaily` ID (single occurrence), containing a positive integer.
* if `event_id` is provided it takes precedence; all other params will be ignored
* `series_id`:
* `calevent` ID (all events in a series)
@@ -343,18 +318,14 @@ URL parameters:
* last day of range, inclusive
* `YYYY-MM-DD` format
* if not provided, current date is used
-* `all`:
- * for range requests only (no effect when `id` is provided)
- * if `true`, delisted events are included; useful for clients that cache results and need to reconcile events that have been removed
* `filename`:
* if `none`, the output is plain text instead of an ICS file; useful for local debugging
+* `all`: when requesting a date range, and `all=true`, deleted events and delisted days are included in the response (with minimal information). This is useful for clients that cache results and need to reconcile events that have been removed. ( ex. the Bike Fun app. )
If multiple ID/range parameters are provided, they take precedence with decreasing specificity: `event_id`, `series_id`, `startdate`/`enddate`.
If no parameters are provided, it responds with a range from 1 month prior to 6 months forward from the current date.
-Unknown parameters are ignored.
-
Example response for a single event:
BEGIN:VCALENDAR
@@ -406,15 +377,21 @@ Endpoint:
* GET `retrieve_event`
URL parameters:
-* `id`: `calevent` event ID
-* `secret`: event password
+* `id`: `calevent` event ID, containing a positive integer.
+* `secret`: event password, as a string.
The `retrieve_event` endpoint returns all private data for the event (if the `secret` is provided) so it can be edited. If you just want to retrieve public data to display the event, use the `event` endpoint.
-Success:
-* status code: `200`
+Responses include an http status code with a json body. The expected response codes are:
+
+* `200` (Success): The response body includes information about the requested event(s).
+* `400` (Bad Request): No `id` specified, or wasn't an integer.
+* `404` (Not Found): Query by `id` for an event that doesn't exist, isn't published, was deleted, or revoked; for an unpublished event without a valid `secret`.
+
+Success includes a json response body with:
+
* key-value pairs of all available fields; the response is similar to the `event` endpoint's event object, but note that they are not identical (the `datestatuses` block, for example)
-* if a valid `secret` is provided, all stored values are returned; if not, you still get a `200` response but private fields (e.g. `email`) will be empty
+* if a valid `secret` is provided, all stored values are returned; if not, private fields (e.g. `email`) will be empty.
Example response:
@@ -457,19 +434,12 @@ Example response:
]
}
-Errors:
-* status code: `400` or `404`
-* possible errors
- * no `id` specified (`400`)
- * `id` not found (`404`); also returned for a deleted event, and for an unpublished one without a valid `secret`
-
-
### Adding or updating an event
Endpoint:
* POST `manage_event`
-Request can be sent as JSON, or as multipart/form-data containing binary image data plus JSON.
+The request can be posted as JSON, or as a multipart/form-data containing binary image data, and a field called `json` with stringified data.
URL parameters:
* none
@@ -519,8 +489,6 @@ Request body:
* `featured`: boolean; featured ride status, set by admins and ignored if provided by the user
* `published`: boolean; set by the server, ignored if provided by the user
-Unknown properties are ignored.
-
Example JSON request:
{
@@ -559,68 +527,62 @@ Example JSON request:
]
}
-Success:
-* status code: `200`
-* if a valid `id` is provided (to update an event), a valid `secret` must also be included
-* response body is the same as the `retrieve_event` endpoint
-
-Errors:
-* status code: `400` or `403`
-* possible errors
- * no request body or not parseable JSON (`400`)
- * required field was not included, or has an invalid value (`400`)
- * invalid or missing `secret` when updating (`403`); also returned for an unknown `id`, so that it isn't revealed whether the event exists
-
-Example error:
-
- {
- "error": {
- "message": "Invalid secret, use link from email"
- }
- }
-
-To add an image to the event, use a multipart/form-data request and send the image as binary. Accepts gif, jpeg, pjpeg, and png images, up to 5 MB.
+To add an image to the event, use a multipart/form-data request and send the image as binary. The endpoint accepts gif, jpeg, pjpeg, and png images, up to 5.5 MB.
Example multipart/form-data request:
-----------------------------1234123412341234123412341234
Content-Disposition: form-data; name="file"; filename="image.jpg"
Content-Type: image/jpeg
-
+
-----------------------------1234123412341234123412341234
Content-Disposition: form-data; name="json"
-
+
{ "id":"99999", ..., "datestatuses": [ { ... } ]}
-----------------------------1234123412341234123412341234--
-Success:
-* status code: `200`
-* response body is the same as when submitting a JSON-only request
+Responses include an http status code with a json body. The expected response codes are:
-Errors:
-* status code: `400` or `413`
-* possible errors
- * unsupported file type (`400`)
- * file size too large (`413`)
+* `200` (Success): The returned event data is valid.
+* `400` (Bad Request): The request body wasn't valid JSON, or a required field was either not included or has an invalid value. This also includes images with unsupported file types.
+* `403` (Forbidden): Returned for an unknown `id`, or a missing or mismatched `secret`.
+* `413` (Content Too Large): The posted image was too large.
-Example error:
+Success includes a json response containing:
+
+ {
+ "id": string,
+ "published": bool,
+ "image": optional, new url of published image
+ }
+
+Example generic error:
+
+ {
+ "error": {
+ "message": "Invalid secret, use link from email"
+ }
+ }
+
+Example image errors:
{
"error": {
"message": "There were errors in your fields",
"fields": {
- "file": "The file uploaded is not an image"
+ "image": "The file uploaded is not an image"
}
}
}
-
### Deleting an event
Endpoint:
* POST `delete_event`
+The request can be posted as JSON, or as a form containing a single field called `json` with stringified data.
+
URL parameters:
* none
@@ -631,8 +593,6 @@ Request body:
* optional fields
* none
-Unknown properties are ignored.
-
Example request:
{
@@ -640,25 +600,19 @@ Example request:
"secret": "example"
}
-Success:
-* status code: `200`
-* `success` true message
+Responses include an http status code with a json body. The expected response codes are:
-Example response:
+* `200` (Success): The event was successfully deleted.
+* `400` (Bad Request): Returned if the request body wasn't valid JSON, or the `id` or `secret` wasn't included.
+* `403` (Forbidden): Returned when the `id` wasn't found, invalid, or a mismatched `secret` was given.
+
+Example success json:
{
"success": true
}
-Errors:
-* status code: `400`, `403`, or `404`
-* possible errors
- * no request body or not parseable JSON (`400`)
- * `id` not included (`400`)
- * `id` not found (`404`)
- * invalid or missing `secret` (`403`)
-
-Example error:
+Example error json:
{
"error": {
@@ -747,3 +701,4 @@ As with v1, there were probably revisions to v2 during this time, but changelog
* 3.63.0: (2026-06-11) Removed the `crawl` endpoint, which is no longer used by any known clients or crawlers. Removed stale Flourish/PHP references from docs and comments.
* 3.64.0: (2026-10-01) The events and ICS export endpoints now return `404` when an id doesn't match a visible event; they previously returned `400`. (The ICS export was already documented as returning `404`.) Response bodies are unchanged. Range requests are unaffected: a range with nothing scheduled is still a `200` with an empty `events` array.
* 3.65.0: (2026-10-08) More specific error status codes, replacing some generic `400`s: `403` for a bad or missing event `secret` (manage, delete), `404` for an id that matches no event (retrieve, delete), `413` for a date range over the maximum, and `422` for an `enddate` before its `startdate` (events, ICS export). Response bodies are unchanged.
+* 3.66.0: (2026-10-14) 1) Narrowed the response of the `manage` endpoint. Rather than returning the whole event, it returns only a minimal status. 2) in cases where delete would have returned 404 ( not found ), it now returns 403 ( forbidden. )
diff --git a/docs/TABLES-V2.md b/docs/TABLES-V2.md
index 3b4dc87bc..05817f824 100644
--- a/docs/TABLES-V2.md
+++ b/docs/TABLES-V2.md
@@ -6,9 +6,10 @@ The core event data.
`cal_series`:
* `id` auto assigned integer: The event and all of its related info binds to this id. ( duplicates the existing `calevent.id` )
-* `created` : Timestamp of when the series was first added to the db.
+* `created`: Timestamp of when the series was first added to the db.
* `modified`: Timestamp of the most recent change to the series data.
-* `published` integer or null. A positive integer value means published and visible to the world. Zero means not yet published by the organizer. Increments every time the series is saved ( for the sake of ical feeds ). `null` means "revoked" -- they are skipped by all queries.
+* `published`: A positive integer value means published and visible to the world. Zero means not yet published by the organizer. Increments every time the series is saved ( for the sake of ical feeds ).
+* `revoked`: A string containing a reason, or null if not revoked. The backend stores the string 'true' when revoked by the organizer.
* `title` string: human readable name of the event.
* `organizer` string: Public visible name of the person or group who authored the event.
* `start_time` string: *local* time as HH:DD:00. **IMPORTANT**: this isn't a mysql timestamp! if an organizer on the east coast says "ride starts at 8AM" we can look in the db expect to see "08:00:00" regardless of their time zone, our time zone, or the server's timezone.
@@ -24,7 +25,7 @@ A specific instance of a given series on a particular day. These are uniquely id
`cal_schedule`:
* `id` ref: The series id.
* `ymd`: Day that the event takes place as a YYYY-MM-DD string. Multiple days can exist for a single series. Individual days are unique within each series.
-* `is_scheduled`: 0 or 1, implying cancelled or active respectively.
+* `is_scheduled`: 0 (cancelled), 1 (active), or null (delisted). If a previously active day was removed without being explictly called, it is considered delisted. Deleting an event is one way this can happen.
* `news`: When an organizer reschedules or cancels a given day, this string allows them to explain why.
* `added`: timestamp of when this entry was created.
* `changed`: timestamp of when this entry was last modified.
@@ -107,7 +108,6 @@ The SERIES start_time and the SCHEDULE days are stored as **strings**, `HH:MM:00
Database created and modified times are stored as TIMESTAMP(s) ( ex. `2023-02-27 18:59:39` ) and are from the perspective of the server.
----
# Views
A few views provide backwards compatibility with the original table format:
@@ -116,7 +116,7 @@ A few views provide backwards compatibility with the original table format:
Returns the scheduled days of published events, joining the event and scheduling data.
-## view_public_series:
+## view_published_series:
Returns the public data of published events, similar to the `calevent` table data.
@@ -159,3 +159,13 @@ Represents a single occurrence on a particular day, similar to the `caldaily` ta
* `eventdate`: YYYY-MM-DD string
* `newsflash`: organizer provided string.
* `eventstatus` either `A` ( active ) or `C` ( cancelled )
+
+# Deleted Events and Delisted Days
+
+Deleted events occur when a ride is revoked by a shift administrator, or when an organizer wants to remove public data about their event.
+
+Delisted days are a day that used to exist on the schedule, that were removed without being explicitly marked as cancelled.
+
+Deleted events and delisted days are tracked in the db for the sake of clients ( esp. the BikeFun app ) which need look at differences of data to know when something changed.
+
+This means that data is not immediately purged from the db when a ride is marked for deletion. Instead, they are flagged as "revoked" which excludes them from the API. A chron job should fully cleanout details about these events after about a month: only the `cal_series` entry will exist, and its title, description, etc. will be nulled out.
diff --git a/app/test/testing.md b/docs/TESTING.md
similarity index 89%
rename from app/test/testing.md
rename to docs/TESTING.md
index 4d3313bb8..16999af62 100644
--- a/app/test/testing.md
+++ b/docs/TESTING.md
@@ -2,16 +2,21 @@
To test the backend, at the root of repo run: `npm test`.
-
All tests use the [Node Test runner](https://nodejs.org/docs/latest/api/test.html#test-runner), with [supertest](https://github.com/forwardemail/supertest) for making (local) http requests.
-## Isolating tests:
+## Database Selection:
+
+By default tests use sqlite, you can test against mysql as well: `npm test -- -db=mysql`. Mysql launches a standalone docker container for the tests. Sqlite `-db=sqlite` can be followed by a colon and a filename to persist test data to disk. ex. `-db=sqlite:test.db`.
-Tests can be identified by name: ex. `npm test -- --test-name-pattern="ical feed"`
+Additionally, `npm test -- -db_debug` will log queries to the db.
-Or, temporarily can be marked with 'only' in the code. For example: `describe.only()`, and then selected with: `npm test -- --test-only```
+## Selecting tests:
+
+`-f` can be used to select any part of a test filename ( ex. `npm test -- -f=manage` would test "manage_test.vscript" )
+
+## Isolating tests:
-By default tests use sqlite, you can test against mysql as well: `npm test -db=mysql`. It launches a standalone docker container for the tests. Additionally, `npm test -db_debug` will log queries to the db.
+Tests can also temporarily be marked with `only` in the code, and then selected via `-only` at the command line. For example: mark a test with `describe.only()`, and then selected it with: `npm test -- -only`.
# Test Data
diff --git a/package.json b/package.json
index 8ad6fd487..0570479a3 100644
--- a/package.json
+++ b/package.json
@@ -19,7 +19,7 @@
"dev": "concurrently -k \"npm:dev-*\"",
"predev": "env-cmd -f tools/dev.env node tools/setupEventImages.js",
- "dev-app": "env-cmd -f tools/dev.env npm -w app start",
+ "dev-app": "env-cmd -f tools/dev.env npm -w app start --",
"dev-hugo": "exec-bin bin/hugo/hugo -w -s site -e development -d ../bin/dist",
"dev-cal": "env-cmd -f tools/dev.env npm -w cal run dev",
@@ -39,8 +39,5 @@
"preview-hugo": "npm run dev-hugo",
"preview-cal": "npm -w cal run build",
"preview-app": " npm -w tools run preview"
- },
- "devDependencies": {
- "http-proxy-middleware": "3.0.5"
}
}
diff --git a/services/db/conf.d/charset.cnf b/services/db/conf.d/charset.cnf
index 6a4687f04..b698b9d49 100644
--- a/services/db/conf.d/charset.cnf
+++ b/services/db/conf.d/charset.cnf
@@ -7,10 +7,16 @@
#
# Setting these here (rather than per-session) makes utf8mb4 the durable
# default for every connection and every `mysql` session.
+#
+# NOTE: init_connect (collation setup) doesn't run if you are 'root'.
+# you can check the character set and collation settings in mysql by running:
+# mysql> SHOW VARIABLES LIKE 'collation%';
+# mysql> SHOW VARIABLES LIKE 'char%';
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
+init_connect=SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';
[client]
default-character-set=utf8mb4
diff --git a/site/themes/s2b_hugo_theme/assets/js/cal/addevent.js b/site/themes/s2b_hugo_theme/assets/js/cal/addevent.js
index 5b6e95ee4..646b48d46 100644
--- a/site/themes/s2b_hugo_theme/assets/js/cal/addevent.js
+++ b/site/themes/s2b_hugo_theme/assets/js/cal/addevent.js
@@ -116,8 +116,9 @@
return options;
}
+ // note: when creating new events, all of the shiftEvent fields can be empty ( undefined )
function populateEditForm(shiftEvent, callback) {
- var template, rendered;
+ const container = $('#mustache-edit');
if (!shiftEvent.time) {
shiftEvent.time = DEFAULT_TIME;
@@ -139,8 +140,13 @@
}
shiftEvent.rideLengthOptions = populateMenuOptions(RIDE_LENGTH, shiftEvent.ridelength);
- template = $('#mustache-edit').html();
- rendered = Mustache.render(template, shiftEvent);
+ if (!shiftEvent.title) {
+ shiftEvent.title = "";
+ }
+ shiftEvent.tinyplaceholder = container.truncateString(shiftEvent.title, 24);
+
+ const template = container.html();
+ const rendered = Mustache.render(template, shiftEvent);
callback(rendered);
$('#date-select').setupDatePicker(shiftEvent['datestatuses'] || []);
@@ -213,9 +219,11 @@
$('#success-message').text('Your event has been updated!');
$('#success-modal').modal('show');
// update the image in case it was changed.
- let imgDisplay = $('div.image-display').find('a');
- imgDisplay.attr("href", returnVal.image);
- imgDisplay.find("img").attr("src", returnVal.image);
+ if (returnVal.image) {
+ const imgDisplay = $('div.image-display').find('a');
+ imgDisplay.attr("href", returnVal.image);
+ imgDisplay.find("img").attr("src", returnVal.image);
+ }
} else {
let newUrl = 'event-submitted';
history.pushState({}, newUrl, newUrl);
@@ -354,6 +362,9 @@
previewEvent['mapLink'] = $form.getMapLink(previewEvent['address']);
previewEvent['webLink'] = $form.getWebLink(previewEvent['weburl']);
previewEvent['contactLink'] = $form.getContactLink(previewEvent['contact']);
+ if (!previewEvent.tinytitle) {
+ previewEvent.tinytitle = $form.truncateString(previewEvent.title, 24);
+ }
$form.hide();
mustacheData = {
diff --git a/site/themes/s2b_hugo_theme/layouts/partials/cal/edit.html b/site/themes/s2b_hugo_theme/layouts/partials/cal/edit.html
index d8efa57d6..bf2f681ea 100644
--- a/site/themes/s2b_hugo_theme/layouts/partials/cal/edit.html
+++ b/site/themes/s2b_hugo_theme/layouts/partials/cal/edit.html
@@ -410,7 +410,7 @@
-
+
diff --git a/tools/empty.env b/tools/empty.env
deleted file mode 100644
index ffbd2a746..000000000
--- a/tools/empty.env
+++ /dev/null
@@ -1,3 +0,0 @@
-# this replaces the dev.env in production
-# and is meant to be empty
-# ( except for, you know, these comments )
diff --git a/tools/geoSearch.js b/tools/geoSearch.js
index 52ebc4199..623b4ed81 100644
--- a/tools/geoSearch.js
+++ b/tools/geoSearch.js
@@ -24,7 +24,7 @@ const knex = require('knex'); // the raw knex, and not the shift-docs
const sqlstring = require('sqlstring'); // part of mysql/2; note: "sql-escape-string" works better for reimport into sqlite ( because sqlstring assumes backslash escaping; which std sql doesnt allow )
const dt = require("shift-docs/util/dateTime");
const nunjucks = require("shift-docs/nunjucks");
-const { Area, Review } = require('shift-docs/models/calConst');
+const { Area, Review } = require('shift-docs/models/newConst');
// https://nominatim.org/release-docs/develop/api/Overview/
const nominatim = {
diff --git a/tools/makeFakeEvents.js b/tools/makeFakeEvents.js
index 99fd0d919..2d953200b 100644
--- a/tools/makeFakeEvents.js
+++ b/tools/makeFakeEvents.js
@@ -2,9 +2,9 @@
* create one or more fake events.
* ex. npm run -w tools make-fake-events
*/
-const knex = require("shift-docs/db");
+const db = require('shift-docs/db');
const dt = require("shift-docs/util/dateTime");
-const { makeFakeData } = require("shift-docs/test/fakeData");
+const fakeData = require("shift-docs/testing/fakeData");
// todo: improve commandline parsing
// this uses npm's command vars ( probably a bad idea )
@@ -31,8 +31,9 @@ async function makeFakeEvents() {
const firstDay = dt.getNow().add(args.start, 'days');
const lastDay = firstDay.add(args.range, 'days');
const numEvents = args.make;
- return knex.initialize().then(_ => {
- return makeFakeData(firstDay, lastDay, numEvents);
+ return db.initialize().then(_ => {
+ const faked = fakeData.generateFakeData(firstDay, lastDay, numEvents);
+ return fakeData.insertFakeData(db, faked);
})
.then(_ => {
console.log("done");
diff --git a/tools/migrate.js b/tools/migrate.js
index 7323b8850..3d1a5b09d 100644
--- a/tools/migrate.js
+++ b/tools/migrate.js
@@ -11,7 +11,7 @@
* > ./shift mysql-pipe < reorg.sql
*
* Example via external communication with docker:
- * > npm run --silent -w tools sqlite-migrate -- -db=mysql:63064 > reorg.sql
+ * > npm run --silent -w tools mysql-migrate -- -db=mysql:54166 > reorg.sql
*
* note, the first few lines may sometimes contain stray commandline logging.
*/
@@ -123,21 +123,18 @@ function buildData(out, events) {
const evt = events[id];
const invalid = evt.allDays.length == 1 && evt.allDays[0].pkid === null;
if (invalid) {
- // TBD: we could keep these i suppose.
- // a series ith no days.
+ // TBD: keep series with no days?
out.note(evt.id, `skipping because it no had scheduled days. review: ${evt.review} title: "${evt.title}"`);
} else {
const days = evt.allDays.filter(at => isScheduled(at) !== undefined);
- const skips = evt.allDays.filter(at => ['S', 'D'].includes(at.eventstatus)).length;
+ const skips = evt.allDays.filter(at => at.eventstatus === 'S').length;
const diff = evt.allDays.length - days.length;
if (diff > 0 && diff !== skips) {
- evt.allDays.forEach(at => {
- console.log(at);
- });
- const err = out.warn(evt.id, `REMOVED ${diff} days; ${skips} skipped.`);
- throw new Error(err);
+ evt.allDays.forEach(at => console.log(at));
+ out.error(evt.id, `REMOVED ${diff} days; ${skips} skipped.`);
}
+ reorderTimes(evt);
seriesTable(out, evt);
imageTable(out, evt);
locTable(out, evt);
@@ -172,27 +169,28 @@ function seriesTable(out, evt) {
// zero and null are considered visible.
const hidden = evt.hidden === 1;
const published = !hidden ? (evt.changes || 1) : 0;
- //
- let { modified, created } = evt;
- if (dt.convert(modified).isBefore(created)) {
- const swap = modified;
- modified = created;
- created = modified;
- }
// munge and don't store if we can generate it.
- const munged = (evt.title || "").substring(0, 24).trim();
- const dbtiny = (evt.tinytitle || "").trim();
+ const munged = nullString(evt.title, 24);
+ const dbtiny = nullString(evt.tinytitle);
+ const summary = nullString(evt.printdescr);
+
+ // a few rides have the text "none" for the tiny title or summary.
+ function eatNone(str) {
+ return str === "none" ? null : str;
+ }
out.insert('cal_series', {
id: evt.id,
published,
+ // events marked with review 'E' are marked as revoked ( aka deleted )
+ revoked: (evt.review === 'E') ? 'excluded' : null,
title: evt.title,
organizer: evt.name,
start_time: evt.eventtime,
scheduled_duration: evt.eventduration,
tiny_title: eatNone((munged === dbtiny) ? null : dbtiny),
- summary: eatNone(evt.printdescr || null),
+ summary: eatNone(summary),
details: evt.descr,
created: evt.created,
modified: evt.modified,
@@ -215,6 +213,7 @@ function imageTable(out, evt) {
}
}
}
+
function locTable(out, evt) {
out.insert('cal_location', {
id: evt.id,
@@ -225,13 +224,15 @@ function locTable(out, evt) {
place_info: evt.locdetails,
time_info: evt.timedetails,
});
- const locEnd = (evt.locend + '').trim();
+
+ // only write the loc end if needed
+ const locEnd = nullString(evt.locend);
if (locEnd) {
out.insert('cal_location', {
id: evt.id,
loc_type: LocType.Finish,
place_name: locEnd,
- // only supported for start right now.
+ // these fields only supported for loc start right now.
address: null,
place_info: null,
time_info: null,
@@ -244,29 +245,38 @@ function privateTable(out, evt) {
const printable = !!evt[`print${field}`]; // false if never set (ie. null)
return Showable.combine(visible, printable);
});
+ const email = nullString(evt.email);
+ if (email === null) {
+ // ride 732, joe the plubmer, has a null email
+ out.note(evt.id, `has a null email`);
+ }
out.insert('cal_private', {
id: evt.id,
- secret: evt.password || null,
- private_email: evt.email || null,
- private_phone: evt.phone || null,
- private_contact: evt.contact || null,
+ secret: nullString(evt.password),
+ private_email: email,
+ private_phone: nullString(evt.phone),
+ private_contact: nullString(evt.contact),
show_email: showEmail.value,
show_phone: showPhone.value,
show_contact: showContact.value,
});
}
+// created and modified times can be a little funky.
+function reorderTimes(evt) {
+ const { created, modified } = evt;
+ // console.log("created", dt.icalFormat(created), "modified", dt.icalFormat(modified));
+ if (dt.convert(modified).isBefore(dt.convert(created))) {
+ evt.created = modified;
+ evt.modified = created;
+ }
+}
function scheduleTable(out, evt, days) {
for (const at of days) {
if (!at.pkid) {
- throw new Error("pkid");
+ out.error(evt.id, "missing pkid");
}
const scheduled = isScheduled(at);
assert(scheduled !== undefined);
- if (evt.modified < evt.created) {
- const swap = evt.created;
- evt.created = evt.modified;
- evt.modified = swap;
- }
out.insert('cal_schedule', {
id: at.id,
ymd: at.eventdate,
@@ -274,7 +284,7 @@ function scheduleTable(out, evt, days) {
pkid: at.pkid,
added: evt.created,
changed: evt.modified,
- news: at.newsflash || null,
+ news: nullString(at.newsflash),
});
}
}
@@ -284,14 +294,12 @@ function tagTable(out, evt) {
// two of which seem to be tests... dunno what's best for them.
tags.area = evt.area ? Area.findShorthand(evt.area)?.value : Area.Portland.toString();
if (!tags.area) {
- const msg = out.note(evt.id, `unknown area ${evt.area}`)
- throw new Error(msg);
+ out.error(evt.id, `unknown area ${evt.area}`);
}
//
tags.audience = evt.audience ? Audience.findShorthand(evt.audience)?.value : Audience.General.toString();
if (!tags.audience) {
- const msg = out.note(evt.id, `unknown audience ${evt.audience}`)
- throw new Error(msg);
+ out.error(evt.id, `unknown audience ${evt.audience}`);
}
// many rides have a null ridelength
const dist = Distance.findShorthand(evt.ridelength)?.value;
@@ -301,14 +309,16 @@ function tagTable(out, evt) {
// dunno why but series 7283 has a length of "12"
// nothing else is like that.
if (evt.ridelength == 12) {
- evt.ridelength = '8-15';
+ const forcedValue = '8-15';
+ out.note(evt.id, `rewriting ride length '${evt.ridelength}' as '${forcedValue}'`);
+ evt.ridelength = forcedValue;
} else {
- out.note(evt.id, `unknown ride length ${evt.ridelength}`);
+ out.error(evt.id, `unknown ride length '${evt.ridelength}'`);
}
}
// false zero or null
if (!!evt.loopride) {
- tags.loop = "true";
+ tags.loopride = "true";
}
if (!!evt.safetyplan) {
tags.safety = "true";
@@ -327,23 +337,29 @@ function tagTable(out, evt) {
}
}
function webTable(out, evt) {
- if (evt.weburl || evt.webname) {
+ const name = nullString(evt.webname);
+ const url = nullString(evt.weburl);
+ if (name || url) {
const visible = true;
const printable = !!evt.printweburl; // false if never set ( null )
const show = Showable.combine(visible, printable);
out.insert('cal_web', {
id: evt.id,
web_type: 'url',
- web_text: evt.webname || null,
- web_link: evt.weburl,
+ web_text: name,
+ web_link: url,
show_weburl: show.value,
});
}
}
// ---- helpers
-function eatNone(str) {
- return str === "none" ? null : str;
+// turn null, or empty strings into null
+// ( where empty means: only spaces or no text at all )
+// clip optionally caps the string to the passed number of characters
+function nullString(v, clip) {
+ return (v || "").substring(0, clip).trim() || null;
}
+
// does the passed data have an valid values other than 'series'
function hasData(d, ignore = []) {
const keys = Object.keys(d);
@@ -380,7 +396,9 @@ function getImageOverride(image) {
}
// ex. "png"
function isValidExt(ext) {
- // NOTE: this drops a random cfm file;
+ // NOTE: this drops a random cfm file; jfif is another kind of jpg.
+ // not all of these are allowed for upload;
+ // but they exist in the db already; so lets keep them as is.
const validExts = ['gif', 'png', 'jpg','jpeg','pjpeg', 'pjp', 'jfif'];
return validExts.includes(ext.toLowerCase());
}
@@ -388,10 +406,9 @@ function isValidExt(ext) {
function isScheduled(at) {
// the complete set of event status are:
// A, C, D, E, S. none were null.
- // we only care about "active" and "cancelled"
- // exclusions get rewritten into regular events.
- // we dont need to remember deleted data
- // skips were an artifact of scheduling.
+ // we only care about A, C, D (active, cancelled, delisted).
+ // E: exclusions get rewritten into regular events.
+ // S: skips were an artifact of scheduling.
return EventStatus.findShorthand(at.eventstatus)?.value;
}
@@ -413,13 +430,18 @@ class Output {
}
t.rows.push(cols);
}
+ note(id, msg) {
+ this.comments.push(`note: event ${id} ${msg}`);
+ return msg;
+ }
warn(id, msg) {
this.comments.push(`WARNING: event ${id} ${msg}`);
return msg;
}
- note(id, msg) {
- this.comments.push(`note: event ${id} ${msg}`);
- return msg;
+ error(id, msg) {
+ const comment = `ERROR: event ${id} ${msg}`;
+ this.comments.push(comment);
+ throw new Error(comment);
}
}
@@ -427,7 +449,7 @@ class Output {
// (safe for the text file output)
function formatValue(columnName, v) {
if (v === undefined) {
- throw new Error('invalid values');
+ throw new Error(`invalid values for ${columnName}`);
}
if (v === null) {
return "NULL";
diff --git a/tools/package.json b/tools/package.json
index 2c21b4842..33531da54 100644
--- a/tools/package.json
+++ b/tools/package.json
@@ -8,7 +8,6 @@
"dependencies": {
"@faker-js/faker": "~8.4.1",
"env-cmd": "~10.1.0",
- "express-http-proxy": "~2.1.1",
"http-proxy-middleware": "~3.0.5",
"shift-docs": "1.0.0",
"sqlstring": "~2.3.3"
diff --git a/tools/setupEventImages.js b/tools/setupEventImages.js
index 04cb10894..d94fd9368 100644
--- a/tools/setupEventImages.js
+++ b/tools/setupEventImages.js
@@ -1,32 +1,38 @@
-// run by npm predev ( automatically as part of running dev )
-// ensures the event images directory exists
-// and that it has the `bike.jpg` used by fake events.
+//
+// Ensures the image directory needed for node development exists,
+// and that it contains the `bike.jpg` used by the `make-fake-events` tool.
+// This tool is run automatically by `npm dev` ( as part of its `predev` cycle. )
+//
const path = require("path");
const fs = require('fs').promises;
const config = require("shift-docs/config");
+const images = require('shift-docs/images');
-const dstDir = config.image.dir;
-const srcDir = path.resolve(config.appPath, 'eventimages');
-const imageFile = "bike.jpg"; // lives in the repo
-
-async function setupEventImages() {
+async function setupEventImages(dstDir) {
console.log("initializing event images...");
- if (srcDir !== dstDir) {
- const src= path.join(srcDir, imageFile);
- const dst= path.join(dstDir, imageFile);
- // ignore failures to create the dir
- await fs.mkdir(dstDir).catch(e => null);
- // copy and log.
- await fs.copyFile(src, dst, fs.constants.COPYFILE_EXCL).then(_ => {
- console.log(`ok: copied "${src}" to "${dst}"`);
- }).catch(e => {
+ await fs.mkdir(dstDir)
+ .then(_ => console.log(`ok: created "${dstDir}"`))
+ .catch(e => {
if (e.code === 'EEXIST') {
- console.log(`ok: "${dst}" already exists.`);
+ console.log(`ok: "${dstDir}" already exists.`);
} else {
- console.error(`ng: couldn't copy "${src}" to "${dst}"`, e.message);
+ console.error(`ng: couldn't create "${dstDir}"`, e.message);
}
});
- }
+
+ // now, copy the files and log success
+ const src = images.bike.jpg;
+ const dst = path.join(config.image.dir, "bike.jpg");
+ await fs.copyFile(src, dst, fs.constants.COPYFILE_EXCL).then(_ => {
+ console.log(`ok: copied "${src}" to "${dst}"`);
+ }).catch(e => {
+ if (e.code === 'EEXIST') {
+ console.log(`ok: "${dst}" already exists.`);
+ } else {
+ console.error(`ng: couldn't copy "${src}" to "${dst}"`, e.message);
+ }
+ });
+
process.exit(); // done.
}
-setupEventImages();
+setupEventImages(config.image.dir);