Skip to content

Commit b0afeaf

Browse files
authored
Merge pull request NCUAppTeam#161 from 1989ONCE/dinner
Refactor
2 parents 7da0395 + e8cf4ab commit b0afeaf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1519
-1144
lines changed

src/assets/acorn.png

212 KB
Loading

src/assets/forest.jpg

-8.06 MB
Binary file not shown.

src/assets/squirrel.jpg

1.75 MB
Loading

src/backend/event/Controllers/EventController.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,27 @@ export default class EventController {
113113
return event
114114
}
115115

116-
}
116+
/**
117+
* Get event types
118+
*
119+
* @returns {Array<{ type_id: number; type_name: string }>} - Array of event types
120+
*
121+
* @throws {Error} - Throws an error if the query fails
122+
*/
123+
public async getEventTypes(): Promise<Array<{ type_id: number; type_name: string }> | null> {
124+
const { data, error } = await supabase
125+
.from('event_type')
126+
.select('*')
127+
.contains('hashtag_relation', [0]) // Use contains for array comparison
128+
.order('type_id', { ascending: true });
129+
130+
// Error handling
131+
if (error) {
132+
ErrorHandler.handleSupabaseError(error);
133+
return null;
134+
}
135+
136+
return data;
137+
}
138+
}
139+

src/backend/user/Controllers/UserController.ts

Lines changed: 94 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import ErrorHandler from "../../../utils/ErrorHandler";
22
import { supabase } from "../../../utils/supabase";
33

44
import User, { DBUser } from '../Entities/User';
5+
import UserFromPortal from "../Entities/UserFromPortal";
56
import UserService from '../Services/UserService';
6-
import UserSignupData from "../Entities/UserSignupData";
77

88
const USER_TABLE_NAME = "members"
99

