Skip to content

Commit 9b0ec98

Browse files
authored
chore: fix group issue (#314)
1 parent 6bea83f commit 9b0ec98

File tree

3 files changed

+64
-25
lines changed

3 files changed

+64
-25
lines changed

β€Žplatforms/evoting-api/src/services/PollService.tsβ€Ž

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,18 @@ export class PollService {
2929
// Get user's group memberships if userId is provided
3030
let userGroupIds: string[] = [];
3131
if (userId) {
32-
const user = await this.userRepository.findOne({
33-
where: { id: userId },
34-
relations: ['followers', 'following'] // This will include group memberships
35-
});
32+
// Get groups where user is a member, admin, or participant
33+
const groupRepository = AppDataSource.getRepository(Group);
34+
const userGroups = await groupRepository
35+
.createQueryBuilder('group')
36+
.leftJoin('group.members', 'member')
37+
.leftJoin('group.admins', 'admin')
38+
.leftJoin('group.participants', 'participant')
39+
.where('member.id = :userId OR admin.id = :userId OR participant.id = :userId', { userId })
40+
.getMany();
3641

37-
if (user) {
38-
// Get groups where user is a member, admin, or participant
39-
const groupRepository = AppDataSource.getRepository(Group);
40-
const userGroups = await groupRepository
41-
.createQueryBuilder('group')
42-
.leftJoin('group.members', 'member')
43-
.leftJoin('group.admins', 'admin')
44-
.leftJoin('group.participants', 'participant')
45-
.where('member.id = :userId OR admin.id = :userId OR participant.id = :userId', { userId })
46-
.getMany();
47-
48-
userGroupIds = userGroups.map(group => group.id);
49-
}
42+
userGroupIds = userGroups.map(group => group.id);
43+
console.log(`User ${userId} is member/admin/participant in groups:`, userGroupIds);
5044
}
5145

5246
const [allPolls, total] = await this.pollRepository.findAndCount({

β€Žplatforms/group-charter-manager-api/src/controllers/AuthController.tsβ€Ž

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class AuthController {
6161
}
6262

6363
const { user, token } =
64-
await this.userService.findOrCreateUser(ename);
64+
await this.userService.findUserByEname(ename);
6565

6666
const data = {
6767
user: {
@@ -76,6 +76,18 @@ export class AuthController {
7676
res.status(200).send();
7777
} catch (error) {
7878
console.error("Error during login:", error);
79+
80+
// Provide more specific error messages
81+
if (error instanceof Error) {
82+
if (error.message.includes("not found")) {
83+
return res.status(404).json({
84+
error: "User not found",
85+
message: error.message,
86+
details: "Please ensure you have the correct ename or contact support to create an account."
87+
});
88+
}
89+
}
90+
7991
res.status(500).json({ error: "Internal server error" });
8092
}
8193
};

β€Žplatforms/group-charter-manager-api/src/services/UserService.tsβ€Ž

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,54 @@ export class UserService {
2121
return await this.userRepository.save(user);
2222
}
2323

24-
async findOrCreateUser(
24+
async findUserByEname(
2525
ename: string
2626
): Promise<{ user: User; token: string }> {
27-
// Strip @ prefix from ename if present (database stores without @)
28-
const cleanEname = ename.startsWith('@') ? ename.slice(1) : ename;
27+
let user: User | null = null;
2928

30-
let user = await this.userRepository.findOne({
31-
where: { ename: cleanEname },
29+
console.log(`πŸ” Looking for user with ename: '${ename}'`);
30+
31+
// Try to find user with the exact ename as provided
32+
user = await this.userRepository.findOne({
33+
where: { ename: ename },
3234
});
33-
35+
36+
if (user) {
37+
console.log(`βœ… Found user with exact ename: '${ename}'`);
38+
} else {
39+
// If not found and ename starts with @, try without @
40+
if (ename.startsWith('@')) {
41+
const enameWithoutAt = ename.slice(1);
42+
console.log(`πŸ” Trying without @ prefix: '${enameWithoutAt}'`);
43+
user = await this.userRepository.findOne({
44+
where: { ename: enameWithoutAt },
45+
});
46+
if (user) {
47+
console.log(`βœ… Found user without @ prefix: '${enameWithoutAt}'`);
48+
}
49+
}
50+
51+
// If not found and ename doesn't start with @, try with @
52+
if (!user && !ename.startsWith('@')) {
53+
const enameWithAt = `@${ename}`;
54+
console.log(`πŸ” Trying with @ prefix: '${enameWithAt}'`);
55+
user = await this.userRepository.findOne({
56+
where: { ename: enameWithAt },
57+
});
58+
if (user) {
59+
console.log(`βœ… Found user with @ prefix: '${enameWithAt}'`);
60+
}
61+
}
62+
}
63+
64+
// If still no user found, throw an error - never create new users
3465
if (!user) {
35-
user = await this.createBlankUser(cleanEname);
66+
console.log(`❌ No user found for ename: '${ename}' (tried with/without @ prefix)`);
67+
throw new Error(`User with ename '${ename}' not found. Cannot create new users automatically.`);
3668
}
3769

3870
const token = signToken({ userId: user.id });
71+
console.log(`πŸŽ‰ Successfully authenticated user: ${user.ename} (ID: ${user.id})`);
3972
return { user, token };
4073
}
4174

0 commit comments

Comments
Β (0)