Skip to content

Commit

Permalink
feat: sync plans between devices (#103)
Browse files Browse the repository at this point in the history
* feat: logout function & middleware with API & new function to check auth status

* fix: format

* feat: honestly idk, too many things

* feat: integration with offline

* feat: continue with sync (save progress)

* feat: save progress

* feat: new env var with api url

* feat: save progress

* feat: save progress

* feat: save progress

* fix: lint & code fix & delete crypto from runtime edge

* fix: almost done i think

* fix: better logout

* fix: minor fixes in backend and frontend

backend: simple array
front:
- logout req
- React.useState -> useState
- better error response in plans functions

* fix: error handlingn fix

* feat: rewrite auth() function

* fix: better comments

* fix: middleware fixes

wiem bartus ze ci sie pewnie nie spodoba i chcialbys po prostu zrobic caly middleware w try catch i sama funkcja auth() jak w poprzednich tylko problem jest taki ze musze tak czy siak wykonac auth() zeby se uswierzytelnic za kazdym razem usera czy ma sesje a po tym doperio wykonac reszte logiki taka jak next jesli nie jest protected, wiec musialem tak zrobic
  • Loading branch information
qamarq authored Dec 10, 2024
1 parent f460f76 commit bca2ef2
Show file tree
Hide file tree
Showing 30 changed files with 1,519 additions and 298 deletions.
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))
}

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

0 comments on commit bca2ef2

Please sign in to comment.