Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 6x 6x 6x 6x 6x 6x 5x 1x 1x 7x 7x 7x 7x 6x 3x 3x 2x 2x 1x 3x 3x 3x 3x 1x 2x 1x 4x 4x 4x 4x 4x 4x 4x 2x 2x 1x 3x 3x 3x 3x 2x 1x 2x 1x 5x 5x 5x 5x 4x 4x 1x 3x 3x 1x 2x 2x 1x 2x 1x 5x 5x 5x 5x 5x 4x 3x 2x 1x 1x 1x 2x 1x 4x 4x 4x 4x 1x 2x 1x 2x 4x 4x 3x 2x 3x 1x 2x 1x | import { Hono } from "hono";
import type { Dal } from "../../../dal";
import type { Services } from "../../../services";
import { success, error, handleError } from "../../../lib/response";
import { requireUser, parseRequiredId } from "../../../lib/utils";
type Env = {
Bindings: CloudflareBindings;
Variables: {
user: { id: string; name: string; email: string } | null;
session: unknown;
dal: Dal;
services: Services;
};
};
const campaigns = new Hono<Env>();
// GET / - List campaigns
campaigns.get("/", async (c) => {
try {
const services = c.get("services");
const limit = Math.min(100, Math.max(1, Number(c.req.query("limit") || 50)));
const offset = Math.max(0, Number(c.req.query("offset") || 0));
const status = c.req.query("status");
const { campaigns: items, total } = await services.waCampaign.list(
{ status },
offset,
limit,
);
return success(c, { campaigns: items, total, limit, offset });
} catch (err) {
return handleError(c, err);
}
});
// POST / - Create campaign
campaigns.post("/", async (c) => {
try {
const user = requireUser(c.get("user"));
const services = c.get("services");
const body = await c.req.json<{
name: string;
templateId: number;
templateParams?: string;
}>();
if (!body.name?.trim() || !body.templateId) {
return error(c, "VALIDATION_ERROR", "name and templateId are required", 400);
}
const campaign = await services.waCampaign.create(
body.name,
body.templateId,
user.id,
body.templateParams,
);
return success(c, { campaign }, 201);
} catch (err) {
return handleError(c, err);
}
});
// GET /:id - Campaign detail
campaigns.get("/:id", async (c) => {
try {
const services = c.get("services");
const id = parseRequiredId(c.req.param("id"), "campaign");
const campaign = await services.waCampaign.getById(id);
return success(c, { campaign });
} catch (err) {
return handleError(c, err);
}
});
// GET /:id/recipients - Paginated recipients
campaigns.get("/:id/recipients", async (c) => {
try {
const services = c.get("services");
const id = parseRequiredId(c.req.param("id"), "campaign");
const limit = Math.min(100, Math.max(1, Number(c.req.query("limit") || 50)));
const offset = Math.max(0, Number(c.req.query("offset") || 0));
const status = c.req.query("status");
const recipients = await services.waCampaign.getRecipients(
id,
{ status },
offset,
limit,
);
return success(c, { recipients });
} catch (err) {
return handleError(c, err);
}
});
// POST /:id/recipients/manual - Add recipients manually
campaigns.post("/:id/recipients/manual", async (c) => {
try {
const services = c.get("services");
const id = parseRequiredId(c.req.param("id"), "campaign");
const body = await c.req.json<{
recipients: Array<{
phone: string;
name?: string;
params?: Record<string, string>;
}>;
}>();
const added = await services.waCampaign.addRecipientsManual(
id,
body.recipients,
);
return success(c, { added });
} catch (err) {
return handleError(c, err);
}
});
// POST /:id/recipients/upload - CSV upload
campaigns.post("/:id/recipients/upload", async (c) => {
try {
const services = c.get("services");
const id = parseRequiredId(c.req.param("id"), "campaign");
const formData = await c.req.formData();
const file = formData.get("file") as File;
if (!file) {
return error(c, "VALIDATION_ERROR", "File is required", 400);
}
const MAX_CSV_SIZE = 10 * 1024 * 1024; // 10 MB
if (file.size > MAX_CSV_SIZE) {
return error(c, "VALIDATION_ERROR", "File too large (max 10 MB)", 400);
}
const csvContent = await file.text();
const result = await services.waCampaign.addRecipientsFromCSV(
id,
csvContent,
);
return success(c, result);
} catch (err) {
return handleError(c, err);
}
});
// POST /:id/recipients/filter - Add from DB filter
campaigns.post("/:id/recipients/filter", async (c) => {
try {
const services = c.get("services");
const dal = c.get("dal");
const id = parseRequiredId(c.req.param("id"), "campaign");
const body = await c.req.json<{
source: string;
city?: string;
requirementType?: string;
dateFrom?: string;
dateTo?: string;
}>();
// Query contacts from DB based on filters
const { contacts, truncated } = await queryContacts(dal, body);
if (contacts.length === 0) {
return success(c, { added: 0, truncated: false });
}
const added = await services.waCampaign.addRecipientsManual(
id,
contacts.map((ct) => ({ phone: ct.phone, name: ct.name })),
);
return success(c, { added, truncated });
} catch (err) {
return handleError(c, err);
}
});
// POST /:id/send - Launch campaign
campaigns.post("/:id/send", async (c) => {
try {
const services = c.get("services");
const id = parseRequiredId(c.req.param("id"), "campaign");
if (!c.env.WHATSAPP_QUEUE) {
return c.json(
{ success: false, error: "WhatsApp queue not configured" },
500,
);
}
await services.waCampaign.launch(id, c.env.WHATSAPP_QUEUE);
return success(c, { message: "Campaign launched" });
} catch (err) {
return handleError(c, err);
}
});
// Helper: query contacts from inquiries/pros
async function queryContacts(
dal: Dal,
filters: {
source: string;
city?: string;
requirementType?: string;
dateFrom?: string;
dateTo?: string;
},
): Promise<{ contacts: Array<{ phone: string; name: string }>; truncated: boolean }> {
const QUERY_LIMIT = 1000;
// For now, use a simple approach querying pros with WhatsApp numbers
if (filters.source === "pros") {
const pros = await dal.pros.findAll(
{ cityId: filters.city },
0,
QUERY_LIMIT,
);
const contacts = pros
.filter((v) => v.whatsapp)
.map((v) => ({
phone: v.whatsapp as string,
name: v.businessName,
}));
return { contacts, truncated: pros.length >= QUERY_LIMIT };
}
// Default: return empty (inquiry-based filtering can be added later)
return { contacts: [], truncated: false };
}
export default campaigns;
|