@hasna/microservices 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.js +70 -0
- package/bin/mcp.js +71 -1
- package/dist/index.js +70 -0
- package/microservices/microservice-ads/package.json +27 -0
- package/microservices/microservice-ads/src/cli/index.ts +407 -0
- package/microservices/microservice-ads/src/db/campaigns.ts +493 -0
- package/microservices/microservice-ads/src/db/database.ts +93 -0
- package/microservices/microservice-ads/src/db/migrations.ts +60 -0
- package/microservices/microservice-ads/src/index.ts +39 -0
- package/microservices/microservice-ads/src/mcp/index.ts +320 -0
- package/microservices/microservice-contracts/package.json +27 -0
- package/microservices/microservice-contracts/src/cli/index.ts +383 -0
- package/microservices/microservice-contracts/src/db/contracts.ts +496 -0
- package/microservices/microservice-contracts/src/db/database.ts +93 -0
- package/microservices/microservice-contracts/src/db/migrations.ts +58 -0
- package/microservices/microservice-contracts/src/index.ts +43 -0
- package/microservices/microservice-contracts/src/mcp/index.ts +308 -0
- package/microservices/microservice-domains/package.json +27 -0
- package/microservices/microservice-domains/src/cli/index.ts +438 -0
- package/microservices/microservice-domains/src/db/database.ts +93 -0
- package/microservices/microservice-domains/src/db/domains.ts +551 -0
- package/microservices/microservice-domains/src/db/migrations.ts +60 -0
- package/microservices/microservice-domains/src/index.ts +44 -0
- package/microservices/microservice-domains/src/mcp/index.ts +368 -0
- package/microservices/microservice-hiring/package.json +27 -0
- package/microservices/microservice-hiring/src/cli/index.ts +431 -0
- package/microservices/microservice-hiring/src/db/database.ts +93 -0
- package/microservices/microservice-hiring/src/db/hiring.ts +582 -0
- package/microservices/microservice-hiring/src/db/migrations.ts +68 -0
- package/microservices/microservice-hiring/src/index.ts +51 -0
- package/microservices/microservice-hiring/src/mcp/index.ts +464 -0
- package/microservices/microservice-payments/package.json +27 -0
- package/microservices/microservice-payments/src/cli/index.ts +357 -0
- package/microservices/microservice-payments/src/db/database.ts +93 -0
- package/microservices/microservice-payments/src/db/migrations.ts +63 -0
- package/microservices/microservice-payments/src/db/payments.ts +652 -0
- package/microservices/microservice-payments/src/index.ts +51 -0
- package/microservices/microservice-payments/src/mcp/index.ts +460 -0
- package/microservices/microservice-payroll/package.json +27 -0
- package/microservices/microservice-payroll/src/cli/index.ts +374 -0
- package/microservices/microservice-payroll/src/db/database.ts +93 -0
- package/microservices/microservice-payroll/src/db/migrations.ts +69 -0
- package/microservices/microservice-payroll/src/db/payroll.ts +741 -0
- package/microservices/microservice-payroll/src/index.ts +48 -0
- package/microservices/microservice-payroll/src/mcp/index.ts +420 -0
- package/microservices/microservice-shipping/package.json +27 -0
- package/microservices/microservice-shipping/src/cli/index.ts +398 -0
- package/microservices/microservice-shipping/src/db/database.ts +93 -0
- package/microservices/microservice-shipping/src/db/migrations.ts +61 -0
- package/microservices/microservice-shipping/src/db/shipping.ts +643 -0
- package/microservices/microservice-shipping/src/index.ts +53 -0
- package/microservices/microservice-shipping/src/mcp/index.ts +385 -0
- package/microservices/microservice-social/package.json +27 -0
- package/microservices/microservice-social/src/cli/index.ts +447 -0
- package/microservices/microservice-social/src/db/database.ts +93 -0
- package/microservices/microservice-social/src/db/migrations.ts +55 -0
- package/microservices/microservice-social/src/db/social.ts +672 -0
- package/microservices/microservice-social/src/index.ts +46 -0
- package/microservices/microservice-social/src/mcp/index.ts +435 -0
- package/microservices/microservice-subscriptions/package.json +27 -0
- package/microservices/microservice-subscriptions/src/cli/index.ts +400 -0
- package/microservices/microservice-subscriptions/src/db/database.ts +93 -0
- package/microservices/microservice-subscriptions/src/db/migrations.ts +57 -0
- package/microservices/microservice-subscriptions/src/db/subscriptions.ts +692 -0
- package/microservices/microservice-subscriptions/src/index.ts +41 -0
- package/microservices/microservice-subscriptions/src/mcp/index.ts +365 -0
- package/microservices/microservice-transcriber/package.json +28 -0
- package/microservices/microservice-transcriber/src/cli/index.ts +1347 -0
- package/microservices/microservice-transcriber/src/db/annotations.ts +37 -0
- package/microservices/microservice-transcriber/src/db/database.ts +82 -0
- package/microservices/microservice-transcriber/src/db/migrations.ts +72 -0
- package/microservices/microservice-transcriber/src/db/transcripts.ts +395 -0
- package/microservices/microservice-transcriber/src/index.ts +43 -0
- package/microservices/microservice-transcriber/src/lib/config.ts +77 -0
- package/microservices/microservice-transcriber/src/lib/diff.ts +91 -0
- package/microservices/microservice-transcriber/src/lib/downloader.ts +570 -0
- package/microservices/microservice-transcriber/src/lib/feeds.ts +62 -0
- package/microservices/microservice-transcriber/src/lib/live.ts +94 -0
- package/microservices/microservice-transcriber/src/lib/notion.ts +129 -0
- package/microservices/microservice-transcriber/src/lib/providers.ts +713 -0
- package/microservices/microservice-transcriber/src/lib/summarizer.ts +147 -0
- package/microservices/microservice-transcriber/src/lib/translator.ts +75 -0
- package/microservices/microservice-transcriber/src/lib/webhook.ts +37 -0
- package/microservices/microservice-transcriber/src/mcp/index.ts +1070 -0
- package/microservices/microservice-transcriber/src/server/index.ts +199 -0
- package/package.json +1 -1
- package/microservices/microservice-invoices/dashboard/dist/assets/index-Bngq7FNM.css +0 -1
- package/microservices/microservice-invoices/dashboard/dist/assets/index-aHW4ARZR.js +0 -124
- package/microservices/microservice-invoices/dashboard/dist/index.html +0 -13
|
@@ -0,0 +1,672 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Social media CRUD operations and analytics
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { getDatabase } from "./database.js";
|
|
6
|
+
|
|
7
|
+
// ---- Types ----
|
|
8
|
+
|
|
9
|
+
export type Platform = "x" | "linkedin" | "instagram" | "threads" | "bluesky";
|
|
10
|
+
export type PostStatus = "draft" | "scheduled" | "published" | "failed";
|
|
11
|
+
|
|
12
|
+
export interface Account {
|
|
13
|
+
id: string;
|
|
14
|
+
platform: Platform;
|
|
15
|
+
handle: string;
|
|
16
|
+
display_name: string | null;
|
|
17
|
+
connected: boolean;
|
|
18
|
+
access_token_env: string | null;
|
|
19
|
+
metadata: Record<string, unknown>;
|
|
20
|
+
created_at: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface AccountRow {
|
|
24
|
+
id: string;
|
|
25
|
+
platform: string;
|
|
26
|
+
handle: string;
|
|
27
|
+
display_name: string | null;
|
|
28
|
+
connected: number;
|
|
29
|
+
access_token_env: string | null;
|
|
30
|
+
metadata: string;
|
|
31
|
+
created_at: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function rowToAccount(row: AccountRow): Account {
|
|
35
|
+
return {
|
|
36
|
+
...row,
|
|
37
|
+
platform: row.platform as Platform,
|
|
38
|
+
connected: row.connected === 1,
|
|
39
|
+
metadata: JSON.parse(row.metadata || "{}"),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface Engagement {
|
|
44
|
+
likes?: number;
|
|
45
|
+
shares?: number;
|
|
46
|
+
comments?: number;
|
|
47
|
+
impressions?: number;
|
|
48
|
+
clicks?: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface Post {
|
|
52
|
+
id: string;
|
|
53
|
+
account_id: string;
|
|
54
|
+
content: string;
|
|
55
|
+
media_urls: string[];
|
|
56
|
+
status: PostStatus;
|
|
57
|
+
scheduled_at: string | null;
|
|
58
|
+
published_at: string | null;
|
|
59
|
+
platform_post_id: string | null;
|
|
60
|
+
engagement: Engagement;
|
|
61
|
+
tags: string[];
|
|
62
|
+
created_at: string;
|
|
63
|
+
updated_at: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface PostRow {
|
|
67
|
+
id: string;
|
|
68
|
+
account_id: string;
|
|
69
|
+
content: string;
|
|
70
|
+
media_urls: string;
|
|
71
|
+
status: string;
|
|
72
|
+
scheduled_at: string | null;
|
|
73
|
+
published_at: string | null;
|
|
74
|
+
platform_post_id: string | null;
|
|
75
|
+
engagement: string;
|
|
76
|
+
tags: string;
|
|
77
|
+
created_at: string;
|
|
78
|
+
updated_at: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function rowToPost(row: PostRow): Post {
|
|
82
|
+
return {
|
|
83
|
+
...row,
|
|
84
|
+
status: row.status as PostStatus,
|
|
85
|
+
media_urls: JSON.parse(row.media_urls || "[]"),
|
|
86
|
+
engagement: JSON.parse(row.engagement || "{}"),
|
|
87
|
+
tags: JSON.parse(row.tags || "[]"),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface Template {
|
|
92
|
+
id: string;
|
|
93
|
+
name: string;
|
|
94
|
+
content: string;
|
|
95
|
+
variables: string[];
|
|
96
|
+
created_at: string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
interface TemplateRow {
|
|
100
|
+
id: string;
|
|
101
|
+
name: string;
|
|
102
|
+
content: string;
|
|
103
|
+
variables: string;
|
|
104
|
+
created_at: string;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function rowToTemplate(row: TemplateRow): Template {
|
|
108
|
+
return {
|
|
109
|
+
...row,
|
|
110
|
+
variables: JSON.parse(row.variables || "[]"),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ---- Accounts ----
|
|
115
|
+
|
|
116
|
+
export interface CreateAccountInput {
|
|
117
|
+
platform: Platform;
|
|
118
|
+
handle: string;
|
|
119
|
+
display_name?: string;
|
|
120
|
+
connected?: boolean;
|
|
121
|
+
access_token_env?: string;
|
|
122
|
+
metadata?: Record<string, unknown>;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function createAccount(input: CreateAccountInput): Account {
|
|
126
|
+
const db = getDatabase();
|
|
127
|
+
const id = crypto.randomUUID();
|
|
128
|
+
const metadata = JSON.stringify(input.metadata || {});
|
|
129
|
+
|
|
130
|
+
db.prepare(
|
|
131
|
+
`INSERT INTO accounts (id, platform, handle, display_name, connected, access_token_env, metadata)
|
|
132
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
133
|
+
).run(
|
|
134
|
+
id,
|
|
135
|
+
input.platform,
|
|
136
|
+
input.handle,
|
|
137
|
+
input.display_name || null,
|
|
138
|
+
input.connected ? 1 : 0,
|
|
139
|
+
input.access_token_env || null,
|
|
140
|
+
metadata
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
return getAccount(id)!;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function getAccount(id: string): Account | null {
|
|
147
|
+
const db = getDatabase();
|
|
148
|
+
const row = db.prepare("SELECT * FROM accounts WHERE id = ?").get(id) as AccountRow | null;
|
|
149
|
+
return row ? rowToAccount(row) : null;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export interface ListAccountsOptions {
|
|
153
|
+
platform?: Platform;
|
|
154
|
+
connected?: boolean;
|
|
155
|
+
limit?: number;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function listAccounts(options: ListAccountsOptions = {}): Account[] {
|
|
159
|
+
const db = getDatabase();
|
|
160
|
+
const conditions: string[] = [];
|
|
161
|
+
const params: unknown[] = [];
|
|
162
|
+
|
|
163
|
+
if (options.platform) {
|
|
164
|
+
conditions.push("platform = ?");
|
|
165
|
+
params.push(options.platform);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (options.connected !== undefined) {
|
|
169
|
+
conditions.push("connected = ?");
|
|
170
|
+
params.push(options.connected ? 1 : 0);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
let sql = "SELECT * FROM accounts";
|
|
174
|
+
if (conditions.length > 0) {
|
|
175
|
+
sql += " WHERE " + conditions.join(" AND ");
|
|
176
|
+
}
|
|
177
|
+
sql += " ORDER BY created_at DESC";
|
|
178
|
+
|
|
179
|
+
if (options.limit) {
|
|
180
|
+
sql += " LIMIT ?";
|
|
181
|
+
params.push(options.limit);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const rows = db.prepare(sql).all(...params) as AccountRow[];
|
|
185
|
+
return rows.map(rowToAccount);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export interface UpdateAccountInput {
|
|
189
|
+
platform?: Platform;
|
|
190
|
+
handle?: string;
|
|
191
|
+
display_name?: string;
|
|
192
|
+
connected?: boolean;
|
|
193
|
+
access_token_env?: string;
|
|
194
|
+
metadata?: Record<string, unknown>;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function updateAccount(id: string, input: UpdateAccountInput): Account | null {
|
|
198
|
+
const db = getDatabase();
|
|
199
|
+
const existing = getAccount(id);
|
|
200
|
+
if (!existing) return null;
|
|
201
|
+
|
|
202
|
+
const sets: string[] = [];
|
|
203
|
+
const params: unknown[] = [];
|
|
204
|
+
|
|
205
|
+
if (input.platform !== undefined) {
|
|
206
|
+
sets.push("platform = ?");
|
|
207
|
+
params.push(input.platform);
|
|
208
|
+
}
|
|
209
|
+
if (input.handle !== undefined) {
|
|
210
|
+
sets.push("handle = ?");
|
|
211
|
+
params.push(input.handle);
|
|
212
|
+
}
|
|
213
|
+
if (input.display_name !== undefined) {
|
|
214
|
+
sets.push("display_name = ?");
|
|
215
|
+
params.push(input.display_name);
|
|
216
|
+
}
|
|
217
|
+
if (input.connected !== undefined) {
|
|
218
|
+
sets.push("connected = ?");
|
|
219
|
+
params.push(input.connected ? 1 : 0);
|
|
220
|
+
}
|
|
221
|
+
if (input.access_token_env !== undefined) {
|
|
222
|
+
sets.push("access_token_env = ?");
|
|
223
|
+
params.push(input.access_token_env);
|
|
224
|
+
}
|
|
225
|
+
if (input.metadata !== undefined) {
|
|
226
|
+
sets.push("metadata = ?");
|
|
227
|
+
params.push(JSON.stringify(input.metadata));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (sets.length === 0) return existing;
|
|
231
|
+
|
|
232
|
+
params.push(id);
|
|
233
|
+
db.prepare(`UPDATE accounts SET ${sets.join(", ")} WHERE id = ?`).run(...params);
|
|
234
|
+
|
|
235
|
+
return getAccount(id);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function deleteAccount(id: string): boolean {
|
|
239
|
+
const db = getDatabase();
|
|
240
|
+
const result = db.prepare("DELETE FROM accounts WHERE id = ?").run(id);
|
|
241
|
+
return result.changes > 0;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function countAccounts(): number {
|
|
245
|
+
const db = getDatabase();
|
|
246
|
+
const row = db.prepare("SELECT COUNT(*) as count FROM accounts").get() as { count: number };
|
|
247
|
+
return row.count;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// ---- Posts ----
|
|
251
|
+
|
|
252
|
+
export interface CreatePostInput {
|
|
253
|
+
account_id: string;
|
|
254
|
+
content: string;
|
|
255
|
+
media_urls?: string[];
|
|
256
|
+
status?: PostStatus;
|
|
257
|
+
scheduled_at?: string;
|
|
258
|
+
tags?: string[];
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function createPost(input: CreatePostInput): Post {
|
|
262
|
+
const db = getDatabase();
|
|
263
|
+
const id = crypto.randomUUID();
|
|
264
|
+
const media_urls = JSON.stringify(input.media_urls || []);
|
|
265
|
+
const tags = JSON.stringify(input.tags || []);
|
|
266
|
+
|
|
267
|
+
db.prepare(
|
|
268
|
+
`INSERT INTO posts (id, account_id, content, media_urls, status, scheduled_at, tags)
|
|
269
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
270
|
+
).run(
|
|
271
|
+
id,
|
|
272
|
+
input.account_id,
|
|
273
|
+
input.content,
|
|
274
|
+
media_urls,
|
|
275
|
+
input.status || "draft",
|
|
276
|
+
input.scheduled_at || null,
|
|
277
|
+
tags
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
return getPost(id)!;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export function getPost(id: string): Post | null {
|
|
284
|
+
const db = getDatabase();
|
|
285
|
+
const row = db.prepare("SELECT * FROM posts WHERE id = ?").get(id) as PostRow | null;
|
|
286
|
+
return row ? rowToPost(row) : null;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export interface ListPostsOptions {
|
|
290
|
+
account_id?: string;
|
|
291
|
+
status?: PostStatus;
|
|
292
|
+
tag?: string;
|
|
293
|
+
search?: string;
|
|
294
|
+
limit?: number;
|
|
295
|
+
offset?: number;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export function listPosts(options: ListPostsOptions = {}): Post[] {
|
|
299
|
+
const db = getDatabase();
|
|
300
|
+
const conditions: string[] = [];
|
|
301
|
+
const params: unknown[] = [];
|
|
302
|
+
|
|
303
|
+
if (options.account_id) {
|
|
304
|
+
conditions.push("account_id = ?");
|
|
305
|
+
params.push(options.account_id);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (options.status) {
|
|
309
|
+
conditions.push("status = ?");
|
|
310
|
+
params.push(options.status);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (options.tag) {
|
|
314
|
+
conditions.push("tags LIKE ?");
|
|
315
|
+
params.push(`%"${options.tag}"%`);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (options.search) {
|
|
319
|
+
conditions.push("content LIKE ?");
|
|
320
|
+
params.push(`%${options.search}%`);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
let sql = "SELECT * FROM posts";
|
|
324
|
+
if (conditions.length > 0) {
|
|
325
|
+
sql += " WHERE " + conditions.join(" AND ");
|
|
326
|
+
}
|
|
327
|
+
sql += " ORDER BY created_at DESC";
|
|
328
|
+
|
|
329
|
+
if (options.limit) {
|
|
330
|
+
sql += " LIMIT ?";
|
|
331
|
+
params.push(options.limit);
|
|
332
|
+
}
|
|
333
|
+
if (options.offset) {
|
|
334
|
+
sql += " OFFSET ?";
|
|
335
|
+
params.push(options.offset);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const rows = db.prepare(sql).all(...params) as PostRow[];
|
|
339
|
+
return rows.map(rowToPost);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
export interface UpdatePostInput {
|
|
343
|
+
content?: string;
|
|
344
|
+
media_urls?: string[];
|
|
345
|
+
status?: PostStatus;
|
|
346
|
+
scheduled_at?: string | null;
|
|
347
|
+
published_at?: string | null;
|
|
348
|
+
platform_post_id?: string;
|
|
349
|
+
engagement?: Engagement;
|
|
350
|
+
tags?: string[];
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export function updatePost(id: string, input: UpdatePostInput): Post | null {
|
|
354
|
+
const db = getDatabase();
|
|
355
|
+
const existing = getPost(id);
|
|
356
|
+
if (!existing) return null;
|
|
357
|
+
|
|
358
|
+
const sets: string[] = [];
|
|
359
|
+
const params: unknown[] = [];
|
|
360
|
+
|
|
361
|
+
if (input.content !== undefined) {
|
|
362
|
+
sets.push("content = ?");
|
|
363
|
+
params.push(input.content);
|
|
364
|
+
}
|
|
365
|
+
if (input.media_urls !== undefined) {
|
|
366
|
+
sets.push("media_urls = ?");
|
|
367
|
+
params.push(JSON.stringify(input.media_urls));
|
|
368
|
+
}
|
|
369
|
+
if (input.status !== undefined) {
|
|
370
|
+
sets.push("status = ?");
|
|
371
|
+
params.push(input.status);
|
|
372
|
+
}
|
|
373
|
+
if (input.scheduled_at !== undefined) {
|
|
374
|
+
sets.push("scheduled_at = ?");
|
|
375
|
+
params.push(input.scheduled_at);
|
|
376
|
+
}
|
|
377
|
+
if (input.published_at !== undefined) {
|
|
378
|
+
sets.push("published_at = ?");
|
|
379
|
+
params.push(input.published_at);
|
|
380
|
+
}
|
|
381
|
+
if (input.platform_post_id !== undefined) {
|
|
382
|
+
sets.push("platform_post_id = ?");
|
|
383
|
+
params.push(input.platform_post_id);
|
|
384
|
+
}
|
|
385
|
+
if (input.engagement !== undefined) {
|
|
386
|
+
sets.push("engagement = ?");
|
|
387
|
+
params.push(JSON.stringify(input.engagement));
|
|
388
|
+
}
|
|
389
|
+
if (input.tags !== undefined) {
|
|
390
|
+
sets.push("tags = ?");
|
|
391
|
+
params.push(JSON.stringify(input.tags));
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (sets.length === 0) return existing;
|
|
395
|
+
|
|
396
|
+
sets.push("updated_at = datetime('now')");
|
|
397
|
+
params.push(id);
|
|
398
|
+
|
|
399
|
+
db.prepare(`UPDATE posts SET ${sets.join(", ")} WHERE id = ?`).run(...params);
|
|
400
|
+
|
|
401
|
+
return getPost(id);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
export function deletePost(id: string): boolean {
|
|
405
|
+
const db = getDatabase();
|
|
406
|
+
const result = db.prepare("DELETE FROM posts WHERE id = ?").run(id);
|
|
407
|
+
return result.changes > 0;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
export function countPosts(): number {
|
|
411
|
+
const db = getDatabase();
|
|
412
|
+
const row = db.prepare("SELECT COUNT(*) as count FROM posts").get() as { count: number };
|
|
413
|
+
return row.count;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Schedule a post — sets status to 'scheduled' and sets scheduled_at
|
|
418
|
+
*/
|
|
419
|
+
export function schedulePost(id: string, scheduledAt: string): Post | null {
|
|
420
|
+
return updatePost(id, { status: "scheduled", scheduled_at: scheduledAt });
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Mark a post as published
|
|
425
|
+
*/
|
|
426
|
+
export function publishPost(id: string, platformPostId?: string): Post | null {
|
|
427
|
+
return updatePost(id, {
|
|
428
|
+
status: "published",
|
|
429
|
+
published_at: new Date().toISOString().replace("T", " ").replace(/\.\d+Z$/, ""),
|
|
430
|
+
platform_post_id: platformPostId,
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// ---- Templates ----
|
|
435
|
+
|
|
436
|
+
export interface CreateTemplateInput {
|
|
437
|
+
name: string;
|
|
438
|
+
content: string;
|
|
439
|
+
variables?: string[];
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
export function createTemplate(input: CreateTemplateInput): Template {
|
|
443
|
+
const db = getDatabase();
|
|
444
|
+
const id = crypto.randomUUID();
|
|
445
|
+
const variables = JSON.stringify(input.variables || []);
|
|
446
|
+
|
|
447
|
+
db.prepare(
|
|
448
|
+
`INSERT INTO templates (id, name, content, variables)
|
|
449
|
+
VALUES (?, ?, ?, ?)`
|
|
450
|
+
).run(id, input.name, input.content, variables);
|
|
451
|
+
|
|
452
|
+
return getTemplate(id)!;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
export function getTemplate(id: string): Template | null {
|
|
456
|
+
const db = getDatabase();
|
|
457
|
+
const row = db.prepare("SELECT * FROM templates WHERE id = ?").get(id) as TemplateRow | null;
|
|
458
|
+
return row ? rowToTemplate(row) : null;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
export function listTemplates(): Template[] {
|
|
462
|
+
const db = getDatabase();
|
|
463
|
+
const rows = db.prepare("SELECT * FROM templates ORDER BY name").all() as TemplateRow[];
|
|
464
|
+
return rows.map(rowToTemplate);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
export function deleteTemplate(id: string): boolean {
|
|
468
|
+
const db = getDatabase();
|
|
469
|
+
const result = db.prepare("DELETE FROM templates WHERE id = ?").run(id);
|
|
470
|
+
return result.changes > 0;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Use a template to create a post — replaces {{variable}} with values
|
|
475
|
+
*/
|
|
476
|
+
export function useTemplate(
|
|
477
|
+
templateId: string,
|
|
478
|
+
accountId: string,
|
|
479
|
+
values: Record<string, string>,
|
|
480
|
+
tags?: string[]
|
|
481
|
+
): Post {
|
|
482
|
+
const template = getTemplate(templateId);
|
|
483
|
+
if (!template) throw new Error(`Template '${templateId}' not found`);
|
|
484
|
+
|
|
485
|
+
let content = template.content;
|
|
486
|
+
for (const [key, value] of Object.entries(values)) {
|
|
487
|
+
content = content.replace(new RegExp(`\\{\\{${key}\\}\\}`, "g"), value);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
return createPost({
|
|
491
|
+
account_id: accountId,
|
|
492
|
+
content,
|
|
493
|
+
tags,
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// ---- Analytics ----
|
|
498
|
+
|
|
499
|
+
export interface EngagementStats {
|
|
500
|
+
total_posts: number;
|
|
501
|
+
total_likes: number;
|
|
502
|
+
total_shares: number;
|
|
503
|
+
total_comments: number;
|
|
504
|
+
total_impressions: number;
|
|
505
|
+
total_clicks: number;
|
|
506
|
+
avg_likes: number;
|
|
507
|
+
avg_shares: number;
|
|
508
|
+
avg_comments: number;
|
|
509
|
+
avg_impressions: number;
|
|
510
|
+
avg_clicks: number;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Get engagement analytics for a specific account or across all accounts
|
|
515
|
+
*/
|
|
516
|
+
export function getEngagementStats(accountId?: string): EngagementStats {
|
|
517
|
+
const db = getDatabase();
|
|
518
|
+
let sql = "SELECT engagement FROM posts WHERE status = 'published'";
|
|
519
|
+
const params: unknown[] = [];
|
|
520
|
+
|
|
521
|
+
if (accountId) {
|
|
522
|
+
sql += " AND account_id = ?";
|
|
523
|
+
params.push(accountId);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
const rows = db.prepare(sql).all(...params) as { engagement: string }[];
|
|
527
|
+
const engagements = rows.map((r) => JSON.parse(r.engagement || "{}") as Engagement);
|
|
528
|
+
|
|
529
|
+
const total_posts = engagements.length;
|
|
530
|
+
const total_likes = engagements.reduce((sum, e) => sum + (e.likes || 0), 0);
|
|
531
|
+
const total_shares = engagements.reduce((sum, e) => sum + (e.shares || 0), 0);
|
|
532
|
+
const total_comments = engagements.reduce((sum, e) => sum + (e.comments || 0), 0);
|
|
533
|
+
const total_impressions = engagements.reduce((sum, e) => sum + (e.impressions || 0), 0);
|
|
534
|
+
const total_clicks = engagements.reduce((sum, e) => sum + (e.clicks || 0), 0);
|
|
535
|
+
|
|
536
|
+
return {
|
|
537
|
+
total_posts,
|
|
538
|
+
total_likes,
|
|
539
|
+
total_shares,
|
|
540
|
+
total_comments,
|
|
541
|
+
total_impressions,
|
|
542
|
+
total_clicks,
|
|
543
|
+
avg_likes: total_posts > 0 ? Math.round(total_likes / total_posts) : 0,
|
|
544
|
+
avg_shares: total_posts > 0 ? Math.round(total_shares / total_posts) : 0,
|
|
545
|
+
avg_comments: total_posts > 0 ? Math.round(total_comments / total_posts) : 0,
|
|
546
|
+
avg_impressions: total_posts > 0 ? Math.round(total_impressions / total_posts) : 0,
|
|
547
|
+
avg_clicks: total_posts > 0 ? Math.round(total_clicks / total_posts) : 0,
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
export interface PlatformStats {
|
|
552
|
+
platform: Platform;
|
|
553
|
+
account_count: number;
|
|
554
|
+
post_count: number;
|
|
555
|
+
engagement: EngagementStats;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* Get analytics grouped by platform
|
|
560
|
+
*/
|
|
561
|
+
export function getStatsByPlatform(): PlatformStats[] {
|
|
562
|
+
const db = getDatabase();
|
|
563
|
+
const accounts = listAccounts();
|
|
564
|
+
const platforms = [...new Set(accounts.map((a) => a.platform))];
|
|
565
|
+
|
|
566
|
+
return platforms.map((platform) => {
|
|
567
|
+
const platformAccounts = accounts.filter((a) => a.platform === platform);
|
|
568
|
+
const accountIds = platformAccounts.map((a) => a.id);
|
|
569
|
+
|
|
570
|
+
let post_count = 0;
|
|
571
|
+
const allEngagements: Engagement[] = [];
|
|
572
|
+
|
|
573
|
+
for (const accountId of accountIds) {
|
|
574
|
+
const posts = listPosts({ account_id: accountId });
|
|
575
|
+
post_count += posts.length;
|
|
576
|
+
const published = posts.filter((p) => p.status === "published");
|
|
577
|
+
allEngagements.push(...published.map((p) => p.engagement));
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
const total_posts = allEngagements.length;
|
|
581
|
+
const total_likes = allEngagements.reduce((sum, e) => sum + (e.likes || 0), 0);
|
|
582
|
+
const total_shares = allEngagements.reduce((sum, e) => sum + (e.shares || 0), 0);
|
|
583
|
+
const total_comments = allEngagements.reduce((sum, e) => sum + (e.comments || 0), 0);
|
|
584
|
+
const total_impressions = allEngagements.reduce((sum, e) => sum + (e.impressions || 0), 0);
|
|
585
|
+
const total_clicks = allEngagements.reduce((sum, e) => sum + (e.clicks || 0), 0);
|
|
586
|
+
|
|
587
|
+
return {
|
|
588
|
+
platform,
|
|
589
|
+
account_count: platformAccounts.length,
|
|
590
|
+
post_count,
|
|
591
|
+
engagement: {
|
|
592
|
+
total_posts,
|
|
593
|
+
total_likes,
|
|
594
|
+
total_shares,
|
|
595
|
+
total_comments,
|
|
596
|
+
total_impressions,
|
|
597
|
+
total_clicks,
|
|
598
|
+
avg_likes: total_posts > 0 ? Math.round(total_likes / total_posts) : 0,
|
|
599
|
+
avg_shares: total_posts > 0 ? Math.round(total_shares / total_posts) : 0,
|
|
600
|
+
avg_comments: total_posts > 0 ? Math.round(total_comments / total_posts) : 0,
|
|
601
|
+
avg_impressions: total_posts > 0 ? Math.round(total_impressions / total_posts) : 0,
|
|
602
|
+
avg_clicks: total_posts > 0 ? Math.round(total_clicks / total_posts) : 0,
|
|
603
|
+
},
|
|
604
|
+
};
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* Get scheduled posts grouped by date (calendar view)
|
|
610
|
+
*/
|
|
611
|
+
export function getCalendar(startDate?: string, endDate?: string): Record<string, Post[]> {
|
|
612
|
+
const db = getDatabase();
|
|
613
|
+
const conditions: string[] = ["status = 'scheduled'", "scheduled_at IS NOT NULL"];
|
|
614
|
+
const params: unknown[] = [];
|
|
615
|
+
|
|
616
|
+
if (startDate) {
|
|
617
|
+
conditions.push("scheduled_at >= ?");
|
|
618
|
+
params.push(startDate);
|
|
619
|
+
}
|
|
620
|
+
if (endDate) {
|
|
621
|
+
conditions.push("scheduled_at <= ?");
|
|
622
|
+
params.push(endDate);
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
const sql = `SELECT * FROM posts WHERE ${conditions.join(" AND ")} ORDER BY scheduled_at`;
|
|
626
|
+
const rows = db.prepare(sql).all(...params) as PostRow[];
|
|
627
|
+
const posts = rows.map(rowToPost);
|
|
628
|
+
|
|
629
|
+
const calendar: Record<string, Post[]> = {};
|
|
630
|
+
for (const post of posts) {
|
|
631
|
+
const date = post.scheduled_at!.split(" ")[0].split("T")[0];
|
|
632
|
+
if (!calendar[date]) calendar[date] = [];
|
|
633
|
+
calendar[date].push(post);
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
return calendar;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
/**
|
|
640
|
+
* Get overall stats summary
|
|
641
|
+
*/
|
|
642
|
+
export function getOverallStats(): {
|
|
643
|
+
total_accounts: number;
|
|
644
|
+
total_posts: number;
|
|
645
|
+
posts_by_status: Record<string, number>;
|
|
646
|
+
total_templates: number;
|
|
647
|
+
engagement: EngagementStats;
|
|
648
|
+
} {
|
|
649
|
+
const db = getDatabase();
|
|
650
|
+
const total_accounts = countAccounts();
|
|
651
|
+
const total_posts = countPosts();
|
|
652
|
+
const total_templates = (db.prepare("SELECT COUNT(*) as count FROM templates").get() as { count: number }).count;
|
|
653
|
+
|
|
654
|
+
// Posts by status
|
|
655
|
+
const statusRows = db
|
|
656
|
+
.prepare("SELECT status, COUNT(*) as count FROM posts GROUP BY status")
|
|
657
|
+
.all() as { status: string; count: number }[];
|
|
658
|
+
const posts_by_status: Record<string, number> = {};
|
|
659
|
+
for (const row of statusRows) {
|
|
660
|
+
posts_by_status[row.status] = row.count;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
const engagement = getEngagementStats();
|
|
664
|
+
|
|
665
|
+
return {
|
|
666
|
+
total_accounts,
|
|
667
|
+
total_posts,
|
|
668
|
+
posts_by_status,
|
|
669
|
+
total_templates,
|
|
670
|
+
engagement,
|
|
671
|
+
};
|
|
672
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* microservice-social — Social media management microservice
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
createAccount,
|
|
7
|
+
getAccount,
|
|
8
|
+
listAccounts,
|
|
9
|
+
updateAccount,
|
|
10
|
+
deleteAccount,
|
|
11
|
+
countAccounts,
|
|
12
|
+
createPost,
|
|
13
|
+
getPost,
|
|
14
|
+
listPosts,
|
|
15
|
+
updatePost,
|
|
16
|
+
deletePost,
|
|
17
|
+
countPosts,
|
|
18
|
+
schedulePost,
|
|
19
|
+
publishPost,
|
|
20
|
+
createTemplate,
|
|
21
|
+
getTemplate,
|
|
22
|
+
listTemplates,
|
|
23
|
+
deleteTemplate,
|
|
24
|
+
useTemplate,
|
|
25
|
+
getEngagementStats,
|
|
26
|
+
getStatsByPlatform,
|
|
27
|
+
getCalendar,
|
|
28
|
+
getOverallStats,
|
|
29
|
+
type Platform,
|
|
30
|
+
type PostStatus,
|
|
31
|
+
type Account,
|
|
32
|
+
type Post,
|
|
33
|
+
type Template,
|
|
34
|
+
type Engagement,
|
|
35
|
+
type EngagementStats,
|
|
36
|
+
type PlatformStats,
|
|
37
|
+
type CreateAccountInput,
|
|
38
|
+
type UpdateAccountInput,
|
|
39
|
+
type ListAccountsOptions,
|
|
40
|
+
type CreatePostInput,
|
|
41
|
+
type UpdatePostInput,
|
|
42
|
+
type ListPostsOptions,
|
|
43
|
+
type CreateTemplateInput,
|
|
44
|
+
} from "./db/social.js";
|
|
45
|
+
|
|
46
|
+
export { getDatabase, closeDatabase } from "./db/database.js";
|