forked from adrianhajdin/healthcare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patient.actions.ts
112 lines (99 loc) · 2.79 KB
/
patient.actions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"use server";
import { ID, InputFile, Query } from "node-appwrite";
import {
BUCKET_ID,
DATABASE_ID,
ENDPOINT,
PATIENT_COLLECTION_ID,
PROJECT_ID,
databases,
storage,
users,
} from "../appwrite.config";
import { parseStringify } from "../utils";
// CREATE APPWRITE USER
export const createUser = async (user: CreateUserParams) => {
try {
// Create new user -> https://appwrite.io/docs/references/1.5.x/server-nodejs/users#create
const newuser = await users.create(
ID.unique(),
user.email,
user.phone,
undefined,
user.name
);
return parseStringify(newuser);
} catch (error: any) {
// Check existing user
if (error && error?.code === 409) {
const existingUser = await users.list([
Query.equal("email", [user.email]),
]);
return existingUser.users[0];
}
console.error("An error occurred while creating a new user:", error);
}
};
// GET USER
export const getUser = async (userId: string) => {
try {
const user = await users.get(userId);
return parseStringify(user);
} catch (error) {
console.error(
"An error occurred while retrieving the user details:",
error
);
}
};
// REGISTER PATIENT
export const registerPatient = async ({
identificationDocument,
...patient
}: RegisterUserParams) => {
try {
// Upload file -> // https://appwrite.io/docs/references/cloud/client-web/storage#createFile
let file;
if (identificationDocument) {
const inputFile =
identificationDocument &&
InputFile.fromBlob(
identificationDocument?.get("blobFile") as Blob,
identificationDocument?.get("fileName") as string
);
file = await storage.createFile(BUCKET_ID!, ID.unique(), inputFile);
}
// Create new patient document -> https://appwrite.io/docs/references/cloud/server-nodejs/databases#createDocument
const newPatient = await databases.createDocument(
DATABASE_ID!,
PATIENT_COLLECTION_ID!,
ID.unique(),
{
identificationDocumentId: file?.$id ? file.$id : null,
identificationDocumentUrl: file?.$id
? `${ENDPOINT}/storage/buckets/${BUCKET_ID}/files/${file.$id}/view??project=${PROJECT_ID}`
: null,
...patient,
}
);
return parseStringify(newPatient);
} catch (error) {
console.error("An error occurred while creating a new patient:", error);
}
};
// GET PATIENT
export const getPatient = async (userId: string) => {
try {
const patients = await databases.listDocuments(
DATABASE_ID!,
PATIENT_COLLECTION_ID!,
[Query.equal("userId", [userId])]
);
return parseStringify(patients.documents[0]);
} catch (error) {
console.error(
"An error occurred while retrieving the patient details:",
error
);
}
};