From 64ca29cf608057c5e2a873c58a056a9e9dbe3ceb Mon Sep 17 00:00:00 2001 From: louiscklaw Date: Tue, 13 May 2025 13:26:41 +0800 Subject: [PATCH] ```add new database operations for billing address module including create, delete, get, update functions and related type definitions``` --- .../cms/src/db/billingAddress/Create.tsx | 11 ++++++ .../cms/src/db/billingAddress/Delete.tsx | 6 ++++ .../src/db/billingAddress/GetActiveCount.tsx | 9 +++++ .../cms/src/db/billingAddress/GetAll.tsx | 7 ++++ .../cms/src/db/billingAddress/GetAllCount.tsx | 10 ++++++ .../src/db/billingAddress/GetBlockedCount.tsx | 9 +++++ .../cms/src/db/billingAddress/GetById.tsx | 34 +++++++++++++++++++ .../src/db/billingAddress/GetPendingCount.tsx | 9 +++++ .../cms/src/db/billingAddress/Helloworld.tsx | 3 ++ .../cms/src/db/billingAddress/Update.tsx | 8 +++++ .../cms/src/db/billingAddress/UpdateById.tsx | 10 ++++++ .../cms/src/db/billingAddress/_GUIDELINES.md | 31 +++++++++++++++++ .../cms/src/db/billingAddress/type.d.ts | 23 +++++++++++++ 13 files changed, 170 insertions(+) create mode 100644 002_source/cms/src/db/billingAddress/Create.tsx create mode 100644 002_source/cms/src/db/billingAddress/Delete.tsx create mode 100644 002_source/cms/src/db/billingAddress/GetActiveCount.tsx create mode 100644 002_source/cms/src/db/billingAddress/GetAll.tsx create mode 100644 002_source/cms/src/db/billingAddress/GetAllCount.tsx create mode 100644 002_source/cms/src/db/billingAddress/GetBlockedCount.tsx create mode 100644 002_source/cms/src/db/billingAddress/GetById.tsx create mode 100644 002_source/cms/src/db/billingAddress/GetPendingCount.tsx create mode 100644 002_source/cms/src/db/billingAddress/Helloworld.tsx create mode 100644 002_source/cms/src/db/billingAddress/Update.tsx create mode 100644 002_source/cms/src/db/billingAddress/UpdateById.tsx create mode 100644 002_source/cms/src/db/billingAddress/_GUIDELINES.md create mode 100644 002_source/cms/src/db/billingAddress/type.d.ts diff --git a/002_source/cms/src/db/billingAddress/Create.tsx b/002_source/cms/src/db/billingAddress/Create.tsx new file mode 100644 index 0000000..75c6f4e --- /dev/null +++ b/002_source/cms/src/db/billingAddress/Create.tsx @@ -0,0 +1,11 @@ +// 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 type { RecordModel } from 'pocketbase'; + +export async function createStudent(data: CreateFormProps): Promise { + return pb.collection(COL_STUDENTS).create(data); +} diff --git a/002_source/cms/src/db/billingAddress/Delete.tsx b/002_source/cms/src/db/billingAddress/Delete.tsx new file mode 100644 index 0000000..9f29ed9 --- /dev/null +++ b/002_source/cms/src/db/billingAddress/Delete.tsx @@ -0,0 +1,6 @@ +import { pb } from '@/lib/pb'; +import { COL_STUDENTS, COL_USER_METAS } from '@/constants'; + +export async function deleteStudent(id: string): Promise { + return pb.collection(COL_USER_METAS).delete(id); +} diff --git a/002_source/cms/src/db/billingAddress/GetActiveCount.tsx b/002_source/cms/src/db/billingAddress/GetActiveCount.tsx new file mode 100644 index 0000000..58eb51b --- /dev/null +++ b/002_source/cms/src/db/billingAddress/GetActiveCount.tsx @@ -0,0 +1,9 @@ +import { COL_STUDENTS, COL_USER_METAS } from '@/constants'; +import { pb } from '@/lib/pb'; + +export default async function GetActiveCount(): Promise { + const { totalItems: count } = await pb.collection(COL_USER_METAS).getList(1, 1, { + filter: 'status = "active" && role = "student"', + }); + return count; +} diff --git a/002_source/cms/src/db/billingAddress/GetAll.tsx b/002_source/cms/src/db/billingAddress/GetAll.tsx new file mode 100644 index 0000000..1e7c2c1 --- /dev/null +++ b/002_source/cms/src/db/billingAddress/GetAll.tsx @@ -0,0 +1,7 @@ +import { pb } from '@/lib/pb'; +import { COL_STUDENTS } from '@/constants'; +import { RecordModel } from 'pocketbase'; + +export async function getAllStudents(options = {}): Promise { + return pb.collection(COL_STUDENTS).getFullList(options); +} diff --git a/002_source/cms/src/db/billingAddress/GetAllCount.tsx b/002_source/cms/src/db/billingAddress/GetAllCount.tsx new file mode 100644 index 0000000..1bcae29 --- /dev/null +++ b/002_source/cms/src/db/billingAddress/GetAllCount.tsx @@ -0,0 +1,10 @@ +import { pb } from '@/lib/pb'; +import { COL_USER_METAS } from '@/constants'; + +export async function getAllStudentsCount(): Promise { + const result = await pb.collection(COL_USER_METAS).getList(1, 1, { + filter: `role = "student"`, + // + }); + return result.totalItems; +} diff --git a/002_source/cms/src/db/billingAddress/GetBlockedCount.tsx b/002_source/cms/src/db/billingAddress/GetBlockedCount.tsx new file mode 100644 index 0000000..0dbceb6 --- /dev/null +++ b/002_source/cms/src/db/billingAddress/GetBlockedCount.tsx @@ -0,0 +1,9 @@ +import { COL_USER_METAS } from '@/constants'; +import { pb } from '@/lib/pb'; + +export default async function GetBlockedCount(): Promise { + const { totalItems: count } = await pb.collection(COL_USER_METAS).getList(1, 1, { + filter: 'status = "blocked" && role = "student"', + }); + return count; +} diff --git a/002_source/cms/src/db/billingAddress/GetById.tsx b/002_source/cms/src/db/billingAddress/GetById.tsx new file mode 100644 index 0000000..0fc5f79 --- /dev/null +++ b/002_source/cms/src/db/billingAddress/GetById.tsx @@ -0,0 +1,34 @@ +import { COL_BILLING_ADDRESS, COL_STUDENTS, COL_USER_METAS } from '@/constants'; +import { RecordModel } from 'pocketbase'; + +import { logger } from '@/lib/default-logger'; +import { pb } from '@/lib/pb'; +import type { DBUserMeta, UserMeta } from '@/components/dashboard/user_meta/type.d'; + +export async function getBillingAddressById(id: string): Promise { + const record = await pb + .collection(COL_BILLING_ADDRESS) + .getOne(id, { expand: 'billingAddress, helloworld' }); + + console.log({ record }); + + 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/billingAddress/GetPendingCount.tsx b/002_source/cms/src/db/billingAddress/GetPendingCount.tsx new file mode 100644 index 0000000..29bfd09 --- /dev/null +++ b/002_source/cms/src/db/billingAddress/GetPendingCount.tsx @@ -0,0 +1,9 @@ +import { COL_USER_METAS } from '@/constants'; +import { pb } from '@/lib/pb'; + +export default async function GetPendingCount(): Promise { + const { totalItems: count } = await pb.collection(COL_USER_METAS).getList(1, 1, { + filter: 'status = "pending" && role = "student"', + }); + return count; +} diff --git a/002_source/cms/src/db/billingAddress/Helloworld.tsx b/002_source/cms/src/db/billingAddress/Helloworld.tsx new file mode 100644 index 0000000..2487997 --- /dev/null +++ b/002_source/cms/src/db/billingAddress/Helloworld.tsx @@ -0,0 +1,3 @@ +export function helloCustomer() { + return 'Hello from Customers module!'; +} diff --git a/002_source/cms/src/db/billingAddress/Update.tsx b/002_source/cms/src/db/billingAddress/Update.tsx new file mode 100644 index 0000000..7beeff7 --- /dev/null +++ b/002_source/cms/src/db/billingAddress/Update.tsx @@ -0,0 +1,8 @@ +import { pb } from '@/lib/pb'; +import { COL_CUSTOMERS } from '@/constants'; +import type { RecordModel } from 'pocketbase'; +import type { EditFormProps } from '@/components/dashboard/customer/type.d'; + +export async function updateCustomer(id: string, data: Partial): Promise { + return pb.collection(COL_CUSTOMERS).update(id, data); +} diff --git a/002_source/cms/src/db/billingAddress/UpdateById.tsx b/002_source/cms/src/db/billingAddress/UpdateById.tsx new file mode 100644 index 0000000..1af0b35 --- /dev/null +++ b/002_source/cms/src/db/billingAddress/UpdateById.tsx @@ -0,0 +1,10 @@ +import { COL_BILLING_ADDRESS } from '@/constants'; +import type { RecordModel } from 'pocketbase'; + +import { pb } from '@/lib/pb'; + +import type { UpdateBillingAddress } from './type'; + +export async function UpdateBillingAddressById(id: string, data: Partial): Promise { + return pb.collection(COL_BILLING_ADDRESS).update(id, data); +} diff --git a/002_source/cms/src/db/billingAddress/_GUIDELINES.md b/002_source/cms/src/db/billingAddress/_GUIDELINES.md new file mode 100644 index 0000000..6515d08 --- /dev/null +++ b/002_source/cms/src/db/billingAddress/_GUIDELINES.md @@ -0,0 +1,31 @@ +# GUIDELINES + +This folder contains drivers for `Customer`/`Customers` records using PocketBase: + +- create (Create.tsx) +- read (GetById.tsx) +- write (Update.tsx) +- count (GetAllCount.tsx, GetActiveCount.tsx, GetBlockedCount.tsx, GetPendingCount.tsx) +- misc (Helloworld.tsx) +- delete (Delete.tsx) +- list (GetAll.tsx) + +the `@` sign refer to `/home/logic/_wsl_workspace/001_github_ws/lettersoup-online-ws/lettersoup-online/project/002_source/cms/src` + +## Assumption and Requirements + +- assume `pb` is located in `@/lib/pb` +- no need to handle error in this function, i'll handle it in the caller +- type information defined in `/home/logic/_wsl_workspace/001_github_ws/lettersoup-online-ws/lettersoup-online/project/002_source/cms/src/db/Customers/type.d.tsx` + +simple template: + +```typescript +import { pb } from '@/lib/pb'; +import { COL_CUSTOMERS } from '@/constants'; + +export async function createCustomer(data: CreateFormProps) { + // ...content + // use direct return of pb.collection (e.g. return pb.collection(xxx)) +} +``` diff --git a/002_source/cms/src/db/billingAddress/type.d.ts b/002_source/cms/src/db/billingAddress/type.d.ts new file mode 100644 index 0000000..4528a4a --- /dev/null +++ b/002_source/cms/src/db/billingAddress/type.d.ts @@ -0,0 +1,23 @@ +export interface BillingAddress { + city: string; + country: string; + line1: string; + line2: string; + state: string; + zipCode: string; + // + id: string; + collectionId: string; + collectionName: string; + updated: string; + created: string; +} + +export interface UpdateBillingAddress { + city?: string; + country?: string; + line1?: string; + line2?: string; + state?: string; + zipCode?: string; +}