File size: 12,449 Bytes
c83190f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env node
/**
 * Peppermint seeder — runs INSIDE the container via docker exec.
 * Uses Node 18 built-in fetch; no external dependencies needed.
 *
 * Usage (from seed-local.sh):
 *   docker cp seed/seed_peppermint.js <container>:/tmp/seed_peppermint.js
 *   docker cp seed_data.json           <container>:/tmp/seed_data.json
 *   docker exec <container> node /tmp/seed_peppermint.js
 */

const fs = require("fs");

const BASE_URL = process.env.PEPPERMINT_URL || "http://localhost:5003";
const ADMIN_EMAIL = process.env.ADMIN_EMAIL || "admin@admin.com";
const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD || "1234";
const DATA_FILE = process.env.SEED_DATA_FILE || "/tmp/seed_data.json";
const EXPANSION_FILES = (process.env.SEED_EXPANSION_FILES || "")
  .split(":")
  .map(value => value.trim())
  .filter(Boolean);

const baseData = JSON.parse(fs.readFileSync(DATA_FILE, "utf8"));
const data = {
  ...baseData,
  clients: [...(baseData.clients ?? [])],
  users: [...(baseData.users ?? [])],
  tickets: [...(baseData.tickets ?? [])],
};

for (const expansionFile of EXPANSION_FILES) {
  const expansion = JSON.parse(fs.readFileSync(expansionFile, "utf8"));
  const clients = expansion.clients ?? [];
  const users = expansion.users ?? [];
  const tickets = expansion.tickets ?? [];
  data.clients.push(...clients);
  data.users.push(...users);
  data.tickets.push(...tickets);
  console.log(
    `Loaded ${clients.length} additive clients, ${users.length} additive users, ` +
    `and ${tickets.length} additive tickets ` +
    `from ${expansionFile}`,
  );
}

// ── Helpers ───────────────────────────────────────────────────────────────────

async function api(method, path, token, body) {
  const headers = { "Content-Type": "application/json" };
  if (token) headers["Authorization"] = `Bearer ${token}`;
  const res = await fetch(`${BASE_URL}${path}`, {
    method,
    headers,
    body: body ? JSON.stringify(body) : undefined,
  });
  const text = await res.text();
  let json;
  try { json = JSON.parse(text); } catch { json = {}; }
  return { ok: res.ok, status: res.status, json, text };
}

async function login(email, password) {
  const r = await api("POST", "/api/v1/auth/login", null, { email, password });
  if (r.ok) {
    let tokenUserId = null;
    try {
      const payloadB64 = String(r.json.token || "").split(".")[1];
      if (payloadB64) {
        const payload = JSON.parse(Buffer.from(payloadB64, "base64url").toString("utf8"));
        tokenUserId = payload?.data?.id ?? null;
      }
    } catch {}

    return {
      token: r.json.token,
      user: r.json.user ?? null,
      userId: r.json.user?.id ?? tokenUserId,
    };
  }
  console.error(`  LOGIN FAILED ${r.status}: ${r.text.slice(0, 120)}`);
  return null;
}

async function waitForApi(maxWait = 120) {
  console.log(`Waiting for API at ${BASE_URL} ...`);
  const deadline = Date.now() + maxWait * 1000;
  while (Date.now() < deadline) {
    try {
      const r = await fetch(`${BASE_URL}/`, { signal: AbortSignal.timeout(5000) });
      if (r.status < 500) { console.log("  API is up."); return true; }
    } catch {}
    process.stdout.write(".");
    await new Promise(r => setTimeout(r, 3000));
  }
  console.error(`\nERROR: API not ready after ${maxWait}s`);
  return false;
}

function richText(text) {
  return [{
    type: "text",
    text,
    styles: {},
  }];
}

