2024-04-22 02:01:14 +01:00
|
|
|
import { Console } from "hsconsole";
|
2024-09-19 00:41:40 +01:00
|
|
|
import User from "../entities/User";
|
2024-04-22 02:01:14 +01:00
|
|
|
import PartyRepo from "../repos/PartyRepo";
|
|
|
|
import UserRepo from "../repos/UserRepo";
|
|
|
|
import PasswordUtility from "../utilities/PasswordUtility";
|
2024-09-19 00:41:40 +01:00
|
|
|
import Party from "../entities/Party";
|
|
|
|
import UserParty from "../entities/UserParty";
|
2024-04-23 17:01:25 +01:00
|
|
|
import UserPartyRepo from "../repos/UserPartyRepo";
|
2024-04-22 02:01:14 +01:00
|
|
|
|
|
|
|
export default class UserService {
|
2024-09-19 00:41:40 +01:00
|
|
|
public static async AuthenticateUser(username:string, password:string) {
|
|
|
|
try {
|
|
|
|
const user = await UserRepo.selectByUsername(username);
|
|
|
|
if (!user) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (await PasswordUtility.ValidatePassword(user.PasswordHash, user.PasswordSalt, password)) {
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
} catch (e) {
|
|
|
|
Console.printError(`MultiProbe server service error:\n${e}`);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-22 02:01:14 +01:00
|
|
|
public static async GetUser(id:number) {
|
|
|
|
try {
|
|
|
|
return await UserRepo.selectById(id);
|
|
|
|
} catch (e) {
|
|
|
|
Console.printError(`MultiProbe server service error:\n${e}`);
|
2024-04-23 17:01:25 +01:00
|
|
|
throw e;
|
2024-04-22 02:01:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 00:58:07 +01:00
|
|
|
public static async GetUserByUsername(username:string) {
|
|
|
|
try {
|
|
|
|
return await UserRepo.selectByUsername(username);
|
|
|
|
} catch (e) {
|
|
|
|
Console.printError(`MultiProbe server service error:\n${e}`);
|
2024-04-23 17:01:25 +01:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-25 02:37:37 +01:00
|
|
|
public static async GetUserByAPIKey(apiKey:string) {
|
|
|
|
try {
|
|
|
|
return await UserRepo.selectByAPIKey(apiKey);
|
|
|
|
} catch (e) {
|
|
|
|
Console.printError(`MultiProbe server service error:\n${e}`);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 17:01:25 +01:00
|
|
|
public static async GetUserParties(userId:number) {
|
|
|
|
try {
|
|
|
|
return await PartyRepo.selectByUserId(userId);
|
|
|
|
} catch (e) {
|
|
|
|
Console.printError(`MultiProbe server service error:\n${e}`);
|
|
|
|
throw e;
|
2024-04-23 00:58:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 00:05:57 +01:00
|
|
|
public static async GetUserPartyForUser(userId:number, partyId:number) {
|
|
|
|
try {
|
|
|
|
return await UserPartyRepo.selectByUserIdPartyId(userId, partyId);
|
|
|
|
} catch (e) {
|
|
|
|
Console.printError(`MultiProbe server service error:\n${e}`);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-22 02:01:14 +01:00
|
|
|
public static async GetParty(id:number) {
|
|
|
|
try {
|
|
|
|
return await PartyRepo.selectById(id);
|
|
|
|
} catch (e) {
|
|
|
|
Console.printError(`MultiProbe server service error:\n${e}`);
|
2024-04-23 17:01:25 +01:00
|
|
|
throw e;
|
2024-04-22 02:01:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async GetPartyByPartyRef(partyRef:string) {
|
|
|
|
try {
|
|
|
|
return await PartyRepo.selectByPartyRef(partyRef);
|
|
|
|
} catch (e) {
|
|
|
|
Console.printError(`MultiProbe server service error:\n${e}`);
|
2024-04-23 17:01:25 +01:00
|
|
|
throw e;
|
2024-04-22 02:01:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async CreateUser(currentUserId:number, username:string, password:string) {
|
|
|
|
try {
|
|
|
|
const user = new User();
|
|
|
|
user.Username = username;
|
|
|
|
user.PasswordSalt = PasswordUtility.GenerateSalt();
|
|
|
|
user.PasswordHash = await PasswordUtility.HashPassword(user.PasswordSalt, password);
|
|
|
|
user.CreatedByUserId = currentUserId;
|
|
|
|
user.CreatedDatetime = new Date();
|
|
|
|
|
|
|
|
await UserRepo.insertUpdate(user);
|
|
|
|
} catch (e) {
|
|
|
|
Console.printError(`MultiProbe server service error:\n${e}`);
|
2024-04-23 17:01:25 +01:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async CreateParty(currentUserId:number, name:string, partyRef:string) {
|
|
|
|
try {
|
|
|
|
const party = new Party();
|
|
|
|
party.Name = name;
|
|
|
|
party.PartyRef = partyRef;
|
|
|
|
party.CreatedByUserId = currentUserId;
|
|
|
|
party.CreatedDatetime = new Date();
|
|
|
|
|
|
|
|
await PartyRepo.insertUpdate(party);
|
|
|
|
|
|
|
|
const newParty = await PartyRepo.selectByPartyRef(partyRef);
|
|
|
|
if (!newParty) {
|
|
|
|
throw "This shouldn't happen";
|
|
|
|
}
|
|
|
|
|
|
|
|
const userParty = new UserParty();
|
|
|
|
userParty.UserId = currentUserId;
|
|
|
|
userParty.PartyId = newParty.Id;
|
|
|
|
userParty.CreatedByUserId = currentUserId;
|
|
|
|
userParty.CreatedDatetime = new Date();
|
|
|
|
|
|
|
|
await UserPartyRepo.insertUpdate(userParty);
|
|
|
|
} catch (e) {
|
|
|
|
Console.printError(`MultiProbe server service error:\n${e}`);
|
|
|
|
throw e;
|
2024-04-22 02:01:14 +01:00
|
|
|
}
|
|
|
|
}
|
2024-04-24 00:05:57 +01:00
|
|
|
|
|
|
|
public static async AddUserToParty(userId:number, partyId:number) {
|
|
|
|
try {
|
|
|
|
const userParty = new UserParty();
|
|
|
|
userParty.UserId = userId;
|
|
|
|
userParty.PartyId = partyId;
|
|
|
|
userParty.CreatedByUserId = userId;
|
|
|
|
userParty.CreatedDatetime = new Date();
|
|
|
|
|
|
|
|
await UserPartyRepo.insertUpdate(userParty);
|
|
|
|
} catch (e) {
|
|
|
|
Console.printError(`MultiProbe server service error:\n${e}`);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async SetActiveParty(currentUserId:number, partyId:number) {
|
|
|
|
try {
|
|
|
|
await UserPartyRepo.deactivateAll(currentUserId);
|
|
|
|
|
|
|
|
const userParty = await UserPartyRepo.selectByUserIdPartyId(currentUserId, partyId);
|
|
|
|
if (!userParty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
userParty.IsActive = true;
|
|
|
|
userParty.LastModifiedByUserId = currentUserId;
|
|
|
|
userParty.LastModifiedDatetime = new Date();
|
|
|
|
|
|
|
|
await UserPartyRepo.insertUpdate(userParty);
|
|
|
|
} catch (e) {
|
|
|
|
Console.printError(`MultiProbe server service error:\n${e}`);
|
|
|
|
console.log(e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async DeactivateCurrentParty(currentUserId:number) {
|
|
|
|
try {
|
|
|
|
await UserPartyRepo.deactivateAll(currentUserId);
|
|
|
|
} catch (e) {
|
|
|
|
Console.printError(`MultiProbe server service error:\n${e}`);
|
|
|
|
console.log(e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async GetActiveParty(currentUserId:number) {
|
|
|
|
try {
|
|
|
|
return await UserPartyRepo.selectActive(currentUserId);
|
|
|
|
} catch (e) {
|
|
|
|
Console.printError(`MultiProbe server service error:\n${e}`);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2024-09-19 00:41:40 +01:00
|
|
|
|
|
|
|
public static async LeaveParty(currentUserId:number, partyId:number) {
|
|
|
|
try {
|
|
|
|
const userParty = await UserPartyRepo.selectByUserIdPartyId(currentUserId, partyId);
|
|
|
|
if (!userParty) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
userParty.DeletedByUserId = currentUserId;
|
|
|
|
userParty.DeletedDatetime = new Date();
|
|
|
|
userParty.IsDeleted = true;
|
|
|
|
|
|
|
|
await UserPartyRepo.insertUpdate(userParty);
|
|
|
|
|
|
|
|
return userParty;
|
|
|
|
} catch (e) {
|
|
|
|
Console.printError(`MultiProbe server service error:\n${e}`);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2024-04-22 02:01:14 +01:00
|
|
|
}
|