feat(campaign): send welcome campaign on sign up
This commit is contained in:
parent
56ffbec4b2
commit
7788a6fcd9
2 changed files with 23 additions and 1 deletions
|
@ -8,6 +8,8 @@ export async function submitToListmonk(req, res) {
|
||||||
const listmonkUrl = process.env.LISTMONK_URL;
|
const listmonkUrl = process.env.LISTMONK_URL;
|
||||||
const listmonkUsername = process.env.LISTMONK_USERNAME;
|
const listmonkUsername = process.env.LISTMONK_USERNAME;
|
||||||
const listmonkPassword = process.env.LISTMONK_PASSWORD;
|
const listmonkPassword = process.env.LISTMONK_PASSWORD;
|
||||||
|
const campaignId = process.env.PRESET_CAMPAIGN_ID;
|
||||||
|
const listmonkBaseUrl = process.env.LISTMONK_BASE_URL;
|
||||||
|
|
||||||
// Encode username and password as base64
|
// Encode username and password as base64
|
||||||
const auth = Buffer.from(`${listmonkUsername}:${listmonkPassword}`).toString('base64');
|
const auth = Buffer.from(`${listmonkUsername}:${listmonkPassword}`).toString('base64');
|
||||||
|
@ -26,6 +28,7 @@ export async function submitToListmonk(req, res) {
|
||||||
console.log('Sending request to Listmonk with payload:', payload);
|
console.log('Sending request to Listmonk with payload:', payload);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Add subscriber
|
||||||
const response = await fetch(listmonkUrl, {
|
const response = await fetch(listmonkUrl, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
|
@ -40,6 +43,25 @@ export async function submitToListmonk(req, res) {
|
||||||
throw new Error(`Listmonk API error: ${response.statusText}. Response body: ${errorBody}`);
|
throw new Error(`Listmonk API error: ${response.statusText}. Response body: ${errorBody}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Send the welcome campaign
|
||||||
|
const campaignSendUrl = `${listmonkBaseUrl}/campaigns/${campaignId}/send`;
|
||||||
|
console.log('Sending campaign using URL:', campaignSendUrl);
|
||||||
|
const campaignResponse = await fetch(campaignSendUrl, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': `Basic ${auth}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!campaignResponse.ok) {
|
||||||
|
const errorBody = await campaignResponse.text();
|
||||||
|
throw new Error(`Listmonk API error (Send Campaign): ${campaignResponse.statusText}. Response body: ${errorBody}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const campaignResult = await campaignResponse.json();
|
||||||
|
console.log('Campaign sent successfully:', campaignResult);
|
||||||
|
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
res.status(200).json({ message: 'Subscription successful!', result });
|
res.status(200).json({ message: 'Subscription successful!', result });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
@ -9,7 +9,7 @@ dotenv.config();
|
||||||
validateEnvVariables();
|
validateEnvVariables();
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = process.env.PORT || 3002;
|
const PORT = process.env.PORT || 3003;
|
||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue