Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions platforms/evoting-api/src/services/PollService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,18 @@ export class PollService {
// Get user's group memberships if userId is provided
let userGroupIds: string[] = [];
if (userId) {
const user = await this.userRepository.findOne({
where: { id: userId },
relations: ['followers', 'following'] // This will include group memberships
});
// Get groups where user is a member, admin, or participant
const groupRepository = AppDataSource.getRepository(Group);
const userGroups = await groupRepository
.createQueryBuilder('group')
.leftJoin('group.members', 'member')
.leftJoin('group.admins', 'admin')
.leftJoin('group.participants', 'participant')
.where('member.id = :userId OR admin.id = :userId OR participant.id = :userId', { userId })
.getMany();

if (user) {
// Get groups where user is a member, admin, or participant
const groupRepository = AppDataSource.getRepository(Group);
const userGroups = await groupRepository
.createQueryBuilder('group')
.leftJoin('group.members', 'member')
.leftJoin('group.admins', 'admin')
.leftJoin('group.participants', 'participant')
.where('member.id = :userId OR admin.id = :userId OR participant.id = :userId', { userId })
.getMany();

userGroupIds = userGroups.map(group => group.id);
}
userGroupIds = userGroups.map(group => group.id);
console.log(`User ${userId} is member/admin/participant in groups:`, userGroupIds);
}

const [allPolls, total] = await this.pollRepository.findAndCount({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class AuthController {
}

const { user, token } =
await this.userService.findOrCreateUser(ename);
await this.userService.findUserByEname(ename);

const data = {
user: {
Expand All @@ -76,6 +76,18 @@ export class AuthController {
res.status(200).send();
} catch (error) {
console.error("Error during login:", error);

// Provide more specific error messages
if (error instanceof Error) {
if (error.message.includes("not found")) {
return res.status(404).json({
error: "User not found",
message: error.message,
details: "Please ensure you have the correct ename or contact support to create an account."
});
}
}

res.status(500).json({ error: "Internal server error" });
}
};
Expand Down
47 changes: 40 additions & 7 deletions platforms/group-charter-manager-api/src/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,54 @@ export class UserService {
return await this.userRepository.save(user);
}

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

let user = await this.userRepository.findOne({
where: { ename: cleanEname },
console.log(`🔍 Looking for user with ename: '${ename}'`);

// Try to find user with the exact ename as provided
user = await this.userRepository.findOne({
where: { ename: ename },
});


if (user) {
console.log(`✅ Found user with exact ename: '${ename}'`);
} else {
// If not found and ename starts with @, try without @
if (ename.startsWith('@')) {
const enameWithoutAt = ename.slice(1);
console.log(`🔍 Trying without @ prefix: '${enameWithoutAt}'`);
user = await this.userRepository.findOne({
where: { ename: enameWithoutAt },
});
if (user) {
console.log(`✅ Found user without @ prefix: '${enameWithoutAt}'`);
}
}

// If not found and ename doesn't start with @, try with @
if (!user && !ename.startsWith('@')) {
const enameWithAt = `@${ename}`;
console.log(`🔍 Trying with @ prefix: '${enameWithAt}'`);
user = await this.userRepository.findOne({
where: { ename: enameWithAt },
});
if (user) {
console.log(`✅ Found user with @ prefix: '${enameWithAt}'`);
}
}
}

// If still no user found, throw an error - never create new users
if (!user) {
user = await this.createBlankUser(cleanEname);
console.log(`❌ No user found for ename: '${ename}' (tried with/without @ prefix)`);
throw new Error(`User with ename '${ename}' not found. Cannot create new users automatically.`);
}

const token = signToken({ userId: user.id });
console.log(`🎉 Successfully authenticated user: ${user.ename} (ID: ${user.id})`);
return { user, token };
}

Expand Down
Loading