Skip to content

Commit

Permalink
Add milestone age groups
Browse files Browse the repository at this point in the history
- add MilestoneAgeGroup
  - specifies a range of months to which milestones apply
  - add corresponding CRUD endpoints
- add age_group_id to MilestoneGroup and MilestoneAnswerSession
- get milestone-groups endpoints now have a query parameter age_group_id
- admin interface
  - add component to edit age groups
  - categorise milestonegroups by age group
- resolves #106
  • Loading branch information
lkeegan committed Oct 18, 2024
1 parent 62ddbe4 commit 3afc323
Show file tree
Hide file tree
Showing 21 changed files with 683 additions and 122 deletions.
82 changes: 73 additions & 9 deletions frontend/src/lib/admin.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import {
authCookieLogin,
getMilestoneGroupsAdmin,
getUserQuestionsAdmin,
usersCurrentUser
getMilestoneAgeGroups,
createMilestoneAgeGroup,
deleteMilestoneAgeGroup,
usersCurrentUser,
updateMilestoneAgeGroup
} from '$lib/client/services.gen';
import type {
GetLanguagesResponse,
MilestoneAgeGroupPublic,
MilestoneGroupAdmin,
UserQuestionAdmin,
MilestoneAgeGroupCreate,
UserRead,
Body_auth_cookie_login_auth_login_post
} from '$lib/client/types.gen';
Expand Down Expand Up @@ -47,12 +51,24 @@ export const adminUser = AdminUser();

export async function refreshMilestoneGroups() {
console.log('refreshMilestoneGroups...');
const { data, error } = await getMilestoneGroupsAdmin();
if (error || data == undefined) {
console.log('Failed to get MilestoneGroups');
milestoneGroups.set([]);
} else {
milestoneGroups.set(data);
await milestoneAgeGroups.refresh();
if (!milestoneAgeGroups.value) {
return;
}
const groups = {} as Record<string, Array<MilestoneGroupAdmin>>;
for (const ageGroup of milestoneAgeGroups.value) {
const { data, error } = await getMilestoneGroupsAdmin({
query: {
milestone_age_group_id: ageGroup.id
}
});
if (error || data == undefined) {
console.log(`Failed to get MilestoneGroups for ${ageGroup}`);
} else {
groups[`${ageGroup.id}`] = data;
}
milestoneGroups.set(groups);
console.log(groups);
}
}

Expand All @@ -70,3 +86,51 @@ export async function refreshUserQuestions() {
userQuestions.set(data);
}
}

function MilestoneAgeGroups() {
let milestoneAgeGroups = $state(null as Array<MilestoneAgeGroupPublic> | null);
return {
get value(): Array<MilestoneAgeGroupPublic> | null {
return milestoneAgeGroups;
},
create: async function (ageGroup: MilestoneAgeGroupCreate) {
const { data, error } = await createMilestoneAgeGroup({ body: ageGroup });
if (error || data === undefined) {
console.log(`Failed to create MilestoneAgeGroup ${ageGroup}`);
} else {
await this.refresh();
}
},
save: async function (ageGroup: MilestoneAgeGroupPublic) {
const { data, error } = await updateMilestoneAgeGroup({ body: ageGroup });
if (error || data === undefined) {
console.log(`Failed to save MilestoneAgeGroup ${ageGroup}`);
} else {
await this.refresh();
}
},
delete: async function (id: number) {
const { data, error } = await deleteMilestoneAgeGroup({
path: {
milestone_age_group_id: id
}
});
if (error || data === undefined) {
console.log(`Failed to delete MilestoneAgeGroup ${id}`);
} else {
await this.refresh();
}
},
refresh: async function () {
const { data, error } = await getMilestoneAgeGroups();
if (error || data === undefined) {
console.log('Failed to get MilestoneAgeGroups');
milestoneAgeGroups = null;
} else {
milestoneAgeGroups = data;
}
}
};
}

export const milestoneAgeGroups = MilestoneAgeGroups();
52 changes: 50 additions & 2 deletions frontend/src/lib/client/schemas.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,42 @@ export const MilestoneAdminSchema = {
title: 'MilestoneAdmin'
} as const;

export const MilestoneAgeGroupCreateSchema = {
properties: {
months_min: {
type: 'integer',
title: 'Months Min'
},
months_max: {
type: 'integer',
title: 'Months Max'
}
},
type: 'object',
required: ['months_min', 'months_max'],
title: 'MilestoneAgeGroupCreate'
} as const;

export const MilestoneAgeGroupPublicSchema = {
properties: {
months_min: {
type: 'integer',
title: 'Months Min'
},
months_max: {
type: 'integer',
title: 'Months Max'
},
id: {
type: 'integer',
title: 'Id'
}
},
type: 'object',
required: ['months_min', 'months_max', 'id'],
title: 'MilestoneAgeGroupPublic'
} as const;

