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 | 2x 2x 6x 6x 6x 6x 6x 6x 5x 1x 2x 7x 7x 7x 7x 6x 3x 3x 2x 2x 2x 6x 6x 6x 6x 2x 2x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 3x 3x 3x 3x 2x 1x 2x 2x 5x 5x 5x 5x 4x 4x 1x 3x 3x 1x 2x 2x 1x 2x 2x 4x 4x 4x 4x 1x 2x 1x 2x | import { Hono } from "hono";
import type { Dal } from "../../../dal";
import { error, handleError, success } from "../../../lib/response";
import { parseRequiredId, requireUser } from "../../../lib/utils";
import type { Services } from "../../../services";
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: the campaign, its template, the recipient count,
// and whether a launched campaign can be stopped (it cannot — see
// CAMPAIGN_STOPPABLE's comment in services/whatsapp/campaign.service.ts).
//
// Additive: `campaign` is still the first key, so the previous
// `?viewId=` round-trip through the create wizard keeps working while the
// portal moves to its own detail route.
campaigns.get("/:id", async (c) => {
try {
const services = c.get("services");
const id = parseRequiredId(c.req.param("id"), "campaign");
return success(c, await services.waCampaign.getDetail(id));
} 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);
}
});
// No POST /:id/recipients/filter.
//
// It declared `{source, city, requirementType, dateFrom, dateTo}` and read
// two of them: `requirementType`, `dateFrom` and `dateTo` were accepted and
// silently dropped, so a narrowed audience quietly got the unnarrowed one. Any
// `source` other than the literal "pros" returned HTTP 200 with
// `{added: 0}` — a success response for a no-op. It also queried pros with no
// status filter while the sibling contacts endpoint pinned
// `status: "published"`, so two "same" audience queries disagreed about
// whether unpublished pros were reachable. It had no caller in the product.
//
// Audiences come from `POST /:id/recipients/manual` and
// `POST /:id/recipients/upload`. A filtered audience is a real feature; when
// it is built, every parameter it accepts must be one it honours.
// 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);
}
});
export default campaigns;
|