fix: CI pipeline alpine+docker-cli, calendar redesign + POST API
Some checks failed
Deploy / deploy (push) Failing after 4s
Some checks failed
Deploy / deploy (push) Failing after 4s
This commit is contained in:
@@ -4,22 +4,24 @@ import { google } from 'googleapis'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
|
||||
function getAuth() {
|
||||
// Service account JSON (inline or from file)
|
||||
function getAuth(readonly = true) {
|
||||
const scopes = readonly
|
||||
? ['https://www.googleapis.com/auth/calendar.readonly']
|
||||
: ['https://www.googleapis.com/auth/calendar']
|
||||
|
||||
const saJson = process.env.GOOGLE_SA_JSON
|
||||
if (saJson) {
|
||||
const sa = JSON.parse(saJson)
|
||||
return new google.auth.GoogleAuth({
|
||||
credentials: sa,
|
||||
scopes: ['https://www.googleapis.com/auth/calendar.readonly'],
|
||||
scopes,
|
||||
})
|
||||
}
|
||||
// Fallback: file
|
||||
const saPath = path.join(process.cwd(), 'google-sa.json')
|
||||
if (fs.existsSync(saPath)) {
|
||||
return new google.auth.GoogleAuth({
|
||||
keyFile: saPath,
|
||||
scopes: ['https://www.googleapis.com/auth/calendar.readonly'],
|
||||
scopes,
|
||||
})
|
||||
}
|
||||
return null
|
||||
@@ -29,7 +31,7 @@ export async function GET(req: Request) {
|
||||
const { searchParams } = new URL(req.url)
|
||||
const range = searchParams.get('range') || 'today'
|
||||
|
||||
const auth = getAuth()
|
||||
const auth = getAuth(true)
|
||||
if (!auth) {
|
||||
return NextResponse.json({ events: [], error: 'not_configured' })
|
||||
}
|
||||
@@ -48,8 +50,11 @@ export async function GET(req: Request) {
|
||||
timeMin = new Date(now.getFullYear(), now.getMonth(), now.getDate()).toISOString()
|
||||
timeMax = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 7).toISOString()
|
||||
} else {
|
||||
timeMin = new Date(now.getFullYear(), now.getMonth(), 1).toISOString()
|
||||
timeMax = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59).toISOString()
|
||||
// month — support year/month query params
|
||||
const targetYear = parseInt(searchParams.get('year') || String(now.getFullYear()))
|
||||
const targetMonth = parseInt(searchParams.get('month') || String(now.getMonth()))
|
||||
timeMin = new Date(targetYear, targetMonth, 1).toISOString()
|
||||
timeMax = new Date(targetYear, targetMonth + 1, 0, 23, 59).toISOString()
|
||||
}
|
||||
|
||||
const calendarClient = google.calendar({ version: 'v3', auth: auth as any })
|
||||
@@ -97,3 +102,46 @@ export async function GET(req: Request) {
|
||||
|
||||
return NextResponse.json({ events: allEvents, errors: errors.length ? errors : undefined, fetchedAt: new Date().toISOString() })
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const body = await req.json()
|
||||
const { title, date, startTime, endTime, allDay } = body
|
||||
|
||||
const auth = getAuth(false)
|
||||
if (!auth) return NextResponse.json({ error: 'not_configured' }, { status: 500 })
|
||||
|
||||
const calendarClient = google.calendar({ version: 'v3', auth: auth as any })
|
||||
|
||||
const daniilCalendarId = process.env.DANIIL_CALENDAR_ID || 'daniilklimov25@gmail.com'
|
||||
|
||||
let start: any, end: any
|
||||
if (allDay) {
|
||||
start = { date }
|
||||
end = { date }
|
||||
} else {
|
||||
start = { dateTime: `${date}T${startTime}:00`, timeZone: 'Europe/Moscow' }
|
||||
end = { dateTime: `${date}T${endTime}:00`, timeZone: 'Europe/Moscow' }
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await calendarClient.events.insert({
|
||||
calendarId: daniilCalendarId,
|
||||
requestBody: { summary: title, start, end },
|
||||
})
|
||||
const e = res.data
|
||||
return NextResponse.json({
|
||||
event: {
|
||||
id: e.id,
|
||||
title: e.summary || title,
|
||||
start: e.start?.dateTime || e.start?.date,
|
||||
end: e.end?.dateTime || e.end?.date,
|
||||
allDay: !e.start?.dateTime,
|
||||
owner: 'daniil',
|
||||
ownerName: 'Даниил',
|
||||
color: '#6366f1',
|
||||
}
|
||||
})
|
||||
} catch (err: any) {
|
||||
return NextResponse.json({ error: err.message || 'Failed to create event' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user