Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions src/commands/blasts.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,10 @@ import { loadConfig, getValidToken, wrapPayload } from '../lib/auth.js';
import { apiRequest } from '../lib/http.js';
import { jsonOutput, jsonError } from '../lib/output.js';
import { PartifulError, ValidationError } from '../lib/errors.js';
import readline from 'readline';
import { confirm } from '../lib/events.js';

const VALID_TO_VALUES = ['GOING', 'MAYBE', 'DECLINED', 'SENT', 'INTERESTED', 'WAITLIST', 'APPROVED', 'RESPONDED_TO_FIND_A_TIME'];
const MAX_MESSAGE_LENGTH = 480;
const MAX_BLASTS_PER_EVENT = 10;

async function confirm(question) {
const rl = readline.createInterface({ input: process.stdin, output: process.stderr });
return new Promise(resolve => {
rl.question(question + ' [y/N]: ', answer => {
rl.close();
resolve(answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes');
});
});
}

export function registerBlastsCommands(program) {
const blasts = program.command('blasts').description('Text blasts to event guests');
Expand Down
100 changes: 26 additions & 74 deletions src/commands/bulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,29 @@

import fs from 'fs';
import { loadConfig, getValidToken, wrapPayload } from '../lib/auth.js';

function makePayload(config, params) {
return {
data: wrapPayload(config, {
params,
amplitudeSessionId: Date.now(),
userId: config.userId,
}),
};
}
import { parseDateTime } from '../lib/dates.js';
import { jsonOutput, jsonError } from '../lib/output.js';
import { apiRequest, firestoreRequest } from '../lib/http.js';
import { PartifulError } from '../lib/errors.js';
import { buildBaseEvent } from '../lib/events.js';

function handleError(e) {
if (e instanceof PartifulError) {
jsonError(e.message, e.exitCode, e.type, e.details);
} else {
jsonError(e instanceof Error ? e.message : String(e));
}
}

const DEFAULT_DELAY = 1000; // ms between API calls

Expand Down Expand Up @@ -38,7 +58,7 @@ function parseCsv(text) {
}

/**
* Normalize a row from JSON/CSV into the shape events create expects.
* Normalize a row from JSON/CSV into the shape buildBaseEvent expects.
*/
function normalizeRow(row) {
return {
Expand All @@ -58,72 +78,6 @@ function normalizeRow(row) {
};
}

/**
* Build event payload (shared with events create — duplicated here to avoid circular imports).
*/
function buildEventPayload(opts) {
const startDate = parseDateTime(opts.date, opts.timezone);
const endDate = opts.endDate ? parseDateTime(opts.endDate, opts.timezone) : null;

const event = {
title: opts.title,
startDate: startDate.toISOString(),
timezone: opts.timezone || 'America/Los_Angeles',
displaySettings: {
theme: opts.theme || 'oxblood',
effect: opts.effect || 'sunbeams',
titleFont: 'display',
},
showHostList: true,
showGuestCount: true,
showGuestList: true,
showActivityTimestamps: true,
displayInviteButton: true,
visibility: opts.private ? 'private' : 'public',
allowGuestPhotoUpload: true,
enableGuestReminders: true,
rsvpsEnabled: true,
allowGuestsToInviteMutuals: true,
rsvpButtonGlyphType: 'emojis',
status: 'UNSAVED',
guestStatusCounts: {
READY_TO_SEND: 0, SENDING: 0, SENT: 0, SEND_ERROR: 0,
DELIVERY_ERROR: 0, INTERESTED: 0, MAYBE: 0, GOING: 0,
DECLINED: 0, WAITLIST: 0, PENDING_APPROVAL: 0, APPROVED: 0,
WITHDRAWN: 0, RESPONDED_TO_FIND_A_TIME: 0,
WAITLISTED_FOR_APPROVAL: 0, REJECTED: 0,
},
};

if (endDate) event.endDate = endDate.toISOString();
if (opts.location) event.location = opts.location;
if (opts.address) event.address = opts.address;
if (opts.description) event.description = opts.description;
if (opts.capacity) { event.guestLimit = opts.capacity; event.enableWaitlist = true; }

return event;
}

/**
* Generate a series of dates: weekly, biweekly, monthly from a start date.
*/
function generateSeries(startDateStr, repeat, count, timezone) {
const dates = [];
const start = parseDateTime(startDateStr, timezone);
for (let i = 0; i < count; i++) {
const d = new Date(start);
switch (repeat) {
case 'daily': d.setDate(d.getDate() + i); break;
case 'weekly': d.setDate(d.getDate() + (i * 7)); break;
case 'biweekly': d.setDate(d.getDate() + (i * 14)); break;
case 'monthly': d.setMonth(d.getMonth() + i); break;
default: throw new Error(`Unknown repeat interval: ${repeat}. Use: daily, weekly, biweekly, monthly`);
}
dates.push(d.toISOString());
}
return dates;
}

export function registerBulkCommands(program) {
const bulk = program.command('bulk').description('Bulk create or update events');

Expand Down Expand Up @@ -167,7 +121,7 @@ export function registerBulkCommands(program) {
});

if (globalOpts.dryRun) {
jsonOutput(normalized.map(n => buildEventPayload(n)), {
jsonOutput(normalized.map(n => buildBaseEvent(n).event), {
total: normalized.length,
action: 'dry_run',
hint: 'Remove --dry-run to create these events',
Expand All @@ -180,8 +134,8 @@ export function registerBulkCommands(program) {
const results = [];

for (let i = 0; i < normalized.length; i++) {
const event = buildEventPayload(normalized[i]);
const payload = { data: wrapPayload(config, { event }) };
const { event } = buildBaseEvent(normalized[i]);
const payload = makePayload(config, { event, cohostIds: [] });

try {
const resp = await apiRequest('POST', '/createEvent', token, payload, globalOpts.verbose);
Expand All @@ -201,7 +155,7 @@ export function registerBulkCommands(program) {
errors: results.filter(r => r.status === 'error').length,
}, globalOpts);
} catch (e) {
jsonError(e.message);
handleError(e);
}
});

Expand Down Expand Up @@ -279,7 +233,6 @@ export function registerBulkCommands(program) {
for (let i = 0; i < matched.length; i++) {
const e = matched[i];
try {
// Build Firestore update
const fields = {};
const updateFields = [];
for (const [key, val] of Object.entries(updates)) {
Expand Down Expand Up @@ -308,8 +261,7 @@ export function registerBulkCommands(program) {
errors: results.filter(r => r.status === 'error').length,
}, globalOpts);
} catch (e) {
if (e instanceof (await import('../lib/errors.js')).PartifulError) jsonError(e.message, e.exitCode, e.type, e.details);
else jsonError(e.message);
handleError(e);
}
});
}
Loading