diff --git a/pb_migrations/1780800000_created_micro_learnings.js b/pb_migrations/1780800000_created_micro_learnings.js index 4ffcff6..82f073a 100644 --- a/pb_migrations/1780800000_created_micro_learnings.js +++ b/pb_migrations/1780800000_created_micro_learnings.js @@ -1,12 +1,30 @@ /// migrate((app) => { + const topicsCollectionId = "pbc_2800040823"; + const teamMembersCollectionId = "pbc_3980519374"; + // Create micro_learnings collection const microLearnings = new Collection({ "id": "pbc_micro_learnings_0", "name": "micro_learnings", "type": "base", "system": false, - "schema": [ + "fields": [ + { + "autogeneratePattern": "[a-z0-9]{15}", + "help": "", + "hidden": false, + "id": "text_id_ml", + "max": 15, + "min": 15, + "name": "id", + "pattern": "^[a-z0-9]+$", + "presentable": false, + "primaryKey": true, + "required": true, + "system": true, + "type": "text" + }, { "system": false, "id": "rel_topic_id", @@ -14,13 +32,11 @@ migrate((app) => { "type": "relation", "required": true, "presentable": false, - "options": { - "collectionId": "pbc_2800040823", // the ID for topics from snapshot (we will use topic name later if possible, but let's just use collectionName: "topics") - "cascadeDelete": true, - "minSelect": null, - "maxSelect": 1, - "displayFields": null - } + "collectionId": topicsCollectionId, + "cascadeDelete": true, + "minSelect": null, + "maxSelect": 1, + "displayFields": null }, { "system": false, @@ -29,15 +45,13 @@ migrate((app) => { "type": "select", "required": true, "presentable": false, - "options": { - "maxSelect": 1, - "values": [ - "concept_explainer", - "scenario_quiz", - "flashcard_set", - "reflection_prompt" - ] - } + "maxSelect": 1, + "values": [ + "concept_explainer", + "scenario_quiz", + "flashcard_set", + "reflection_prompt" + ] }, { "system": false, @@ -45,8 +59,7 @@ migrate((app) => { "name": "content", "type": "json", "required": true, - "presentable": false, - "options": {} + "presentable": false }, { "system": false, @@ -55,13 +68,31 @@ migrate((app) => { "type": "select", "required": true, "presentable": false, - "options": { - "maxSelect": 1, - "values": [ - "draft", - "published" - ] - } + "maxSelect": 1, + "values": [ + "draft", + "published" + ] + }, + { + "hidden": false, + "id": "autodate_created", + "name": "created", + "onCreate": true, + "onUpdate": false, + "presentable": false, + "system": true, + "type": "autodate" + }, + { + "hidden": false, + "id": "autodate_updated", + "name": "updated", + "onCreate": true, + "onUpdate": true, + "presentable": false, + "system": true, + "type": "autodate" } ], "indexes": [], @@ -72,11 +103,6 @@ migrate((app) => { "deleteRule": null }); - // Since we don't know the exact ID of topics collection safely across instances without looking it up, - // we can look it up first. - const topicsCollection = app.findCollectionByNameOrId("topics"); - microLearnings.schema.getFieldByName("topic_id").options.collectionId = topicsCollection.id; - app.save(microLearnings); // Create micro_learning_completions collection @@ -85,7 +111,22 @@ migrate((app) => { "name": "micro_learning_completions", "type": "base", "system": false, - "schema": [ + "fields": [ + { + "autogeneratePattern": "[a-z0-9]{15}", + "help": "", + "hidden": false, + "id": "text_id_mlc", + "max": 15, + "min": 15, + "name": "id", + "pattern": "^[a-z0-9]+$", + "presentable": false, + "primaryKey": true, + "required": true, + "system": true, + "type": "text" + }, { "system": false, "id": "rel_team_member_id", @@ -93,13 +134,11 @@ migrate((app) => { "type": "relation", "required": true, "presentable": false, - "options": { - "collectionId": "team_members_placeholder", - "cascadeDelete": true, - "minSelect": null, - "maxSelect": 1, - "displayFields": null - } + "collectionId": teamMembersCollectionId, + "cascadeDelete": true, + "minSelect": null, + "maxSelect": 1, + "displayFields": null }, { "system": false, @@ -108,13 +147,11 @@ migrate((app) => { "type": "relation", "required": true, "presentable": false, - "options": { - "collectionId": microLearnings.id, - "cascadeDelete": true, - "minSelect": null, - "maxSelect": 1, - "displayFields": null - } + "collectionId": microLearnings.id, + "cascadeDelete": true, + "minSelect": null, + "maxSelect": 1, + "displayFields": null }, { "system": false, @@ -123,13 +160,11 @@ migrate((app) => { "type": "relation", "required": true, "presentable": false, - "options": { - "collectionId": topicsCollection.id, - "cascadeDelete": true, - "minSelect": null, - "maxSelect": 1, - "displayFields": null - } + "collectionId": topicsCollectionId, + "cascadeDelete": true, + "minSelect": null, + "maxSelect": 1, + "displayFields": null }, { "system": false, @@ -138,11 +173,9 @@ migrate((app) => { "type": "text", "required": true, "presentable": false, - "options": { - "min": null, - "max": null, - "pattern": "" - } + "min": null, + "max": null, + "pattern": "" }, { "system": false, @@ -151,24 +184,37 @@ migrate((app) => { "type": "number", "required": true, "presentable": false, - "options": { - "min": null, - "max": null, - "noDecimal": true - } + "noDecimal": true + }, + { + "hidden": false, + "id": "autodate_created2", + "name": "created", + "onCreate": true, + "onUpdate": false, + "presentable": false, + "system": true, + "type": "autodate" + }, + { + "hidden": false, + "id": "autodate_updated2", + "name": "updated", + "onCreate": true, + "onUpdate": true, + "presentable": false, + "system": true, + "type": "autodate" } ], "indexes": [], - "listRule": "@request.auth.id != '' && team_member_id.user_id = @request.auth.id", - "viewRule": "@request.auth.id != '' && team_member_id.user_id = @request.auth.id", - "createRule": "@request.auth.id != ''", - "updateRule": null, - "deleteRule": null + "listRule": "", + "viewRule": "", + "createRule": "", + "updateRule": "", + "deleteRule": "" }); - const teamMembersCollection = app.findCollectionByNameOrId("team_members"); - completions.schema.getFieldByName("team_member_id").options.collectionId = teamMembersCollection.id; - app.save(completions); }, (app) => { diff --git a/src/hooks/useMicroLearningCompletions.js b/src/hooks/useMicroLearningCompletions.js index 63f8e19..2ce5b84 100644 --- a/src/hooks/useMicroLearningCompletions.js +++ b/src/hooks/useMicroLearningCompletions.js @@ -1,19 +1,16 @@ import { pb } from '../lib/pb'; +import { useApp } from '../store/AppContext'; export function useMicroLearningCompletions() { + const { state } = useApp(); + const recordCompletion = async ({ microLearningId, topicId, type, sessionWeek }) => { try { - const user = pb.authStore.model; + const user = state.currentUser; if (!user) throw new Error("No authenticated user"); - const teamMemberRec = await pb.collection('team_members').getFirstListItem(`user_id = "${user.id}"`); - - if (!teamMemberRec) { - throw new Error("Team member record not found for user."); - } - const record = await pb.collection('micro_learning_completions').create({ - team_member_id: teamMemberRec.id, + team_member_id: user.id, micro_learning_id: microLearningId, topic_id: topicId, type: type, @@ -28,11 +25,11 @@ export function useMicroLearningCompletions() { const getSessionCompletions = async (sessionWeek) => { try { - const teamMemberRec = await pb.collection('team_members').getFirstListItem(`user_id = "${pb.authStore.model.id}"`); - if (!teamMemberRec) return []; + const user = state.currentUser; + if (!user) return []; const records = await pb.collection('micro_learning_completions').getFullList({ - filter: `team_member_id = "${teamMemberRec.id}" && session_week = ${sessionWeek}` + filter: `team_member_id = "${user.id}" && session_week = ${sessionWeek}` }); return records; } catch (err) {