-
Notifications
You must be signed in to change notification settings - Fork 73
Fix: [Enhancement] Scheduled publishing queue for reels (Auto-Generated) #450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ndyugwu
wants to merge
8
commits into
Deen-Bridge:main
Choose a base branch
from
ndyugwu:driptide/issue-267-1787925544836
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
75966a0
Fix issue #267: update lib/services/creator-notifications.js
ndyugwu 2c71501
Fix issue #267: update lib/services/scheduled-reels.js
ndyugwu 06d63f2
Fix issue #267: update app/[locale]/admin/page.jsx
ndyugwu 219eb9d
Fix issue #267: update __tests__/admin/scheduled-reels.service.test.js
ndyugwu b929cae
Rebase driptide/issue-267-1787925544836 onto main
ndyugwu 7d136e8
Fix PR #450: update lib/actions/scheduled-reels.js
ndyugwu 7c61f59
Fix PR #450: update app/[locale]/admin/page.jsx
ndyugwu 5487ecb
Fix PR #450: update __tests__/admin/scheduled-reels.service.test.js
ndyugwu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const { notifyCreator } = vi.hoisted(() => ({ | ||
| notifyCreator: vi.fn().mockResolvedValue({ | ||
| success: true, | ||
| notificationId: "stub_notification", | ||
| deliveredBy: "stub", | ||
| }), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/services/creator-notifications", () => ({ | ||
| notifyCreatorOfScheduledReelCancellation: notifyCreator, | ||
| })); | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetModules(); | ||
| notifyCreator.mockClear(); | ||
| }); | ||
|
|
||
| describe("scheduled reels service", () => { | ||
| it("returns upcoming scheduled reels sorted by go-live time", async () => { | ||
| const { listUpcomingScheduledReels } = await import( | ||
| "@/lib/services/scheduled-reels" | ||
| ); | ||
|
|
||
| const reels = await listUpcomingScheduledReels(); | ||
| const timestamps = reels.map((reel) => Date.parse(reel.scheduledFor)); | ||
|
|
||
| expect(reels.length).toBeGreaterThan(0); | ||
| expect(timestamps).toEqual([...timestamps].sort((a, b) => a - b)); | ||
| expect(reels.every((reel) => reel.status === "scheduled")).toBe(true); | ||
| expect(reels.every((reel) => Date.parse(reel.scheduledFor) > Date.now())).toBe(true); | ||
| }); | ||
|
|
||
| it("cancels an upcoming reel and notifies its creator", async () => { | ||
| const { cancelScheduledReel, listUpcomingScheduledReels } = await import( | ||
| "@/lib/services/scheduled-reels" | ||
| ); | ||
| const [reel] = await listUpcomingScheduledReels(); | ||
|
|
||
| const result = await cancelScheduledReel(reel.id, { | ||
| cancelledBy: "admin_test", | ||
| reason: "Editorial review", | ||
| }); | ||
|
|
||
| expect(result.reel.status).toBe("cancelled"); | ||
| expect(result.reel.cancelledBy).toBe("admin_test"); | ||
| expect(result.reel.cancellationReason).toBe("Editorial review"); | ||
| expect(notifyCreator).toHaveBeenCalledOnce(); | ||
| expect(notifyCreator).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| creatorId: reel.creator.id, | ||
| reelId: reel.id, | ||
| cancelledBy: "admin_test", | ||
| reason: "Editorial review", | ||
| }) | ||
| ); | ||
|
|
||
| const remaining = await listUpcomingScheduledReels(); | ||
| expect(remaining.some((item) => item.id === reel.id)).toBe(false); | ||
| }); | ||
|
|
||
| it("rejects cancellation when the scheduled reel does not exist", async () => { | ||
| const { cancelScheduledReel } = await import( | ||
| "@/lib/services/scheduled-reels" | ||
| ); | ||
|
|
||
| await expect( | ||
| cancelScheduledReel("missing_reel", { cancelledBy: "admin_test" }) | ||
| ).rejects.toThrow("Scheduled reel was not found."); | ||
| expect(notifyCreator).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("exports the expected scheduling fields for platform integration", async () => { | ||
| const { SCHEDULED_REEL_FIELDS } = await import( | ||
| "@/lib/services/scheduled-reels" | ||
| ); | ||
|
|
||
| expect(SCHEDULED_REEL_FIELDS).toEqual( | ||
| expect.objectContaining({ | ||
| id: expect.any(String), | ||
| creator: expect.any(String), | ||
| scheduledFor: expect.any(String), | ||
| timezone: expect.any(String), | ||
| status: expect.any(String), | ||
| cancelledAt: expect.any(String), | ||
| cancelledBy: expect.any(String), | ||
| cancellationReason: expect.any(String), | ||
| }) | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,271 @@ | ||
| "use client"; | ||
|
|
||
| import { useCallback, useEffect, useState } from "react"; | ||
| import { CalendarClock, Clock3, Film, Loader2, RefreshCw, XCircle } from "lucide-react"; | ||
| import { format } from "date-fns"; | ||
| import { toast } from "sonner"; | ||
| import { PageShell } from "@/components/ui/page-shell"; | ||
| import { PageHeader } from "@/components/ui/page-header"; | ||
| import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { Badge } from "@/components/ui/badge"; | ||
| import { Avatar, AvatarFallback } from "@/components/ui/avatar"; | ||
| import { Label } from "@/components/ui/label"; | ||
| import { Textarea } from "@/components/ui/textarea"; | ||
| import { | ||
| Dialog, | ||
| DialogContent, | ||
| DialogDescription, | ||
| DialogFooter, | ||
| DialogHeader, | ||
| DialogTitle, | ||
| } from "@/components/ui/dialog"; | ||
| import { | ||
| Table, | ||
| TableBody, | ||
| TableCell, | ||
| TableHead, | ||
| TableHeader, | ||
| TableRow, | ||
| } from "@/components/ui/table"; | ||
| import { EmptyState } from "@/components/ui/empty-state"; | ||
| import { | ||
| cancelScheduledReel, | ||
| listUpcomingScheduledReels, | ||
| } from "@/lib/services/scheduled-reels"; | ||
|
|
||
| function creatorInitials(name) { | ||
| return name | ||
| .split(" ") | ||
| .filter(Boolean) | ||
| .slice(0, 2) | ||
| .map((part) => part[0]) | ||
| .join("") | ||
| .toUpperCase(); | ||
| } | ||
|
|
||
| function formatGoLive(timestamp) { | ||
| const date = new Date(timestamp); | ||
| return Number.isNaN(date.getTime()) | ||
| ? "Invalid schedule" | ||
| : format(date, "MMM d, yyyy 'at' h:mm a"); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| export default function ScheduledReelsQueuePage() { | ||
| const [reels, setReels] = useState([]); | ||
| const [loading, setLoading] = useState(true); | ||
| const [loadError, setLoadError] = useState(""); | ||
| const [selectedReel, setSelectedReel] = useState(null); | ||
| const [reason, setReason] = useState(""); | ||
| const [cancelling, setCancelling] = useState(false); | ||
|
|
||
| const loadQueue = useCallback(async () => { | ||
| setLoading(true); | ||
| setLoadError(""); | ||
|
|
||
| try { | ||
| const upcomingReels = await listUpcomingScheduledReels(); | ||
| setReels(upcomingReels); | ||
| } catch { | ||
| setReels([]); | ||
| setLoadError("The scheduled reels queue could not be loaded."); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| loadQueue(); | ||
| }, [loadQueue]); | ||
|
|
||
| const closeCancelDialog = () => { | ||
| if (cancelling) return; | ||
| setSelectedReel(null); | ||
| setReason(""); | ||
| }; | ||
|
|
||
| const confirmCancellation = async () => { | ||
| if (!selectedReel) return; | ||
|
|
||
| setCancelling(true); | ||
| try { | ||
| await cancelScheduledReel(selectedReel.id, { | ||
| cancelledBy: "current-admin", | ||
| reason, | ||
| }); | ||
| setReels((current) => | ||
| current.filter((reel) => reel.id !== selectedReel.id) | ||
| ); | ||
| setSelectedReel(null); | ||
| setReason(""); | ||
| toast.success("Scheduled reel cancelled and creator notified."); | ||
| } catch (error) { | ||
| toast.error(error instanceof Error ? error.message : "The reel could not be cancelled."); | ||
| } finally { | ||
| setCancelling(false); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <PageShell> | ||
| <PageHeader | ||
| title="Scheduled reels" | ||
| description="Review and manage reels waiting to be published. Upcoming items are ordered by go-live time." | ||
| icon={CalendarClock} | ||
| actions={ | ||
| <Button variant="outline" onClick={loadQueue} disabled={loading}> | ||
| <RefreshCw className={loading ? "animate-spin" : ""} /> | ||
| Refresh | ||
| </Button> | ||
| } | ||
| /> | ||
|
|
||
| <Card> | ||
| <CardHeader className="flex flex-row items-start justify-between gap-4"> | ||
| <div> | ||
| <CardTitle>Upcoming publishing queue</CardTitle> | ||
| <CardDescription> | ||
| {reels.length === 1 | ||
| ? "1 reel is scheduled to publish." | ||
| : `${reels.length} reels are scheduled to publish.`} | ||
| </CardDescription> | ||
| </div> | ||
| <Badge variant="secondary" className="shrink-0"> | ||
| <Clock3 className="mr-1 h-3.5 w-3.5" /> | ||
| Earliest first | ||
| </Badge> | ||
| </CardHeader> | ||
| <CardContent> | ||
| {loading ? ( | ||
| <div className="flex min-h-64 items-center justify-center" role="status"> | ||
| <Loader2 className="h-7 w-7 animate-spin text-accent" /> | ||
| <span className="sr-only">Loading scheduled reels</span> | ||
| </div> | ||
| ) : loadError ? ( | ||
| <EmptyState | ||
| icon={XCircle} | ||
| title="Unable to load the queue" | ||
| description={loadError} | ||
| action={ | ||
| <Button variant="outline" onClick={loadQueue}> | ||
| Try again | ||
| </Button> | ||
| } | ||
| /> | ||
| ) : reels.length === 0 ? ( | ||
| <EmptyState | ||
| icon={Film} | ||
| title="No scheduled reels" | ||
| description="There are no upcoming reels waiting to be published. New scheduled items will appear here automatically." | ||
| /> | ||
| ) : ( | ||
| <div className="overflow-x-auto rounded-lg border"> | ||
| <Table> | ||
| <TableHeader> | ||
| <TableRow> | ||
| <TableHead>Reel</TableHead> | ||
| <TableHead>Creator</TableHead> | ||
| <TableHead>Go-live time</TableHead> | ||
| <TableHead>Status</TableHead> | ||
| <TableHead className="text-right">Actions</TableHead> | ||
| </TableRow> | ||
| </TableHeader> | ||
| <TableBody> | ||
| {reels.map((reel) => ( | ||
| <TableRow key={reel.id}> | ||
| <TableCell className="min-w-72"> | ||
| <div className="flex items-center gap-3"> | ||
| <div className="flex h-12 w-16 shrink-0 items-center justify-center rounded-lg bg-secondary/10"> | ||
| <Film className="h-5 w-5 text-accent" aria-hidden="true" /> | ||
| </div> | ||
| <div> | ||
| <p className="line-clamp-2 max-w-sm font-medium text-ink"> | ||
| {reel.caption} | ||
| </p> | ||
| <p className="mt-1 text-xs text-ink-muted">{reel.id}</p> | ||
| </div> | ||
| </div> | ||
| </TableCell> | ||
| <TableCell> | ||
| <div className="flex min-w-52 items-center gap-3"> | ||
| <Avatar className="h-9 w-9"> | ||
| <AvatarFallback>{creatorInitials(reel.creator.name)}</AvatarFallback> | ||
| </Avatar> | ||
| <div> | ||
| <p className="font-medium text-ink">{reel.creator.name}</p> | ||
| <p className="text-xs text-ink-muted">{reel.creator.email}</p> | ||
| </div> | ||
| </div> | ||
| </TableCell> | ||
| <TableCell className="min-w-56"> | ||
| <p className="font-medium text-ink">{formatGoLive(reel.scheduledFor)}</p> | ||
| <p className="mt-1 text-xs text-ink-muted">{reel.timezone}</p> | ||
| </TableCell> | ||
| <TableCell> | ||
| <Badge variant="outline">Scheduled</Badge> | ||
| </TableCell> | ||
| <TableCell className="text-right"> | ||
| <Button | ||
| variant="destructive" | ||
| size="sm" | ||
| onClick={() => setSelectedReel(reel)} | ||
| aria-label={`Cancel scheduled reel by ${reel.creator.name}`} | ||
| > | ||
| <XCircle /> | ||
| Cancel | ||
| </Button> | ||
| </TableCell> | ||
| </TableRow> | ||
| ))} | ||
| </TableBody> | ||
| </Table> | ||
| </div> | ||
| )} | ||
| </CardContent> | ||
| </Card> | ||
|
|
||
| <Dialog open={Boolean(selectedReel)} onOpenChange={(open) => !open && closeCancelDialog()}> | ||
| <DialogContent> | ||
| <DialogHeader> | ||
| <DialogTitle>Cancel scheduled reel?</DialogTitle> | ||
| <DialogDescription> | ||
| This removes the reel from the publishing queue. The creator will be notified about the cancellation. | ||
| </DialogDescription> | ||
| </DialogHeader> | ||
|
|
||
| {selectedReel && ( | ||
| <div className="space-y-4"> | ||
| <div className="rounded-lg border bg-muted/30 p-4"> | ||
| <p className="font-medium text-ink">{selectedReel.caption}</p> | ||
| <p className="mt-2 text-sm text-ink-muted"> | ||
| By {selectedReel.creator.name} · {formatGoLive(selectedReel.scheduledFor)} | ||
| </p> | ||
| </div> | ||
| <div className="space-y-2"> | ||
| <Label htmlFor="cancellation-reason">Reason for cancellation (optional)</Label> | ||
| <Textarea | ||
| id="cancellation-reason" | ||
| value={reason} | ||
| onChange={(event) => setReason(event.target.value)} | ||
| placeholder="Add context for the creator" | ||
| maxLength={500} | ||
| disabled={cancelling} | ||
| /> | ||
| </div> | ||
| </div> | ||
| )} | ||
|
|
||
| <DialogFooter> | ||
| <Button variant="outline" onClick={closeCancelDialog} disabled={cancelling}> | ||
| Keep scheduled | ||
| </Button> | ||
| <Button variant="destructive" onClick={confirmCancellation} disabled={cancelling}> | ||
| {cancelling ? <Loader2 className="animate-spin" /> : <XCircle />} | ||
| {cancelling ? "Cancelling…" : "Cancel reel"} | ||
| </Button> | ||
| </DialogFooter> | ||
| </DialogContent> | ||
| </Dialog> | ||
| </PageShell> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /** | ||
| * Stub notification adapter for creator-facing publishing notifications. | ||
| * Replace this implementation with the platform notification API when it is | ||
| * available without changing callers of this service. | ||
| */ | ||
|
|
||
| /** | ||
| * Notify a creator that an administrator cancelled one of their scheduled reels. | ||
| * | ||
| * @param {Object} notification | ||
| * @param {string} notification.creatorId | ||
| * @param {string} notification.reelId | ||
| * @param {string} notification.reelCaption | ||
| * @param {string} notification.scheduledFor | ||
| * @param {string} notification.cancelledBy | ||
| * @param {string} [notification.reason] | ||
| * @returns {Promise<{success: boolean, notificationId: string, deliveredBy: string}>} | ||
| */ | ||
| export async function notifyCreatorOfScheduledReelCancellation(notification) { | ||
| if (!notification?.creatorId || !notification?.reelId) { | ||
| throw new Error("Creator and reel identifiers are required for notification."); | ||
| } | ||
|
|
||
| return { | ||
| success: true, | ||
| notificationId: `stub_reel_cancelled_${notification.reelId}`, | ||
| deliveredBy: "stub", | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.