, * date?: ?string, * scheduled_at?: ?string, * platforms?: array}>, * label_ids?: array * } $data */ public static function execute(Workspace $workspace, User $user, array $data): Post { $scheduledAt = self::resolveScheduledAt($data); $post = DB::transaction(function () use ($workspace, $user, $data, $scheduledAt): Post { $post = $workspace->posts()->create([ 'user_id' => $user->id, 'content' => data_get($data, 'content', ''), 'media' => data_get($data, 'media', []), 'status' => PostStatus::Draft, 'scheduled_at' => $scheduledAt, ]); SyncPostPlatforms::execute($post); foreach (data_get($data, 'platforms', []) as $platformData) { $accountId = data_get($platformData, 'social_account_id'); if (! $accountId) { continue; } $updates = ['enabled' => true]; if ($contentType = data_get($platformData, 'content_type')) { $updates['content_type'] = $contentType; } $meta = data_get($platformData, 'meta'); if (is_array($meta) && $meta !== []) { $existing = $post->postPlatforms() ->where('social_account_id', $accountId) ->first(); if ($existing) { $updates['meta'] = array_merge($existing->meta ?? [], $meta); } } $post->postPlatforms() ->where('social_account_id', $accountId) ->update($updates); } if ($labelIds = data_get($data, 'label_ids')) { $post->labels()->sync($labelIds); } return $post; }); PostCreated::dispatch($post); return $post; } /** * @param array $data */ private static function resolveScheduledAt(array $data): Carbon { if ($scheduledAt = data_get($data, 'scheduled_at')) { return Carbon::parse($scheduledAt)->utc(); } $date = data_get($data, 'date') ?: Carbon::now('UTC')->format('Y-m-d'); return Carbon::parse($date, 'UTC')->setTime(9, 0)->utc(); } }