Updated campagin cover photos
This commit is contained in:
@@ -2,6 +2,45 @@ const nocoDB = require('../services/nocodb');
|
||||
const emailService = require('../services/email');
|
||||
const representAPI = require('../services/represent-api');
|
||||
const { generateSlug, validateSlug } = require('../utils/validators');
|
||||
const multer = require('multer');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
// Configure multer for file uploads
|
||||
const storage = multer.diskStorage({
|
||||
destination: function (req, file, cb) {
|
||||
const uploadDir = path.join(__dirname, '../public/uploads');
|
||||
// Ensure the upload directory exists
|
||||
if (!fs.existsSync(uploadDir)) {
|
||||
fs.mkdirSync(uploadDir, { recursive: true });
|
||||
}
|
||||
cb(null, uploadDir);
|
||||
},
|
||||
filename: function (req, file, cb) {
|
||||
// Generate unique filename with timestamp
|
||||
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
|
||||
cb(null, 'cover-' + uniqueSuffix + path.extname(file.originalname));
|
||||
}
|
||||
});
|
||||
|
||||
const upload = multer({
|
||||
storage: storage,
|
||||
limits: {
|
||||
fileSize: 5 * 1024 * 1024 // 5MB limit
|
||||
},
|
||||
fileFilter: function (req, file, cb) {
|
||||
// Accept only image files
|
||||
const allowedTypes = /jpeg|jpg|png|gif|webp/;
|
||||
const extname = allowedTypes.test(path.extname(file.originalname).toLowerCase());
|
||||
const mimetype = allowedTypes.test(file.mimetype);
|
||||
|
||||
if (mimetype && extname) {
|
||||
return cb(null, true);
|
||||
} else {
|
||||
cb(new Error('Only image files are allowed (jpeg, jpg, png, gif, webp)'));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const VALID_CAMPAIGN_STATUSES = ['draft', 'active', 'paused', 'archived'];
|
||||
|
||||
@@ -76,6 +115,7 @@ class CampaignsController {
|
||||
email_subject: campaign['Email Subject'] || campaign.email_subject,
|
||||
email_body: campaign['Email Body'] || campaign.email_body,
|
||||
call_to_action: campaign['Call to Action'] || campaign.call_to_action,
|
||||
cover_photo: campaign['Cover Photo'] || campaign.cover_photo,
|
||||
status: normalizeStatus(campaign['Status'] || campaign.status),
|
||||
allow_smtp_email: campaign['Allow SMTP Email'] || campaign.allow_smtp_email,
|
||||
allow_mailto_link: campaign['Allow Mailto Link'] || campaign.allow_mailto_link,
|
||||
@@ -157,6 +197,7 @@ class CampaignsController {
|
||||
email_subject: campaign['Email Subject'] || campaign.email_subject,
|
||||
email_body: campaign['Email Body'] || campaign.email_body,
|
||||
call_to_action: campaign['Call to Action'] || campaign.call_to_action,
|
||||
cover_photo: campaign['Cover Photo'] || campaign.cover_photo,
|
||||
status: normalizeStatus(campaign['Status'] || campaign.status),
|
||||
allow_smtp_email: campaign['Allow SMTP Email'] || campaign.allow_smtp_email,
|
||||
allow_mailto_link: campaign['Allow Mailto Link'] || campaign.allow_mailto_link,
|
||||
@@ -214,6 +255,10 @@ class CampaignsController {
|
||||
}
|
||||
}
|
||||
|
||||
// Debug cover photo value
|
||||
const coverPhoto = campaign['Cover Photo'] || campaign.cover_photo;
|
||||
console.log('Raw cover_photo from NocoDB:', coverPhoto, 'Type:', typeof coverPhoto);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
campaign: {
|
||||
@@ -222,6 +267,7 @@ class CampaignsController {
|
||||
title: campaign['Campaign Title'] || campaign.title,
|
||||
description: campaign['Description'] || campaign.description,
|
||||
call_to_action: campaign['Call to Action'] || campaign.call_to_action,
|
||||
cover_photo: coverPhoto || null,
|
||||
email_subject: campaign['Email Subject'] || campaign.email_subject,
|
||||
email_body: campaign['Email Body'] || campaign.email_body,
|
||||
allow_smtp_email: campaign['Allow SMTP Email'] || campaign.allow_smtp_email,
|
||||
@@ -294,7 +340,9 @@ class CampaignsController {
|
||||
// Add user ownership data
|
||||
created_by_user_id: ownerUserId,
|
||||
created_by_user_email: ownerEmail,
|
||||
created_by_user_name: ownerName
|
||||
created_by_user_name: ownerName,
|
||||
// Add cover photo if uploaded
|
||||
cover_photo: req.file ? req.file.filename : null
|
||||
};
|
||||
|
||||
const campaign = await nocoDB.createCampaign(campaignData);
|
||||
@@ -310,6 +358,7 @@ class CampaignsController {
|
||||
email_subject: campaign['Email Subject'] || campaign.email_subject,
|
||||
email_body: campaign['Email Body'] || campaign.email_body,
|
||||
call_to_action: campaign['Call to Action'] || campaign.call_to_action,
|
||||
cover_photo: campaign['Cover Photo'] || campaign.cover_photo,
|
||||
status: normalizeStatus(campaign['Status'] || campaign.status),
|
||||
allow_smtp_email: campaign['Allow SMTP Email'] || campaign.allow_smtp_email,
|
||||
allow_mailto_link: campaign['Allow Mailto Link'] || campaign.allow_mailto_link,
|
||||
@@ -390,6 +439,16 @@ class CampaignsController {
|
||||
updates.target_government_levels = normalizeTargetLevels(updates.target_government_levels);
|
||||
}
|
||||
|
||||
// Handle cover photo upload
|
||||
if (req.file) {
|
||||
console.log('Cover photo file received:', req.file.filename);
|
||||
updates.cover_photo = req.file.filename;
|
||||
} else {
|
||||
console.log('No cover photo file in request');
|
||||
}
|
||||
|
||||
console.log('Updates object before saving:', updates);
|
||||
|
||||
if (updates.status !== undefined) {
|
||||
const sanitizedStatus = normalizeStatus(updates.status, null);
|
||||
if (!sanitizedStatus) {
|
||||
@@ -423,6 +482,7 @@ class CampaignsController {
|
||||
email_subject: campaign['Email Subject'] || campaign.email_subject,
|
||||
email_body: campaign['Email Body'] || campaign.email_body,
|
||||
call_to_action: campaign['Call to Action'] || campaign.call_to_action,
|
||||
cover_photo: campaign['Cover Photo'] || campaign.cover_photo,
|
||||
status: normalizeStatus(campaign['Status'] || campaign.status),
|
||||
allow_smtp_email: campaign['Allow SMTP Email'] || campaign.allow_smtp_email,
|
||||
allow_mailto_link: campaign['Allow Mailto Link'] || campaign.allow_mailto_link,
|
||||
@@ -834,4 +894,8 @@ class CampaignsController {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new CampaignsController();
|
||||
// Export controller instance and upload middleware
|
||||
const controller = new CampaignsController();
|
||||
controller.upload = upload;
|
||||
|
||||
module.exports = controller;
|
||||
Reference in New Issue
Block a user