Skip to content
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

feat: sync plans between devices #103

Merged
merged 20 commits into from
Dec 10, 2024
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
env:
USOS_CONSUMER_KEY: hello
USOS_CONSUMER_SECRET: jello
NEXT_PUBLIC_API_URL: http://localhost:3333
if: always()

- name: Find deadcode
Expand Down
92 changes: 55 additions & 37 deletions backend/app/controllers/schedules_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ export default class SchedulesController {
userId: userId,
})

if (payload.groups !== undefined) {
await schedule.related('groups').sync(payload.groups.map((group) => group.id))
}

if (payload.registrations !== undefined) {
await schedule.related('registrations').sync(payload.registrations.map((group) => group.id))
}

if (payload.courses !== undefined) {
await schedule.related('courses').sync(payload.courses.map((group) => group.id))
}

Comment on lines +81 to +92
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Czemu już tutaj dodajemy groupsy, rejegistrations i courses do planu? Zamysł miałem taki, zeby po kliknięciu dodania planu na froncie szedł request po prostu z domyślną nazwą. Czemu trzeba było to zmienić?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

z prostego powodu, jak mam plan lokalny i chce stworzyc jego online wersje po zalogowaniu od razu moge to zrobic a nie robic dwa requesty

return { message: 'Schedule created.', schedule }
}

Expand Down Expand Up @@ -112,6 +124,10 @@ export default class SchedulesController {

// Transformacja danych do żądanej struktury
const transformedSchedule = {
id: schedule.id,
userId: schedule.userId,
createdAt: schedule.createdAt,
updatedAt: schedule.updatedAt,
name: schedule.name,
registrations: schedule.registrations.map((reg) => ({
id: reg.id,
Expand All @@ -136,55 +152,57 @@ export default class SchedulesController {
* Allows updating the schedule and modifying its groups
*/
async update({ params, request, auth }: HttpContext) {
const userId = auth.user?.id
if (!userId) {
return { message: 'User not authenticated.' }
}
try {
const userId = auth.user?.id
if (!userId) {
return { message: 'User not authenticated.' }
}

const payload = await request.validateUsing(updateScheduleValidator)
const payload = await request.validateUsing(updateScheduleValidator)

const currSchedule = await Schedule.query()
.where('id', params.schedule_id)
.andWhere('userId', userId)
.firstOrFail()
const currSchedule = await Schedule.query()
.where('id', params.schedule_id)
.andWhere('userId', userId)
.firstOrFail()

if (payload.name) {
currSchedule.name = payload.name
}
if (payload.name) {
currSchedule.name = payload.name
}

if (payload.groups !== undefined) {
if (payload.groups.length === 0) {
await currSchedule.related('groups').sync([])
} else {
await currSchedule.related('groups').sync(payload.groups.map((group) => group.id))
if (payload.groups !== undefined) {
if (payload.groups.length === 0) {
await currSchedule.related('groups').sync([])
} else {
await currSchedule.related('groups').sync(payload.groups.map((group) => group.id))
}
}
}

if (payload.registrations !== undefined) {
if (payload.registrations.length === 0) {
await currSchedule.related('registrations').sync([])
} else {
await currSchedule
.related('registrations')
.sync(payload.registrations.map((group) => group.id))
if (payload.registrations !== undefined) {
if (payload.registrations.length === 0) {
await currSchedule.related('registrations').sync([])
} else {
await currSchedule
.related('registrations')
.sync(payload.registrations.map((group) => group.id))
}
}
}

if (payload.courses !== undefined) {
if (payload.courses.length === 0) {
await currSchedule.related('courses').sync([])
} else {
await currSchedule.related('courses').sync(payload.courses.map((group) => group.id))
if (payload.courses !== undefined) {
if (payload.courses.length === 0) {
await currSchedule.related('courses').sync([])
} else {
await currSchedule.related('courses').sync(payload.courses.map((group) => group.id))
}
}
}

if (payload.updatedAt) {
currSchedule.updatedAt = DateTime.fromJSDate(payload.updatedAt)
}
currSchedule.updatedAt = DateTime.fromISO(new Date().toISOString())

await currSchedule.save()
await currSchedule.save()

return { message: 'Schedule updated successfully.', currSchedule }
return { message: 'Schedule updated successfully.', schedule: currSchedule, success: true }
} catch (error) {
return { message: 'Schedule not found.', success: false }
}
}

/**
Expand Down
23 changes: 22 additions & 1 deletion backend/app/validators/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@ import vine from '@vinejs/vine'
export const createScheduleValidator = vine.compile(
vine.object({
name: vine.string(),
groups: vine
.array(
vine.object({
id: vine.number(),
})
)
.optional(),
courses: vine
.array(
vine.object({
id: vine.string(),
})
)
.optional(),
registrations: vine
.array(
vine.object({
id: vine.string(),
})
)
.optional(),
})
)

Expand Down Expand Up @@ -30,6 +51,6 @@ export const updateScheduleValidator = vine.compile(
})
)
.optional(),
updatedAt: vine.date().optional(),
updatedAt: vine.string().optional(),
})
)
2 changes: 1 addition & 1 deletion frontend/knip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const config = {
"lint-staged.config.mjs",
],
// sharp is used in nextjs image optimization
ignoreDependencies: ["sharp", "@radix-ui/*", "eslint-config-next"],
ignoreDependencies: ["sharp", "@radix-ui/*"],
} satisfies KnipConfig;

export default config;
Loading
Loading