``add new database operations for billing address module including create, delete, get, update functions and related type definitions``

This commit is contained in:
louiscklaw
2025-05-13 13:26:41 +08:00
parent 1aa0502edc
commit 64ca29cf60
13 changed files with 170 additions and 0 deletions

View File

@@ -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<RecordModel> {
return pb.collection(COL_STUDENTS).create(data);
}

View File

@@ -0,0 +1,6 @@
import { pb } from '@/lib/pb';
import { COL_STUDENTS, COL_USER_METAS } from '@/constants';
export async function deleteStudent(id: string): Promise<boolean> {
return pb.collection(COL_USER_METAS).delete(id);
}

View File

@@ -0,0 +1,9 @@
import { COL_STUDENTS, COL_USER_METAS } from '@/constants';
import { pb } from '@/lib/pb';
export default async function GetActiveCount(): Promise<number> {
const { totalItems: count } = await pb.collection(COL_USER_METAS).getList(1, 1, {
filter: 'status = "active" && role = "student"',
});
return count;
}

View File

@@ -0,0 +1,7 @@
import { pb } from '@/lib/pb';
import { COL_STUDENTS } from '@/constants';
import { RecordModel } from 'pocketbase';
export async function getAllStudents(options = {}): Promise<RecordModel[]> {
return pb.collection(COL_STUDENTS).getFullList(options);
}

View File

@@ -0,0 +1,10 @@
import { pb } from '@/lib/pb';
import { COL_USER_METAS } from '@/constants';
export async function getAllStudentsCount(): Promise<number> {
const result = await pb.collection(COL_USER_METAS).getList(1, 1, {
filter: `role = "student"`,
//
});
return result.totalItems;
}

View File

@@ -0,0 +1,9 @@
import { COL_USER_METAS } from '@/constants';
import { pb } from '@/lib/pb';
export default async function GetBlockedCount(): Promise<number> {
const { totalItems: count } = await pb.collection(COL_USER_METAS).getList(1, 1, {
filter: 'status = "blocked" && role = "student"',
});
return count;
}

View File

@@ -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<UserMeta> {
const record = await pb
.collection(COL_BILLING_ADDRESS)
.getOne<DBUserMeta>(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;
}

View File

@@ -0,0 +1,9 @@
import { COL_USER_METAS } from '@/constants';
import { pb } from '@/lib/pb';
export default async function GetPendingCount(): Promise<number> {
const { totalItems: count } = await pb.collection(COL_USER_METAS).getList(1, 1, {
filter: 'status = "pending" && role = "student"',
});
return count;
}

View File

@@ -0,0 +1,3 @@
export function helloCustomer() {
return 'Hello from Customers module!';
}

View File

@@ -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<EditFormProps>): Promise<RecordModel> {
return pb.collection(COL_CUSTOMERS).update(id, data);
}

View File

@@ -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<UpdateBillingAddress>): Promise<RecordModel> {
return pb.collection(COL_BILLING_ADDRESS).update(id, data);
}

View File

@@ -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))
}
```

View File

@@ -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;
}