function ticketDetailBlocks(value) {
  const text = String(value ?? "").replace(/\r\n?/g, "\n");
  const blocks = [];

  for (const rawLine of text.split("\n")) {
    const line = rawLine.trim();
    if (!line) continue;

    const numbered = line.match(/^\d+\.\s+(.+)$/);
    const bulleted = line.match(/^[-*]\s+(.+)$/);
    const heading = line.length <= 120 && /^[^:]+:$/.test(line);

    if (heading) {
      blocks.push({
        type: "heading",
        props: {
          textColor: "default",
          backgroundColor: "default",
          textAlignment: "left",
          level: 2,
        },
        content: richText(line.slice(0, -1)),
        children: [],
      });
    } else if (numbered || bulleted) {
      blocks.push({
        type: numbered ? "numberedListItem" : "bulletListItem",
        props: {
          textColor: "default",
          backgroundColor: "default",
          textAlignment: "left",
        },
        content: richText((numbered || bulleted)[1]),
        children: [],
      });
    } else {
      blocks.push({
        type: "paragraph",
        props: {
          textColor: "default",
          backgroundColor: "default",
          textAlignment: "left",
        },
        content: richText(line),
        children: [],
      });
    }
  }

  return blocks.length > 0 ? blocks : [{
    type: "paragraph",
    props: {
      textColor: "default",
      backgroundColor: "default",
      textAlignment: "left",
    },
    content: [],
    children: [],
  }];
}

// ── Main ──────────────────────────────────────────────────────────────────────

