t00-multiuser/server/services/UserService.ts

272 lines
No EOL
6.9 KiB
TypeScript

import { Console } from "hsconsole";
import User from "../entities/User";
import UserRepo from "../repos/UserRepo";
import PasswordUtility from "../utilities/PasswordUtility";
import UserParty from "../entities/UserParty";
import UserPartyRepo from "../repos/UserPartyRepo";
import { UserLevel } from "../enums/UserLevel";
import UserBadgeRepo from "../repos/UserBadgeRepo";
import UserBadge from "../entities/UserBadge";
export default class UserService {
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;
}
}
public static async GetUser(id:number) {
try {
return await UserRepo.selectById(id);
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
public static async GetAll() {
try {
return await UserRepo.selectAll();
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
public static async GetUserByUsername(username:string) {
try {
return await UserRepo.selectByUsername(username);
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
public static async GetUserByAPIKey(apiKey:string) {
try {
return await UserRepo.selectByAPIKey(apiKey);
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
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;
}
}
public static async CreateUser(currentUserId:number, username:string, password:string) {
try {
const existingCheck = await UserRepo.selectByUsername(username);
if (existingCheck) {
return null;
}
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);
return user;
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
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}`);
throw e;
}
}
public static async DeactivateCurrentParty(currentUserId:number) {
try {
await UserPartyRepo.deactivateAll(currentUserId);
} catch (e) {
Console.printError(`MultiProbe server service error:\n${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;
}
}
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;
}
}
public static async SaveUsername(currentUserId:number, username:string) {
try {
const existingCheck = await UserRepo.selectByUsername(username)
if (existingCheck) {
return null;
}
const user = await UserRepo.selectById(currentUserId);
if (!user) {
return null;
}
user.LastModifiedByUserId = currentUserId;
user.LastModifiedDatetime = new Date();
user.Username = username;
await UserRepo.insertUpdate(user);
return user;
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
public static async GetUserCount() {
try {
return await UserRepo.selectUserCount();
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
public static async SaveUser(currentUserId:number, id:number | undefined, username:string, userLevel:UserLevel) {
try {
const existingCheck = id ? null : await UserRepo.selectByUsername(username);
if (existingCheck) {
return null;
}
let user = id ? await UserRepo.selectById(id) : null;
if (!user) {
user = new User();
user.CreatedByUserId = currentUserId;
user.CreatedDatetime = new Date();
} else {
user.LastModifiedByUserId = currentUserId;
user.LastModifiedDatetime = new Date();
}
user.Username = username;
user.UserLevel = userLevel;
return await UserRepo.insertUpdate(user);
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
public static async UnlockBadgeIfNotUnlocked(currentUserId:number, badgeId:number) {
try {
let userBadge = await UserBadgeRepo.selectByUserIdBadgeId(currentUserId, badgeId);
if (userBadge) {
return false;
}
userBadge = new UserBadge();
userBadge.UserId = currentUserId;
userBadge.BadgeId = badgeId;
userBadge.CreatedByUserId = currentUserId;
userBadge.CreatedDatetime = new Date();
await UserBadgeRepo.insertUpdate(userBadge)
return true;
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
public static async SaveUserClientLoginFlag(currentUserId:number) {
try {
const user = await UserRepo.selectById(currentUserId);
if (!user) {
return null;
}
user.HasUsedClient = true;
user.LastModifiedByUserId = currentUserId;
user.LastModifiedDatetime = new Date();
return await UserRepo.insertUpdate(user);
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
}