import Database from "../objects/Database"; import UserParty from "../entities/UserParty"; import RepoBase from "./RepoBase"; export default class UserPartyRepo { public static async selectById(id:number) { const dbUserParty = await Database.Instance.query("SELECT * FROM UserParty WHERE Id = ? AND IsDeleted = 0 LIMIT 1", [id]); if (dbUserParty == null || dbUserParty.length === 0) { return null; } else { const userParty = new UserParty(); populateUserPartyFromDB(userParty, dbUserParty[0]); return userParty; } } public static async selectByUserId(userId:number) { const dbUserParties = await Database.Instance.query("SELECT * FROM UserParty WHERE UserId = ? AND IsDeleted = 0", [userId]); if (dbUserParties == null || dbUserParties.length === 0) { return null; } else { const userParties = new Array(); for (const dbUserParty of dbUserParties) { const userParty = new UserParty(); populateUserPartyFromDB(userParty, dbUserParty[0]); userParties.push(userParty); } return userParties; } } public static async selectByUserIdPartyId(userId:number, partyId:number) { const dbUserParty = await Database.Instance.query("SELECT * FROM UserParty WHERE UserId = ? AND PartyId = ? AND IsDeleted = 0", [userId, partyId]); if (dbUserParty == null || dbUserParty.length === 0) { return null; } else { const userParty = new UserParty(); populateUserPartyFromDB(userParty, dbUserParty[0]); return userParty; } } public static async deactivateAll(userId:number) { await Database.Instance.query("UPDATE UserParty SET IsActive = 0, LastModifiedByUserId = ?, LastModifiedDatetime = ? WHERE UserId = ? AND IsActive = 1", [ userId, Date.now(), userId ]); } public static async selectActive(userId:number) { const dbUserParty = await Database.Instance.query("SELECT * FROM UserParty WHERE UserId = ? AND IsActive = 1 AND IsDeleted = 0 LIMIT 1", [userId]); if (dbUserParty == null || dbUserParty.length === 0) { return null; } else { const userParty = new UserParty(); populateUserPartyFromDB(userParty, dbUserParty[0]); return userParty; } } public static async insertUpdate(userParty:UserParty) { if (userParty.Id === Number.MIN_VALUE) { userParty.Id = (await Database.Instance.query("INSERT UserParty (UserId, PartyId, IsActive, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING Id;", [ userParty.UserId, userParty.PartyId, Number(userParty.IsActive), userParty.CreatedByUserId, userParty.CreatedDatetime.getTime(), userParty.LastModifiedByUserId ?? null, userParty.LastModifiedDatetime?.getTime() ?? null, userParty.DeletedByUserId ?? null, userParty.DeletedDatetime?.getTime() ?? null, Number(userParty.IsDeleted) ]))[0]["Id"]; } else { await Database.Instance.query(`UPDATE UserParty SET UserId = ?, PartyId = ?, IsActive = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ? WHERE Id = ?`, [ userParty.UserId, userParty.PartyId, Number(userParty.IsActive), userParty.CreatedByUserId, userParty.CreatedDatetime.getTime(), userParty.LastModifiedByUserId ?? null, userParty.LastModifiedDatetime?.getTime() ?? null, userParty.DeletedByUserId ?? null, userParty.DeletedDatetime?.getTime() ?? null, Number(userParty.IsDeleted), userParty.Id ]); } return userParty; } } function populateUserPartyFromDB(userParty:UserParty, dbUserParty:any) { userParty.Id = dbUserParty.Id; userParty.UserId = dbUserParty.UserId; userParty.PartyId = dbUserParty.PartyId; userParty.IsActive = dbUserParty.IsActive; userParty.CreatedByUserId = dbUserParty.CreatedByUserId; userParty.CreatedDatetime = new Date(dbUserParty.CreatedDatetime); userParty.LastModifiedByUserId = dbUserParty.LastModifiedByUserId; userParty.LastModifiedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbUserParty.LastModifiedDatetime); userParty.DeletedByUserId = dbUserParty.DeletedByUserId; userParty.DeletedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbUserParty.DeletedDatetime); userParty.IsDeleted = dbUserParty.IsDeleted[0] === 1; }