Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

.migrations.json

# dependencies
/node_modules
/.pnp
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ COPY --from=builder /app/package.json ./
COPY --from=builder /app/src ./src
COPY --from=builder /app/scripts ./scripts
EXPOSE 3000
CMD ["node", "scripts/start.js"]
CMD ["node", "./start.js"]
18 changes: 18 additions & 0 deletions migrations/20251020_init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
async function up(service) {
// Example migration: Add a new field to app metadata
if (service.isPreviousVersionLessThan('0.1.1')) {
console.log('Migrating to 0.1.1: <description>')
}
}

async function down(service) {
// Example rollback: Remove the new field from app metadata
if (service.isPreviousVersionGreaterThan('0.1.0')) {
console.log('Reverting migration to 0.1.0: <description>')
}
}

module.exports = {
up,
down,
}
155 changes: 0 additions & 155 deletions scripts/start.js

This file was deleted.

53 changes: 27 additions & 26 deletions src/app/api/drafts/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,45 @@ import { NextResponse } from 'next/server'

import {
getDraftPosts,
getDraftPost,
createDraftPost,
updateDraftPost,
getDraftPostsInGroup,
getDraftPostsInSchedule,
} from '@/app/api/services/DraftPostService'
import { getSchedules } from '@/app/api/services/SchedulePostService'
import type { CreateDraftInput, DraftPost } from '@/types/drafts'
import Logger from '@/app/api-helpers/logger'
import {
withBskyLogoutForRequest,
withBskyLogoutWithId,
} from '@/app/api-helpers/apiWrapper'
import { Schedule } from '@/types/scheduler'

const logger = new Logger('DraftsRoute')

export async function GET(
request: Request,
{ params }: { params: Promise<{ id?: string }> }
) {
let id = undefined

if (params) {
const resolvedParams = await params
id = resolvedParams.id
}

export const GET = withBskyLogoutForRequest(async (request) => {
const { searchParams } = new URL(request.url)
const group = searchParams.get('group') || undefined
const scheduleId = searchParams.get('schedule') || undefined
const searchTerm = searchParams.get('searchTerm') || undefined

try {
if (id) {
const post = await getDraftPost(id)
return NextResponse.json(post)
}

let posts: DraftPost[] = []
if (group) {
posts = await getDraftPostsInGroup(group)
} else if (scheduleId) {
const schedule = (await getSchedules()).find(
(s) => s.id === scheduleId
) as Schedule

if (!schedule) {
return NextResponse.json(
{ error: 'Schedule not found' },
{ status: 404 }
)
}

posts = await getDraftPostsInSchedule(schedule)
} else {
posts = await getDraftPosts()
}
Expand All @@ -61,9 +66,9 @@ export async function GET(
{ status: 500 }
)
}
}
})

export async function POST(request: Request) {
export const POST = withBskyLogoutForRequest(async (request) => {
try {
const input = await request.json()
if (Array.isArray(input)) {
Expand All @@ -84,15 +89,11 @@ export async function POST(request: Request) {
{ status: 500 }
)
}
}
})

export async function PUT(
request: Request,
{ params }: { params: Promise<{ id?: string }> }
) {
export const PUT = withBskyLogoutWithId(async (id, request) => {
try {
const input: CreateDraftInput = await request.json()
const { id } = await params
if (!id) {
return NextResponse.json(
{ error: 'Post ID is required' },
Expand All @@ -109,4 +110,4 @@ export async function PUT(
{ status: 500 }
)
}
}
})
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import {
getScheduleLookups,
publishNextPost,
} from '../../../services/SchedulePostService'
reorderSchedulePosts,
} from '../../services/SchedulePostService'
import { NextResponse } from 'next/server'
import Logger from '@/app/api-helpers/logger'
const logger = new Logger('SchPostRoute')
import { withBskyLogoutWithId } from '@/app/api-helpers/apiWrapper'

export const GET = withBskyLogoutWithId(async (id, request) => {
// Get schedule lookups
export const GET = withBskyLogoutWithId(async (id) => {
try {
const searchParams = new URL(request.url).searchParams
const dateCountParam = searchParams.get('dateCount')
const dateCount = dateCountParam ? parseInt(dateCountParam, 10) : 5
if (Number.isNaN(dateCount) || dateCount <= 0) {
return NextResponse.json(
{ error: 'dateCount must be a positive integer' },
{ status: 400 }
)
}

if (!id) {
logger.error('Schedule ID is required')
return NextResponse.json(
Expand All @@ -28,7 +20,7 @@ export const GET = withBskyLogoutWithId(async (id, request) => {
}

const now = new Date()
const lookups = await getScheduleLookups(now, id, dateCount)
const lookups = await getScheduleLookups(now, id)
if (!lookups) {
logger.error('No scheduled lookups found')
return NextResponse.json(
Expand All @@ -46,6 +38,40 @@ export const GET = withBskyLogoutWithId(async (id, request) => {
}
})

// Reorder next posts for schedule
export const PUT = withBskyLogoutWithId(async (id, request) => {
try {
const { newOrder } = await request.json()
if (!id) {
logger.error('Schedule ID is required for updating next posts')
return NextResponse.json(
{ error: 'Schedule ID is required' },
{ status: 400 }
)
}
if (!Array.isArray(newOrder)) {
logger.error('newOrder must be an array')
return NextResponse.json(
{ error: 'newOrder must be an array' },
{ status: 400 }
)
}

await reorderSchedulePosts(id, newOrder)

return NextResponse.json({
message: 'Schedule order updated successfully',
})
} catch (error) {
logger.error('Failed to update schedule order', error)
return NextResponse.json(
{ error: 'Failed to update schedule order' },
{ status: 500 }
)
}
})

// Publish the next post for the schedule
export const POST = withBskyLogoutWithId(async (id) => {
try {
if (!id) {
Expand Down
Loading
Loading