10-
1110
export default class UserController {
1211

1312
/**
@@ -80,36 +79,29 @@ export default class UserController {
8079
* )
8180
*
8281
* @param {string} userID - Target user ID
83-
* @param {string} fields - The columns to retrieve
8482
*
8583
* @returns {User} - The target user entity (null if not found)
8684
*
8785
* @author Henry C. (@yeahlowflicker)
8886
*/
89-
public async findUserByID(userID: string, fields?: string) : Promise<User | null> {
87+
public async findUserByID(userID: string): Promise<User | null> {
9088

9189
const { data, error } = await supabase
9290
.from(USER_TABLE_NAME)
93-
.select(fields)
91+
.select("*")
9492
.eq("uuid", userID)
9593
.returns<DBUser>()
96-
.limit(1)
97-
.single()
98-
94+
.single();
9995

10096
// Error handling
101-
if (error) {
102-
ErrorHandler.handleSupabaseError(error)
103-
return null
97+
if (error || !data) {
98+
ErrorHandler.handleSupabaseError(error);
99+
return null;
104100
}
105101

106-
if (!data)
107-
return null
108-
109-
// Type conversion: DBUser -> User
110-
const user : User = UserService.parseUser(data)
102+
const user = UserService.parseUser(data);
111103

112-
return user
104+
return user;
113105
}
114106

115107
/**
@@ -122,20 +114,27 @@ export default class UserController {
122114
* (user: User) => { ... }
123115
* )
124116
*
125-
* @param {UserSignupData} userSignupData - The user data to create
117+
* @param {UserFromPortal} userPortal - The user data access from NCU Portal
126118
*
127119
* @returns {User} - The created user entity
128120
*
129121
* @author Boyiliu (@boyiliu1007)
122+
* @author Susan Chen(@1989ONCE)(refactor)
130123
*/
131124

132-
public async createUser(userSignupData: UserSignupData): Promise<User | null> {
125+
public async createUser(userPortal: UserFromPortal, pwd: string, username: string): Promise<User | null> {
133126

134127
// should signup in users table at auth schema
135128
const { data, error } = await supabase.auth.signUp({
136-
email: userSignupData.email,
137-
password: userSignupData.password,
129+
email: userPortal.email,
130+
password: pwd,
138131
})
132+
133+
if (pwd.length < 8) {
134+
console.error("Password too short");
135+
return null;
136+
}
137+
139138
if (error) {
140139
// handle auth error cannot use handleSupabaseError
141140
console.log(error.name);
@@ -147,20 +146,18 @@ export default class UserController {
147146
// get user id from users table at auth schema
148147
const userID = data.user?.id
149148

149+
if (!userID) {
150+
console.error("User ID is undefined");
151+
return null;
152+
}
153+
154+
// convert user portal info to DBUser to fit memebers' schema
155+
const dbData : DBUser = UserService.convertSignupToDB(userPortal, userID, username)
156+
150157
// insert into members table at public schema
151158
const { error: insertMemberError } = await supabase
152159
.from(USER_TABLE_NAME)
153-
.insert(
154-
{
155-
uuid: userID,
156-
name: userSignupData.name,
157-
email: userSignupData.email,
158-
username: userSignupData.username,
159-
studentId: userSignupData.studentId,
160-
identity: 2,
161-
created_at: new Date().toISOString(),
162-
}
163-
)
160+
.insert(dbData)
164161
.single();
165162
if (insertMemberError) {
166163
console.log("Error inserting member:", insertMemberError);
@@ -183,6 +180,7 @@ export default class UserController {
183180
}
184181

185182
console.log("User created successfully:", memberData);
183+
alert("註冊成功!請使用剛剛註冊的信箱及密碼登入!")
186184

187185
// NOTE: Signing out immediately after signup may be unexpected—
188186
// please confirm if automatic sign-in is not desired.
@@ -194,4 +192,68 @@ export default class UserController {
194192

195193
return UserService.parseUser(memberData);
196194
}
195+
196+
/**
197+
* Get the Current User, return the user information from members table by uuid
198+
*
199+
* @usage userController.getCurrentUser().then(
200+
* (user: User) => { ... }
201+
* )
202+
*
203+
* @returns {User} - The created user entity
204+
*
205+
* @author Susan Chen(@1989ONCE)
206+
*/
207+
208+
public async getCurrentUser(): Promise<User | null> {
209+
const { data: { user } } = await supabase.auth.getUser()
210+
211+
if (!user) {
212+
return null
213+
}
214+
const userID = user.id
215+
const { data, error } = await supabase
216+
.from(USER_TABLE_NAME)
217+
.select("*")
218+
.eq("uuid", userID)
219+
.single()
220+
if (error) {
221+
console.error("Error retrieving member data:", error);
222+
ErrorHandler.handleSupabaseError(error);
223+
return null;
224+
}
225+
226+
if (!data) {
227+
console.error("User data not found");
228+
return null;
229+
}
230+
231+
const userData : User | null = await this.findUserByID(userID)
232+
if (!userData) {
233+
console.error("User not found");
234+
return null;
235+
}
236+
237+
return userData
238+
239+
}
240+
241+
public async updateUser(userID: string, userData: Partial<User>): Promise<User | null> {
242+
const { data, error } = await supabase
243+
.from(USER_TABLE_NAME)
244+
.update(userData)
245+
.eq("uuid", userID)
246+
.returns<DBUser>()
247+
.single()
248+
249+
if (error) {
250+
ErrorHandler.handleSupabaseError(error)
251+
return null
252+
}
253+
254+
if (!data)
255+
return null
256+
257+
return UserService.parseUser(data)
258+
}
197259
}

src/backend/user/Entities/User.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ export default class User {
2020
public avatar: string = ""
2121
public profileBackground: string = ""
2222
public joinedAt: Date = new Date()
23-
public identity: number = 0
23+
public identity: number = 2
2424
public department: string = ""
25-
public grade: string = ""
25+
public grade: number = 1
2626
public bio: string = ""
27+
public studentId: string = ""
28+
public grad_time: string = ""
2729

2830

2931
public convertIdentity(): string {
3032
switch (this.identity) {
31-
case 1: return "管理員"
32-
case 2: return "學生"
33-
case 3: return "校友"
34-
case 4: return "教職員"
35-
default: return "用戶"
33+
case 1: return "開發團隊"
34+
case 2: return "Verified"
35+
default: return "未驗證"
3636
}
3737
}
3838
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
export default class UserFromPortal {
3+
public chineseName: string = '';
4+
public email: string = '';
5+
public studentId: string = '';
6+
public academyRecords: { name: string; grad: string; studySystemNo: string } = { name: '', grad: '', studySystemNo: ''};
7+
8+
constructor(userInfo: { chineseName: string; email: string; studentId: string; academyRecords: { name: string; grad: string, studySystemNo: string } }) {
9+
this.chineseName = userInfo.chineseName;
10+
this.email = userInfo.email;
11+
this.studentId = userInfo.studentId;
12+
this.academyRecords = userInfo.academyRecords;
13+
}
14+
}

src/backend/user/Entities/UserSignupData.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)