added time working

This commit is contained in:
2026-08-23 17:13:30 +03:30
parent 657c10d1cc
commit d3997cfc8f
5 changed files with 213 additions and 56 deletions
@@ -108,17 +108,35 @@ class AutomationController extends Controller
$smsHour = $request->input('sms_send_hour');
$inAppHour = $request->input('in_app_send_hour');
if (in_array(AutomationCatalog::CHANNEL_SMS, $channels, true) && $smsHour === null) {
throw new UnprocessableEntityHttpException('ساعت ارسال پیامک را مشخص کنید.');
}
if (in_array(AutomationCatalog::CHANNEL_IN_APP, $channels, true) && $inAppHour === null) {
throw new UnprocessableEntityHttpException('ساعت ارسال اعلان را مشخص کنید.');
$triggerConfigRaw = (array) $request->input('trigger_config', []);
$priceWindow = (string) ($triggerConfigRaw['price_window'] ?? 'calendar_day');
$needsChannelHour = ! (
(
$triggerType === AutomationCatalog::TRIGGER_EVENT
&& (int) ($triggerConfigRaw['delay_hours'] ?? 0) === 0
)
|| (
$triggerType === AutomationCatalog::TRIGGER_PRICE
&& $priceWindow === 'calendar_day'
)
);
if ($needsChannelHour) {
if (in_array(AutomationCatalog::CHANNEL_SMS, $channels, true) && $smsHour === null) {
throw new UnprocessableEntityHttpException('ساعت ارسال پیامک را مشخص کنید.');
}
if (in_array(AutomationCatalog::CHANNEL_IN_APP, $channels, true) && $inAppHour === null) {
throw new UnprocessableEntityHttpException('ساعت ارسال اعلان را مشخص کنید.');
}
}
$conditions = array_values(array_filter(
(array) $request->input('conditions', []),
fn ($row) => is_array($row) && filled($row['field'] ?? null)
));
$audienceMode = (string) $request->input('audience_mode', $conditions === [] ? 'all' : 'filter');
if ($audienceMode === 'all') {
$conditions = [];
}
$catalog = app(AutomationCatalog::class);
$allowed = $catalog->fieldKeys();
foreach ($conditions as $row) {
@@ -128,8 +146,12 @@ class AutomationController extends Controller
}
$triggerConfig = (array) $request->input('trigger_config', []);
$cooldownHours = max(0, (int) $request->input('cooldown_hours', 24));
if ($triggerType === AutomationCatalog::TRIGGER_SCHEDULE) {
$triggerConfig = [];
$everyDays = max(1, (int) ($triggerConfig['every_days'] ?? 1));
$triggerConfig = ['every_days' => $everyDays];
// هر مشتری حداکثر یک‌بار در همین بازهٔ تکرار
$cooldownHours = max($cooldownHours, $everyDays * 24);
} elseif ($triggerType === AutomationCatalog::TRIGGER_EVENT) {
$eventKeys = array_column(AutomationCatalog::EVENTS, 'key');
if (! in_array((string) ($triggerConfig['event'] ?? ''), $eventKeys, true)) {
@@ -148,11 +170,20 @@ class AutomationController extends Controller
if (! in_array($dir, ['up', 'down', 'both'], true)) {
$dir = 'both';
}
$window = (string) ($triggerConfig['price_window'] ?? 'calendar_day');
if (! in_array($window, ['calendar_day', 'from_last'], true)) {
$window = 'calendar_day';
}
$triggerConfig = [
'step_toman' => $step,
'direction' => $dir,
'product_id' => (int) ($triggerConfig['product_id'] ?? 0) ?: null,
'price_window' => $window,
];
// برای «در یک روز» معمولاً یک‌بار در روز کافی است
if ($window === 'calendar_day') {
$cooldownHours = max($cooldownHours, 20);
}
}
$fingerprint = app(AutomationFingerprint::class)->make($triggerType, $triggerConfig, $conditions);
@@ -172,15 +203,19 @@ class AutomationController extends Controller
'is_active' => (bool) $request->input('is_active', true),
'starts_at' => $starts,
'ends_at' => $ends,
'sms_send_hour' => in_array(AutomationCatalog::CHANNEL_SMS, $channels, true) ? (int) $smsHour : null,
'in_app_send_hour' => in_array(AutomationCatalog::CHANNEL_IN_APP, $channels, true) ? (int) $inAppHour : null,
'sms_send_hour' => in_array(AutomationCatalog::CHANNEL_SMS, $channels, true)
? ($smsHour === null ? null : (int) $smsHour)
: null,
'in_app_send_hour' => in_array(AutomationCatalog::CHANNEL_IN_APP, $channels, true)
? ($inAppHour === null ? null : (int) $inAppHour)
: null,
'trigger_type' => $triggerType,
'trigger_config' => $triggerConfig,
'conditions' => $conditions,
'channels' => $channels,
'title' => mb_substr($title, 0, 191),
'body' => $body,
'cooldown_hours' => max(0, (int) $request->input('cooldown_hours', 24)),
'cooldown_hours' => $cooldownHours,
'max_sends' => $request->filled('max_sends') ? max(1, (int) $request->input('max_sends')) : null,
'logic_fingerprint' => $fingerprint,
];