mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
34 schema files wrote the same two autoValue functions out by hand, 65 times in five spellings: createdAt set on insert and $setOnInsert on upsert, unset otherwise; modifiedAt set on every write. They are now createdAtAutoValue and modifiedAtAutoValue in models/lib/schemaTimestamps.js, named by each field as `autoValue: createdAtAutoValue`. models/settings.js, which may not use import statements, requires them. Schemas whose rule differs keep their own: changeHistory keeps a createdAt the caller set, and listSyncCredentials' updatedAt does not unset. tests/schemaTimestamps runs both functions in SimpleSchema's insert, upsert, update and validation contexts, and fails when a schema writes either one out by hand again. Thanks to xet7 !
110 lines
3.0 KiB
JavaScript
110 lines
3.0 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { Mongo } from 'meteor/mongo';
|
|
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import { createdAtAutoValue, modifiedAtAutoValue } from '/models/lib/schemaTimestamps';
|
|
const { SimpleSchema } = require('/imports/simpleSchema');
|
|
|
|
const CardTextNotes = new Mongo.Collection('card_text_notes');
|
|
|
|
/**
|
|
* A markdown text note attached to a card (GitHub issue #595): a small,
|
|
* separate, editable block of markdown text a card can carry in addition to
|
|
* its own description, so a card is not limited to a single description
|
|
* field for everything it needs to say. Modeled on Checklists - a per-card
|
|
* collection of small named sub-documents - rather than on Attachments,
|
|
* because a text note is edited in place, not uploaded/downloaded.
|
|
*/
|
|
CardTextNotes.attachSchema(
|
|
new SimpleSchema({
|
|
boardId: {
|
|
/**
|
|
* the board ID, denormalized from the card so the board publication
|
|
* can publish text notes with a single reactive cursor filtered by
|
|
* boardId, the same pattern used by Checklists/CardComments/Attachments.
|
|
*/
|
|
type: String,
|
|
},
|
|
cardId: {
|
|
/**
|
|
* the ID of the card the text note is on
|
|
*/
|
|
type: String,
|
|
},
|
|
title: {
|
|
/**
|
|
* the title of the text note
|
|
*/
|
|
type: String,
|
|
defaultValue: '',
|
|
},
|
|
text: {
|
|
/**
|
|
* the markdown text of the note
|
|
*/
|
|
type: String,
|
|
optional: true,
|
|
defaultValue: '',
|
|
},
|
|
createdAt: {
|
|
/**
|
|
* creation date of the text note
|
|
*/
|
|
type: Date,
|
|
autoValue: createdAtAutoValue,
|
|
},
|
|
modifiedAt: {
|
|
type: Date,
|
|
autoValue: modifiedAtAutoValue,
|
|
},
|
|
userId: {
|
|
/**
|
|
* the ID of the user who created the text note
|
|
*/
|
|
type: String,
|
|
// eslint-disable-next-line consistent-return
|
|
autoValue() {
|
|
if (this.isInsert && !this.isSet) {
|
|
return this.userId;
|
|
}
|
|
},
|
|
},
|
|
}),
|
|
);
|
|
|
|
CardTextNotes.helpers({
|
|
card() {
|
|
return ReactiveCache.getCard(this.cardId);
|
|
},
|
|
user() {
|
|
return ReactiveCache.getUser(this.userId);
|
|
},
|
|
});
|
|
|
|
CardTextNotes.before.insert((userId, doc) => {
|
|
doc.createdAt = new Date();
|
|
if (!doc.userId) {
|
|
doc.userId = userId;
|
|
}
|
|
});
|
|
|
|
// #6813: on the server the board is ALWAYS the card's, not what the client
|
|
// sent - a text note on its own card could otherwise carry another board's id
|
|
// (published to that board, and mistaken for being on it).
|
|
if (Meteor.isServer) {
|
|
CardTextNotes.before.insert(async (userId, doc) => {
|
|
if (!doc.cardId) return;
|
|
const card = await ReactiveCache.getCard(doc.cardId);
|
|
if (card && card.boardId) doc.boardId = card.boardId;
|
|
});
|
|
}
|
|
|
|
if (Meteor.isServer) {
|
|
const { ensureIndex } = require('/server/lib/mongoStartup');
|
|
Meteor.startup(async () => {
|
|
await ensureIndex(CardTextNotes, { cardId: 1, createdAt: 1 });
|
|
await ensureIndex(CardTextNotes, { boardId: 1 });
|
|
});
|
|
}
|
|
|
|
export default CardTextNotes;
|