refactor: standardize post platform data structure and simplify display logic in PostResource

This commit is contained in:
Paulo Castellano 2026-05-03 22:11:58 -03:00
parent 595510812c
commit dc75e2b381
5 changed files with 20 additions and 14 deletions

View file

@ -12,6 +12,7 @@
use App\Enums\Post\Status as PostStatus;
use App\Enums\SocialAccount\Platform;
use App\Http\Requests\App\Post\UpdatePostRequest;
use App\Http\Resources\Api\PostResource;
use App\Http\Resources\App\PlatformConfigResource;
use App\Http\Resources\App\SocialAccountResource;
use App\Models\Post;
@ -228,7 +229,7 @@ public function show(Request $request, Post $post): Response|RedirectResponse
return Inertia::render('posts/Show', [
'workspace' => $workspace,
'post' => $post,
'post' => (new PostResource($post))->resolve(),
]);
}

View file

@ -20,6 +20,14 @@ public function toArray(Request $request): array
'content_type' => $this->content_type?->value,
'status' => $this->status?->value,
'enabled' => $this->enabled,
'platform_url' => $this->platform_url,
'published_at' => $this->published_at?->format('Y-m-d H:i:s'),
'error_message' => $this->error_message,
// Display fields fall back to snapshots (platform_name/username/avatar)
// so deleted accounts still render correctly in the post history.
'display_name' => $this->display_name,
'display_username' => $this->display_username,
'display_avatar' => $this->display_avatar,
'social_account' => new SocialAccountResource($this->whenLoaded('socialAccount')),
];
}

View file

@ -96,7 +96,7 @@ public function scopeDraft(Builder $query): Builder
public function scopePublished(Builder $query): Builder
{
return $query->where('status', PostStatus::Published);
return $query->whereIn('status', [PostStatus::Published, PostStatus::PartiallyPublished]);
}
public function scopeFailed(Builder $query): Builder

View file

@ -33,9 +33,9 @@ interface SocialAccount {
interface PostPlatform {
id: string;
platform: string;
platform_name: string | null;
platform_username: string | null;
platform_avatar: string | null;
display_name: string;
display_username: string | null;
display_avatar: string | null;
content_type: string | null;
status: 'pending' | 'publishing' | 'published' | 'failed';
platform_url: string | null;
@ -52,7 +52,7 @@ interface Post {
status: string;
scheduled_at: string | null;
published_at: string | null;
post_platforms: PostPlatform[];
platforms: PostPlatform[];
labels?: { id: string; name: string; color: string }[];
}
@ -66,7 +66,7 @@ const props = defineProps<{
post: Post;
}>();
const enabledPlatforms = computed(() => props.post.post_platforms.filter((pp) => pp.enabled));
const enabledPlatforms = computed(() => props.post.platforms.filter((pp) => pp.enabled));
const publishedCount = computed(() => enabledPlatforms.value.filter((pp) => pp.status === 'published').length);
const failedCount = computed(() => enabledPlatforms.value.filter((pp) => pp.status === 'failed').length);
@ -79,14 +79,11 @@ const pageTitle = computed(() => {
return snippet ? `${trans('posts.show.title')} · ${snippet}${props.post.content.length > 60 ? '…' : ''}` : trans('posts.show.title');
});
const getDisplayName = (pp: PostPlatform): string =>
pp.social_account?.display_name ?? pp.platform_name ?? pp.platform;
const getDisplayName = (pp: PostPlatform): string => pp.display_name ?? pp.platform;
const getDisplayUsername = (pp: PostPlatform): string | null =>
pp.social_account?.username ?? pp.platform_username;
const getDisplayUsername = (pp: PostPlatform): string | null => pp.display_username;
const getDisplayAvatar = (pp: PostPlatform): string | null =>
pp.social_account?.avatar_url ?? pp.platform_avatar;
const getDisplayAvatar = (pp: PostPlatform): string | null => pp.display_avatar;
const formatDateTime = (date: string | null): string =>
date ? dayjs.utc(date).local().format('D MMM YYYY, HH:mm') : '';

View file

@ -653,7 +653,7 @@
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->component('posts/Show', false)
->has('post.post_platforms', 1)
->has('post.platforms', 1)
);
});