export const MilestoneAnswerPublicSchema = {
properties: {
milestone_id: {
Expand All @@ -330,6 +366,14 @@ export const MilestoneAnswerSessionPublicSchema = {
type: 'integer',
title: 'Id'
},
child_id: {
type: 'integer',
title: 'Child Id'
},
age_group_id: {
type: 'integer',
title: 'Age Group Id'
},
created_at: {
type: 'string',
format: 'date-time',
Expand All @@ -344,7 +388,7 @@ export const MilestoneAnswerSessionPublicSchema = {
}
},
type: 'object',
required: ['id', 'created_at', 'answers'],
required: ['id', 'child_id', 'age_group_id', 'created_at', 'answers'],
title: 'MilestoneAnswerSessionPublic'
} as const;

Expand All @@ -354,6 +398,10 @@ export const MilestoneGroupAdminSchema = {
type: 'integer',
title: 'Id'
},
age_group_id: {
type: 'integer',
title: 'Age Group Id'
},
order: {
type: 'integer',
title: 'Order'
Expand All @@ -376,7 +424,7 @@ export const MilestoneGroupAdminSchema = {
}
},
type: 'object',
required: ['id', 'order'],
required: ['id', 'age_group_id', 'order'],
title: 'MilestoneGroupAdmin'
} as const;

Expand Down
86 changes: 82 additions & 4 deletions frontend/src/lib/client/services.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ import type {
GetMilestoneData,
GetMilestoneError,
GetMilestoneResponse,
GetMilestoneGroupsData,
GetMilestoneGroupsError,
GetMilestoneGroupsResponse,
GetMilestoneGroupData,
GetMilestoneGroupError,
GetMilestoneGroupResponse,
GetMilestoneAgeGroupsError,
GetMilestoneAgeGroupsResponse,
GetUserQuestionsError,
GetUserQuestionsResponse,
CreateLanguageData,
Expand All @@ -31,8 +34,19 @@ import type {
UpdateI18NData,
UpdateI18NError,
UpdateI18NResponse,
UpdateMilestoneAgeGroupData,
UpdateMilestoneAgeGroupError,
UpdateMilestoneAgeGroupResponse,
CreateMilestoneAgeGroupData,
CreateMilestoneAgeGroupError,
CreateMilestoneAgeGroupResponse,
DeleteMilestoneAgeGroupData,
DeleteMilestoneAgeGroupError,
DeleteMilestoneAgeGroupResponse,
GetMilestoneGroupsAdminData,
GetMilestoneGroupsAdminError,
GetMilestoneGroupsAdminResponse,
CreateMilestoneGroupAdminData,
CreateMilestoneGroupAdminError,
CreateMilestoneGroupAdminResponse,
UpdateMilestoneGroupAdminData,
Expand Down Expand Up @@ -169,7 +183,7 @@ export const getMilestone = <ThrowOnError extends boolean = false>(
* Get Milestone Groups
*/
export const getMilestoneGroups = <ThrowOnError extends boolean = false>(
options?: Options<unknown, ThrowOnError>
options: Options<GetMilestoneGroupsData, ThrowOnError>
) => {
return (options?.client ?? client).get<
GetMilestoneGroupsResponse,
Expand Down Expand Up @@ -197,6 +211,22 @@ export const getMilestoneGroup = <ThrowOnError extends boolean = false>(
});
};

/**
* Get Milestone Age Groups
*/
export const getMilestoneAgeGroups = <ThrowOnError extends boolean = false>(
options?: Options<unknown, ThrowOnError>
) => {
return (options?.client ?? client).get<
GetMilestoneAgeGroupsResponse,
GetMilestoneAgeGroupsError,
ThrowOnError
>({
...options,
url: '/milestone-age-groups/'
});
};

/**
* Get User Questions
*/
Expand Down Expand Up @@ -257,11 +287,59 @@ export const updateI18N = <ThrowOnError extends boolean = false>(
});
};

/**
* Update Milestone Age Group
*/
export const updateMilestoneAgeGroup = <ThrowOnError extends boolean = false>(
options: Options<UpdateMilestoneAgeGroupData, ThrowOnError>
) => {
return (options?.client ?? client).put<
UpdateMilestoneAgeGroupResponse,
UpdateMilestoneAgeGroupError,
ThrowOnError
>({
...options,
url: '/admin/milestone-age-groups/'
});
};

/**
* Create Milestone Age Group
*/
export const createMilestoneAgeGroup = <ThrowOnError extends boolean = false>(
options: Options<CreateMilestoneAgeGroupData, ThrowOnError>
) => {
return (options?.client ?? client).post<
CreateMilestoneAgeGroupResponse,
CreateMilestoneAgeGroupError,
ThrowOnError
>({
...options,
url: '/admin/milestone-age-groups/'
});
};

/**
* Delete Milestone Age Group
*/
export const deleteMilestoneAgeGroup = <ThrowOnError extends boolean = false>(
options: Options<DeleteMilestoneAgeGroupData, ThrowOnError>
) => {
return (options?.client ?? client).delete<
DeleteMilestoneAgeGroupResponse,
DeleteMilestoneAgeGroupError,
ThrowOnError
>({
...options,
url: '/admin/milestone-age-groups/{milestone_age_group_id}'
});
};

/**
* Get Milestone Groups Admin
*/
export const getMilestoneGroupsAdmin = <ThrowOnError extends boolean = false>(
options?: Options<unknown, ThrowOnError>
options: Options<GetMilestoneGroupsAdminData, ThrowOnError>
) => {
return (options?.client ?? client).get<
GetMilestoneGroupsAdminResponse,
Expand All @@ -277,15 +355,15 @@ export const getMilestoneGroupsAdmin = <ThrowOnError extends boolean = false>(
* Create Milestone Group Admin
*/
export const createMilestoneGroupAdmin = <ThrowOnError extends boolean = false>(
options?: Options<unknown, ThrowOnError>
options: Options<CreateMilestoneGroupAdminData, ThrowOnError>
) => {
return (options?.client ?? client).post<
CreateMilestoneGroupAdminResponse,
CreateMilestoneGroupAdminError,
ThrowOnError
>({
...options,
url: '/admin/milestone-groups/'
url: '/admin/milestone-groups/{milestone_age_group_id}'
});
};

Expand Down
Loading

0 comments on commit 3afc323

Please sign in to comment.