From 09ded06dd2af86d09d2464fb618ce98d0a5338d3 Mon Sep 17 00:00:00 2001 From: louiscklaw Date: Tue, 13 May 2025 13:27:27 +0800 Subject: [PATCH] ```update student database operations to use COL_USER_METAS instead of COL_STUDENTS, refactor getStudentById to include expanded billing address data, and add/update related types and functions for student and user meta management``` --- 002_source/cms/src/db/Students/Create.tsx | 9 +++-- 002_source/cms/src/db/Students/GetById.tsx | 31 ++++++++++++--- 002_source/cms/src/db/Students/UpdateById.tsx | 10 +++++ 002_source/cms/src/db/Students/type.d.ts | 28 +++++++++++++ .../cms/src/db/UserMetas/UpdateById.tsx | 10 +++++ 002_source/cms/src/db/UserMetas/type.d.ts | 39 +++++++++++++++++++ 6 files changed, 118 insertions(+), 9 deletions(-) create mode 100644 002_source/cms/src/db/Students/UpdateById.tsx create mode 100644 002_source/cms/src/db/UserMetas/UpdateById.tsx create mode 100644 002_source/cms/src/db/UserMetas/type.d.ts diff --git a/002_source/cms/src/db/Students/Create.tsx b/002_source/cms/src/db/Students/Create.tsx index 75c6f4e..d9aa24f 100644 --- a/002_source/cms/src/db/Students/Create.tsx +++ b/002_source/cms/src/db/Students/Create.tsx @@ -1,11 +1,12 @@ // api method for crate student record // RULES: // TBA -import { pb } from '@/lib/pb'; -import { COL_STUDENTS } from '@/constants'; -import type { CreateFormProps } from '@/components/dashboard/student/type.d'; +import { COL_STUDENTS, COL_USER_METAS } from '@/constants'; import type { RecordModel } from 'pocketbase'; +import { pb } from '@/lib/pb'; +import type { CreateFormProps } from '@/components/dashboard/student/type.d'; + export async function createStudent(data: CreateFormProps): Promise { - return pb.collection(COL_STUDENTS).create(data); + return pb.collection(COL_USER_METAS).create(data); } diff --git a/002_source/cms/src/db/Students/GetById.tsx b/002_source/cms/src/db/Students/GetById.tsx index 39a12d8..1c0cd17 100644 --- a/002_source/cms/src/db/Students/GetById.tsx +++ b/002_source/cms/src/db/Students/GetById.tsx @@ -1,7 +1,28 @@ -import { pb } from '@/lib/pb'; -import { COL_STUDENTS } from '@/constants'; -import { RecordModel } from 'pocketbase'; +import { COL_USER_METAS } from '@/constants'; -export async function getStudentById(id: string): Promise { - return pb.collection(COL_STUDENTS).getOne(id); +import { pb } from '@/lib/pb'; +import type { DBUserMeta, UserMeta } from '@/components/dashboard/user_meta/type.d'; + +export async function getStudentById(id: string): Promise { + const record = await pb.collection(COL_USER_METAS).getOne(id, { expand: 'billingAddress, helloworld' }); + + const temp: UserMeta = { + id: record.id, + name: record.name, + email: record.email, + quota: record.quota, + billingAddress: record.expand.billingAddress ? record.expand.billingAddress[0] : {}, + status: record.status, + state: record.state, + createdAt: new Date(record.created), + collectionId: record.collectionId, + avatar: record.avatar, + phone: record.phone, + company: record.company, + timezone: record.timezone, + language: record.language, + currency: record.currency, + }; + + return temp; } diff --git a/002_source/cms/src/db/Students/UpdateById.tsx b/002_source/cms/src/db/Students/UpdateById.tsx new file mode 100644 index 0000000..0a408b3 --- /dev/null +++ b/002_source/cms/src/db/Students/UpdateById.tsx @@ -0,0 +1,10 @@ +import { COL_USER_METAS } from '@/constants'; +import type { RecordModel } from 'pocketbase'; + +import { pb } from '@/lib/pb'; + +import type { UpdateStudent } from './type'; + +export async function UpdateStudentById(id: string, data: Partial): Promise { + return pb.collection(COL_USER_METAS).update(id, data); +} diff --git a/002_source/cms/src/db/Students/type.d.ts b/002_source/cms/src/db/Students/type.d.ts index 39e510b..32ed267 100644 --- a/002_source/cms/src/db/Students/type.d.ts +++ b/002_source/cms/src/db/Students/type.d.ts @@ -1,3 +1,5 @@ +import type { BillingAddress } from '@/components/dashboard/user_meta/type.d'; + // Student type definitions export interface Student { id: string; @@ -9,3 +11,29 @@ export interface Student { status: 'active' | 'blocked' | 'pending'; createdAt: Date; } + +export interface UpdateStudent { + name?: string; + // + // NOTE: obslete "avatar" and use "avatar_file" + // avatar_file?: string; + avatar: File | null; + // + email?: string; + phone?: string; + quota?: number; + company?: string; + // + // relation handle seperately + // billingAddress: BillingAddress | Record; + + // status is obsoleted, replace by state + // status: 'pending' | 'active' | 'blocked'; + state?: 'pending' | 'active' | 'blocked'; + // + timezone?: string; + language?: string; + currency?: string; + // + taxId?: string; +} diff --git a/002_source/cms/src/db/UserMetas/UpdateById.tsx b/002_source/cms/src/db/UserMetas/UpdateById.tsx new file mode 100644 index 0000000..61131d5 --- /dev/null +++ b/002_source/cms/src/db/UserMetas/UpdateById.tsx @@ -0,0 +1,10 @@ +import { COL_USER_METAS } from '@/constants'; +import type { RecordModel } from 'pocketbase'; + +import { pb } from '@/lib/pb'; + +import type { UpdateUserMeta } from './type'; + +export async function UpdateUserMetaById(id: string, data: Partial): Promise { + return pb.collection(COL_USER_METAS).update(id, data); +} diff --git a/002_source/cms/src/db/UserMetas/type.d.ts b/002_source/cms/src/db/UserMetas/type.d.ts new file mode 100644 index 0000000..65c5dd8 --- /dev/null +++ b/002_source/cms/src/db/UserMetas/type.d.ts @@ -0,0 +1,39 @@ +import type { BillingAddress } from '@/components/dashboard/user_meta/type.d'; + +// UserMeta type definitions +export interface UserMeta { + id: string; + name: string; + avatar: string; + email: string; + phone: string; + quota: number; + status: 'active' | 'blocked' | 'pending'; + createdAt: Date; +} + +export interface UpdateUserMeta { + name?: string; + // + // NOTE: obslete "avatar" and use "avatar_file" + // avatar_file?: string; + avatar: File | null; + // + email?: string; + phone?: string; + quota?: number; + company?: string; + // + // relation handle seperately + // billingAddress: BillingAddress | Record; + + // status is obsoleted, replace by state + // status: 'pending' | 'active' | 'blocked'; + state?: 'pending' | 'active' | 'blocked'; + // + timezone?: string; + language?: string; + currency?: string; + // + taxId?: string; +}