2019-11-23 14:11:33 +00:00
|
|
|
import { User, UserPageStatus } from "../entities/User";
|
|
|
|
import { DatabaseHelper } from "./DatabaseHelper";
|
2019-11-23 15:10:51 +00:00
|
|
|
import { AccountImageHelper } from "./AccountImageHelper";
|
2019-11-23 14:11:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* User helper
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
const TABLE_NAME = "utilisateurs";
|
|
|
|
|
|
|
|
export class UserHelper {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get information a single user
|
|
|
|
*
|
|
|
|
* @param id The ID of the user to get
|
|
|
|
* @returns Information about the user | null if not found
|
|
|
|
*/
|
|
|
|
public static async GetUserInfo(id: number) : Promise<User|null> {
|
|
|
|
const result = await DatabaseHelper.QueryRow({
|
|
|
|
table: TABLE_NAME,
|
|
|
|
where: {
|
|
|
|
ID: id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!result)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return this.DbToUser(result);
|
|
|
|
}
|
|
|
|
|
2019-11-30 09:11:51 +00:00
|
|
|
/**
|
|
|
|
* Check out whether a user exists or not
|
|
|
|
*
|
|
|
|
* @param id The ID of the user to check
|
|
|
|
*/
|
|
|
|
public static async Exists(id: number) : Promise<boolean> {
|
|
|
|
return await DatabaseHelper.Count({
|
|
|
|
table: TABLE_NAME,
|
|
|
|
where: {
|
|
|
|
ID: id
|
|
|
|
}
|
|
|
|
}) > 0;
|
|
|
|
}
|
|
|
|
|
2019-11-30 08:28:50 +00:00
|
|
|
/**
|
|
|
|
* Search for user in the database
|
|
|
|
*
|
|
|
|
* @param query The query
|
|
|
|
* @param limit Limit for the request (default: 10)
|
|
|
|
* @returns The list of ids of the user
|
|
|
|
*/
|
|
|
|
public static async SearchUser(query: string, limit: number = 10) : Promise<Array<number>> {
|
|
|
|
|
|
|
|
query = "%" + query.replace(/\ /g, "%") + "%";
|
|
|
|
|
|
|
|
const request = await DatabaseHelper.Query({
|
|
|
|
fields: ["ID"],
|
|
|
|
table: TABLE_NAME,
|
|
|
|
customWhere: "(nom LIKE ?) || (prenom LIKE ?) || (CONCAT(prenom, '%', nom) LIKE ?) || (CONCAT(nom, '%', prenom) LIKE ?)",
|
|
|
|
customWhereArgs: [query, query, query, query],
|
|
|
|
limit: limit,
|
|
|
|
});
|
|
|
|
|
|
|
|
return request.map((e) => e.ID);
|
|
|
|
}
|
|
|
|
|
2019-11-23 14:11:33 +00:00
|
|
|
|
2019-11-23 15:10:51 +00:00
|
|
|
private static async DbToUser(row: any) : Promise<User> {
|
2019-11-23 14:11:33 +00:00
|
|
|
return new User({
|
|
|
|
id: row.ID,
|
|
|
|
firstName: row.prenom,
|
|
|
|
lastName: row.nom,
|
|
|
|
timeCreate: new Date(row.date_creation).getTime()/1000,
|
|
|
|
virtualDirectory: row.sous_repertoire,
|
2019-11-23 15:10:51 +00:00
|
|
|
pageStatus: row.pageouverte == 1 ? UserPageStatus.OPEN : (row.public == 1 ? UserPageStatus.PUBLIC : UserPageStatus.PRIVATE),
|
|
|
|
accountImage: await AccountImageHelper.Get(row.ID)
|
2019-11-23 14:11:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|