Files
memind/scripts/push-wechat-news-morning-draft.mjs
john 830f8a4011
Memind CI / Test, build, and release guards (push) Successful in 5m36s
feat(wechat): add service-account push subscriptions and news morning cover delivery
Ship the 10-item numeric subscription loop on WeChat MP, wire schedule delivery
for news/weather/quote/health/finance/tech/knowledge/night/surprise pushes, and
extend news morning draft cover generation plus a one-shot draft push script.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-20 16:15:15 +08:00

74 lines
2.2 KiB
JavaScript

#!/usr/bin/env node
/**
* Trigger a one-shot WeChat news morning draft push on the Portal worker.
* Sets news_morning_draft.forcePushOnce; worker picks it up on the next scan (~30s).
*
* Usage:
* node scripts/push-wechat-news-morning-draft.mjs
* node scripts/push-wechat-news-morning-draft.mjs --apply
* node scripts/push-wechat-news-morning-draft.mjs --apply --allow-yesterday
*/
import process from 'node:process';
import mysql from 'mysql2/promise';
import { loadH5Environment } from './load-env.mjs';
loadH5Environment(import.meta.dirname);
const CONFIG_KEY = 'news_morning_draft';
const CONFIG_TABLE = 'h5_wechat_admin_config';
async function main() {
const apply = process.argv.includes('--apply');
const allowYesterday = process.argv.includes('--allow-yesterday');
if (!process.env.DATABASE_URL) {
console.error('DATABASE_URL missing');
process.exit(1);
}
const pool = mysql.createPool({ uri: process.env.DATABASE_URL, connectionLimit: 2 });
const [rows] = await pool.query(
`SELECT config_json FROM ${CONFIG_TABLE} WHERE config_key = ? LIMIT 1`,
[CONFIG_KEY],
);
const current = rows[0]?.config_json
? (typeof rows[0].config_json === 'string'
? JSON.parse(rows[0].config_json)
: rows[0].config_json)
: {};
const next = {
...current,
enabled: current.enabled !== false,
forcePushOnce: true,
forcePushRequireToday: !allowYesterday,
};
console.log(JSON.stringify({
apply,
allowYesterday,
sourceUserId: current.sourceUserId ?? null,
enabled: next.enabled,
forcePushRequireToday: next.forcePushRequireToday,
}, null, 2));
if (apply) {
const now = Date.now();
await pool.query(
`INSERT INTO ${CONFIG_TABLE} (config_key, config_json, updated_by, updated_at)
VALUES (?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
config_json = VALUES(config_json),
updated_by = VALUES(updated_by),
updated_at = VALUES(updated_at)`,
[CONFIG_KEY, JSON.stringify(next), 'manual-push-draft', now],
);
console.log('forcePushOnce=true persisted; Portal worker should push draft within ~30s');
}
await pool.end();
}
main().catch((error) => {
console.error(error instanceof Error ? error.message : error);
process.exit(1);
});