async function main() {
  if (!await waitForApi()) process.exit(1);

  // Extra pause for Prisma to finish seeding
  await new Promise(r => setTimeout(r, 5000));

  // ── Login as admin ──────────────────────────────────────────────────────
  console.log(`\nLogging in as ${ADMIN_EMAIL}...`);
  let adminLogin = await login(ADMIN_EMAIL, ADMIN_PASSWORD);
  if (!adminLogin) {
    await new Promise(r => setTimeout(r, 10000));
    adminLogin = await login(ADMIN_EMAIL, ADMIN_PASSWORD);
  }
  if (!adminLogin) { console.error("Failed to login as admin. Exiting."); process.exit(1); }
  const adminToken = adminLogin.token;
  console.log("  OK");

  const adminId = adminLogin.userId ?? null;

  const userTokens = { [ADMIN_EMAIL]: adminToken };
  const userIds    = { [ADMIN_EMAIL]: adminId };
  const userNames  = { [ADMIN_EMAIL]: adminLogin.user?.name ?? "Admin" };
  const userProfiles = {
    [ADMIN_EMAIL]: {
      id: adminId,
      name: adminLogin.user?.name ?? "Admin",
      email: ADMIN_EMAIL,
      role: "admin",
    },
  };

  // ── Create users ────────────────────────────────────────────────────────
  console.log(`\nCreating ${data.users.length} users...`);
  for (const u of data.users) {
    const r = await api("POST", "/api/v1/auth/user/register", adminToken, {
      name: u.name, email: u.email, password: u.password, admin: u.admin ?? false,
    });
    if (r.ok) {
      const uid = r.json.id ?? r.json.user?.id;
      userIds[u.email] = uid;
      userNames[u.email] = u.name;
      console.log(`  Created: ${u.email} (id=${uid})`);
    } else {
      console.log(`  Skipped (may exist): ${u.email} — ${r.status}`);
    }
    const userLogin = await login(u.email, u.password);
    if (!userLogin) throw new Error(`Unable to log in seeded user ${u.email}`);
    userTokens[u.email] = userLogin.token;
    if (userLogin.userId) userIds[u.email] = userLogin.userId;
    if (userLogin.user?.name) userNames[u.email] = userLogin.user.name;
    userProfiles[u.email] = {
      id: userLogin.userId,
      name: userLogin.user?.name ?? u.name,
      email: u.email,
      role: userLogin.user?.isAdmin ? "admin" : "user",
    };
  }

  // ── Create clients ──────────────────────────────────────────────────────
  console.log(`\nCreating ${data.clients.length} clients...`);
  for (const client of data.clients) {
    const r = await api("POST", "/api/v1/client/create", adminToken, {
      name: client.name,
      email: client.email,
      number: client.number,
      contactName: client.contactName,
    });
    if (r.ok) {
      console.log(`  Created client: ${client.name}`);
    } else {
      console.log(`  Skipped client (may exist): ${client.email} — ${r.status}`);
    }
  }

  const clientResult = await api("GET", "/api/v1/clients/all", adminToken);
  if (!clientResult.ok) {
    throw new Error(`Unable to load seeded clients: ${clientResult.status}`);
  }
  const clientRows = Array.isArray(clientResult.json)
    ? clientResult.json
    : clientResult.json.clients ?? clientResult.json.data ?? [];
  const clientIds = Object.fromEntries(
    clientRows.filter(client => client.email).map(client => [client.email, client.id]),
  );
  for (const client of data.clients) {
    if (!clientIds[client.email]) {
      throw new Error(`Unable to resolve seeded client ${client.email}`);
    }
  }

  // ── Create tickets ──────────────────────────────────────────────────────
  console.log(`\nCreating ${data.tickets.length} tickets...`);
  for (const t of data.tickets) {
    const assignedEmail = t.assignedTo;
    if (assignedEmail && !userIds[assignedEmail]) {
      throw new Error(`Unknown assigned user ${assignedEmail} for ticket '${t.title}'`);
    }
    const engineer =
      assignedEmail && userIds[assignedEmail]
        ? { id: userIds[assignedEmail], name: userNames[assignedEmail] ?? assignedEmail }
        : undefined;
    const creatorEmail = t.createdByEmail;
    if (creatorEmail && !userProfiles[creatorEmail]?.id) {
      throw new Error(`Unknown creator ${creatorEmail} for ticket '${t.title}'`);
    }
    const createdBy = creatorEmail ? userProfiles[creatorEmail] : undefined;
    const clientEmail = t.clientEmail;
    if (clientEmail && !clientIds[clientEmail]) {
      throw new Error(`Unknown client ${clientEmail} for ticket '${t.title}'`);
    }

    const body = {
      title:    t.title,
      name:     t.title,
      // The API JSON-stringifies this field. Imported plain text therefore
      // needs to be submitted as native BlockNote blocks; submitting a string
      // makes escaped newlines appear literally in the ticket editor.
      detail:   t.source ? ticketDetailBlocks(t.detail) : t.detail,
      priority: t.priority ?? "low",
      type:     t.type ?? "support",
      ...(engineer ? { engineer } : {}),
      ...(createdBy ? { createdBy } : {}),
      ...(clientEmail ? { company: clientIds[clientEmail] } : {}),
    };

    const r = await api("POST", "/api/v1/ticket/create", adminToken, body);
    if (!r.ok) {
      console.log(`  FAILED '${t.title.slice(0, 45)}': ${r.status} ${r.text.slice(0, 80)}`);
      continue;
    }
    const ticketId = r.json.id;
    console.log(`  Ticket: '${t.title.slice(0, 45)}' (id=${ticketId})`);

    // Update status
    const status = t.status ?? "needs_support";
    if (status !== "needs_support") {
      const sr = await api("PUT", "/api/v1/ticket/update", adminToken, { id: ticketId, status });
      if (!sr.ok) {
        await api("PUT", "/api/v1/ticket/status/update", adminToken, { id: ticketId, status });
      }
    }

    // Add comments
    for (const c of t.comments ?? []) {
      const commenterEmail = c.userEmail ?? ADMIN_EMAIL;
      const tok = userTokens[commenterEmail];
      if (!tok) {
        throw new Error(`Unknown commenter ${commenterEmail} for ticket '${t.title}'`);
      }
      const cr = await api("POST", "/api/v1/ticket/comment", tok, {
        id: ticketId, text: c.text, public: c.public ?? false,
      });
      if (!cr.ok) console.log(`    Comment failed: ${cr.status} ${cr.text.slice(0, 80)}`);
    }

    // Peppermint stores workflow status and open/closed state independently.
    // Existing seed tickets intentionally omit isComplete and retain their
    // original behavior. Additive imports may explicitly close resolved items.
    if (t.isComplete === true) {
      const closeResult = await api(
        "PUT",
        "/api/v1/ticket/status/update",
        adminToken,
        { id: ticketId, status: true },
      );
      if (!closeResult.ok) {
        console.log(`    Close failed: ${closeResult.status} ${closeResult.text.slice(0, 80)}`);
      }
    }
  }

  console.log("\nSeeding complete!");
}

main().catch(e => { console.error(e); process.exit(1); });