mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-06-05 14:18:13 +02:00
fix custom stat campaign start date
Signed-off-by: Ronni Skansing <rskansing@gmail.com>
This commit is contained in:
@@ -1783,7 +1783,7 @@ func (r *Campaign) GetCampaignStats(ctx context.Context, campaignID *uuid.UUID)
|
||||
func (r *Campaign) GetAllCampaignStats(ctx context.Context, companyID *uuid.UUID) ([]database.CampaignStats, error) {
|
||||
var stats []database.CampaignStats
|
||||
|
||||
db := r.DB.WithContext(ctx).Order("created_at DESC")
|
||||
db := r.DB.WithContext(ctx).Order("campaign_start_date DESC NULLS LAST, created_at DESC")
|
||||
|
||||
if companyID != nil {
|
||||
db = db.Where("company_id = ?", companyID)
|
||||
@@ -1817,7 +1817,7 @@ func (r *Campaign) DeleteCampaignStats(ctx context.Context, campaignID *uuid.UUI
|
||||
func (r *Campaign) GetManualCampaignStats(ctx context.Context, companyID *uuid.UUID) ([]database.CampaignStats, error) {
|
||||
var stats []database.CampaignStats
|
||||
|
||||
db := r.DB.WithContext(ctx).Where("campaign_id IS NULL").Order("created_at DESC")
|
||||
db := r.DB.WithContext(ctx).Where("campaign_id IS NULL").Order("campaign_start_date DESC NULLS LAST, created_at DESC")
|
||||
|
||||
if companyID != nil {
|
||||
db = db.Where("company_id = ?", companyID)
|
||||
|
||||
@@ -4307,18 +4307,10 @@ func (c *Campaign) CreateManualCampaignStats(ctx context.Context, session *model
|
||||
id := uuid.New()
|
||||
now := time.Now()
|
||||
|
||||
// Use provided date for created_at and updated_at, or current time if not provided
|
||||
var statsDate time.Time
|
||||
if req.CampaignStartDate != nil {
|
||||
statsDate = *req.CampaignStartDate
|
||||
} else {
|
||||
statsDate = now
|
||||
}
|
||||
|
||||
// Set required fields
|
||||
req.ID = &id
|
||||
req.CreatedAt = &statsDate
|
||||
req.UpdatedAt = &statsDate
|
||||
req.CreatedAt = &now
|
||||
req.UpdatedAt = &now
|
||||
req.CampaignID = nil // No campaign reference for manual stats
|
||||
|
||||
// Calculate total events
|
||||
|
||||
@@ -287,20 +287,19 @@
|
||||
// ignore errors
|
||||
}
|
||||
|
||||
// Filtered campaigns based on selected time range (using sendStartAt or createdAt)
|
||||
// Filtered campaigns based on selected time range (using sendStartAt)
|
||||
$: filteredCampaignStats = (() => {
|
||||
if (!campaignStats || campaignStats.length === 0) return [];
|
||||
const range = Number(selectedTimeRange);
|
||||
const now = new Date();
|
||||
const cutoff = new Date(now.getFullYear(), now.getMonth() - range + 1, 1);
|
||||
return campaignStats.filter((c) => {
|
||||
const startDate = c.sendStartAt
|
||||
? new Date(c.sendStartAt)
|
||||
: c.createdAt
|
||||
? new Date(c.createdAt)
|
||||
: c.date instanceof Date
|
||||
? c.date
|
||||
: new Date(c.date);
|
||||
// filter out stats with no dates (data integrity issue)
|
||||
if (!c.campaignStartDate && !c.createdAt) {
|
||||
console.warn('Filtering out campaign stat with missing dates:', c);
|
||||
return false;
|
||||
}
|
||||
const startDate = c.campaignStartDate ? new Date(c.campaignStartDate) : new Date(c.createdAt);
|
||||
return startDate >= cutoff;
|
||||
});
|
||||
})();
|
||||
@@ -335,17 +334,18 @@
|
||||
};
|
||||
})();
|
||||
|
||||
// Update chartData to use filteredCampaignStats, using sendStartAt or createdAt as date, and sort by date ascending
|
||||
// Update chartData to use filteredCampaignStats, using sendStartAt as date, and sort by date ascending
|
||||
$: chartData = filteredCampaignStats
|
||||
.filter((c) => {
|
||||
if (!c.campaignStartDate && !c.createdAt) {
|
||||
console.warn('Skipping campaign stat with missing dates in chart data:', c);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.map((c) => ({
|
||||
...c,
|
||||
date: c.sendStartAt
|
||||
? new Date(c.sendStartAt)
|
||||
: c.createdAt
|
||||
? new Date(c.createdAt)
|
||||
: c.date instanceof Date
|
||||
? c.date
|
||||
: new Date(c.date),
|
||||
date: c.campaignStartDate ? new Date(c.campaignStartDate) : new Date(c.createdAt),
|
||||
name: c.campaignName || c.name || c.title || ''
|
||||
}))
|
||||
.sort((a, b) => a.date.getTime() - b.date.getTime());
|
||||
|
||||
@@ -417,7 +417,7 @@
|
||||
alignText="center"
|
||||
value={`${pct.reported.count} (${pct.reported.absolute}%, rel: ${pct.reported.relative}%)`}
|
||||
/>
|
||||
<TableCell alignText="center" value={stats.createdAt} isDate isRelative />
|
||||
<TableCell alignText="center" value={stats.campaignStartDate} isDate isRelative />
|
||||
<TableCellEmpty />
|
||||
<TableCellAction>
|
||||
<TableDropDownEllipsis>
|
||||
|
||||
Reference in New Issue
Block a user