Files
wekan/server/lib/shopEmail.js
T
Lauri Ojansivu c7b48bce2f Shop: an order is also a Budget and billing invoice, the buyer gets a confirmation email, and a board copy closes its shop.
Budget and billing invoice. When Budget and billing is on (Admin Panel /
Settings / Features) for the admin whose Board Settings / Shop takes the
orders, and they are still an admin of the shop's board, shopPlaceOrder also
writes the order into billing's own invoices of that board
(server/lib/shopInvoice.js): billing's numbering INV-<year>-<number>, the
board's Invoicer profile and payment terms, status Sent, the order number as
its reference, the buyer as its customer, one fixed-price line per product
naming its card, and the ORDER's payment reference, so the card, the invoice
and the payment all name the same one. The order card says which invoice it
is, and the checkout shows its number. An order that fails after the invoice
was made removes it again. With Budget and billing off, no invoice.

Confirmation email. After the order is stored the buyer gets its number,
lines, totals with VAT, the payment method with its reference and account
lines, an https payment link and the shop's name, in the language of their
browser (server/lib/shopEmail.js, models/lib/shop.js orderEmail). It goes
through Meteor's Email, so Admin Panel / E-mail / Bounces takes out a stopped
address and the SMTP limits hold; it is not sent without MAIL_URL, when the
new Board Settings / Shop row "Send the buyer an order confirmation by email"
is unticked, or to a stopped address. The subject is one line; the HTML part
is the account email layout of the plain text, which escapes every
character. A failed send is recorded in Admin Panel / Problems / Email and
never fails the order. No QR image: the mail code has no inline images.

A board copy. Board.copy() - the copyBoard method, the REST copy route and
the board templates - resets shopSettings (copiedShopSettings): the copy's
shop is closed, and its Orders list, the price, product code and stock
fields and the lists on sale, which are the source board's, are cleared, so
a copy never takes orders into the source's Orders list. Board Settings /
Shop on the copy suggests the copy's own fields and Orders list again. A
WeKan import, and the clone that runs through it, already left it out.

The order card's labels are now in the owner's language: the server loads
the language before translating.

Thanks to xet7 !
2026-11-22 20:41:15 +02:00

94 lines
3.6 KiB
JavaScript

// Board View / Shop: the buyer's order confirmation e-mail
// (docs/Features/Board-Header/Board-Views/Shop.md).
//
// Sent once the order is placed, through Meteor's Email - so every hook WeKan
// has on sending applies: an address stopped by Admin Panel / E-mail /
// Bounces is taken out (server/emailBounces.js), the SMTP limits and
// deadlines of server/lib/mailTransport.js hold. Not sent when this WeKan
// sends no e-mail (no MAIL_URL, from the environment or Admin Panel /
// E-mail), when Board Settings / Shop turns it off, or to a stopped address.
//
// It is the buyer's receipt, not the order: a failure is recorded in Admin
// Panel / Problems (server/lib/mailProblems.js) and never fails the order.
// The text is plain (models/lib/shop.js orderEmail); the HTML part is the
// account e-mail layout of that same text, which escapes every character of
// it (models/lib/accountEmailHtml.js).
import { Meteor } from 'meteor/meteor';
import { TAPi18n } from '/imports/i18n';
const { orderEmail } = require('/models/lib/shop');
const { renderAccountEmailHtml } = require('/models/lib/accountEmailHtml');
let testSender = null;
// Tests only: capture or fail the send instead of reaching a mail server.
export function setOrderEmailSenderForTests(fn) {
testSender = typeof fn === 'function' ? fn : null;
}
async function send(mail) {
if (testSender) return testSender(mail);
const { Email } = require('meteor/email');
return Email.sendAsync(mail);
}
function mailConfigured() {
return !!testSender || !!process.env.MAIL_URL;
}
function from() {
try {
return require('meteor/accounts-base').Accounts.emailTemplates.from;
} catch (e) {
return undefined;
}
}
async function translatorFor(language) {
let lang = 'en';
try {
const key = language && TAPi18n.resolveTag ? TAPi18n.resolveTag(language) : null;
if (key) lang = key;
await TAPi18n.ensureLanguageLoaded(lang);
} catch (e) { /* English, then */ }
return { language: lang, t: (key, options) => {
try { return TAPi18n.__(key, options || {}, lang); } catch (e) { return key; }
} };
}
/**
* @returns {Promise<'sent'|'skipped'|'failed'>} never throws
*/
export async function sendOrderConfirmation({ settings, shopTitle: title, to, language, order, payment, method, money }) {
try {
if (!settings || settings.confirmationEmail === false || !to || !mailConfigured()) return 'skipped';
try {
if (require('/server/emailBounces').isAddressStopped(to)) return 'skipped';
} catch (e) { /* no bounce list: send */ }
const { t, language: lang } = await translatorFor(language);
const methodLabel = (method && method.method && method.method.label) || t(`billing-method-${method && method.method && method.method.type}`);
const shopTitle = settings.title || title || '';
const { subject, text } = orderEmail({ order, payment, methodLabel, shopTitle, t, money: minor => money(minor, lang) });
await send({
to,
from: from(),
subject,
text,
html: renderAccountEmailHtml({ text, siteName: shopTitle }),
});
return 'sent';
} catch (error) {
try {
require('/server/lib/mailProblems').recordMailProblem('send-failed', {
source: 'shop-order-confirmation',
detail: `${(error && (error.responseCode || error.code)) || ''} ${(error && (error.response || error.message)) || error}`.trim(),
});
} catch (e) { /* logging must never break the order */ }
if (Meteor.isDevelopment) {
// eslint-disable-next-line no-console
console.warn('[shop] order confirmation not sent:', error && error.message);
}
return 'failed';
}
}