645 lines
16 KiB
JavaScript
645 lines
16 KiB
JavaScript
|
|
const db = require('../models');
|
|
const FileDBApi = require('./file');
|
|
const crypto = require('crypto');
|
|
const Utils = require('../utils');
|
|
|
|
|
|
|
|
const Sequelize = db.Sequelize;
|
|
const Op = Sequelize.Op;
|
|
|
|
module.exports = class PlayersDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const players = await db.players.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
gamer_tag: data.gamer_tag
|
|
||
|
|
null
|
|
,
|
|
|
|
real_name: data.real_name
|
|
||
|
|
null
|
|
,
|
|
|
|
dob: data.dob
|
|
||
|
|
null
|
|
,
|
|
|
|
position: data.position
|
|
||
|
|
null
|
|
,
|
|
|
|
country: data.country
|
|
||
|
|
null
|
|
,
|
|
|
|
pro: data.pro
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
rating: data.rating
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await players.setOrganizations( data.organizations || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.players.getTableName(),
|
|
belongsToColumn: 'avatar',
|
|
belongsToId: players.id,
|
|
},
|
|
data.avatar,
|
|
options,
|
|
);
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.players.getTableName(),
|
|
belongsToColumn: 'portfolio',
|
|
belongsToId: players.id,
|
|
},
|
|
data.portfolio,
|
|
options,
|
|
);
|
|
|
|
|
|
return players;
|
|
}
|
|
|
|
|
|
static async bulkImport(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
// Prepare data - wrapping individual data transformations in a map() method
|
|
const playersData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
gamer_tag: item.gamer_tag
|
|
||
|
|
null
|
|
,
|
|
|
|
real_name: item.real_name
|
|
||
|
|
null
|
|
,
|
|
|
|
dob: item.dob
|
|
||
|
|
null
|
|
,
|
|
|
|
position: item.position
|
|
||
|
|
null
|
|
,
|
|
|
|
country: item.country
|
|
||
|
|
null
|
|
,
|
|
|
|
pro: item.pro
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
rating: item.rating
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const players = await db.players.bulkCreate(playersData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < players.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.players.getTableName(),
|
|
belongsToColumn: 'avatar',
|
|
belongsToId: players[i].id,
|
|
},
|
|
data[i].avatar,
|
|
options,
|
|
);
|
|
}
|
|
|
|
for (let i = 0; i < players.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.players.getTableName(),
|
|
belongsToColumn: 'portfolio',
|
|
belongsToId: players[i].id,
|
|
},
|
|
data[i].portfolio,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return players;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
const globalAccess = currentUser.app_role?.globalAccess;
|
|
|
|
const players = await db.players.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.gamer_tag !== undefined) updatePayload.gamer_tag = data.gamer_tag;
|
|
|
|
|
|
if (data.real_name !== undefined) updatePayload.real_name = data.real_name;
|
|
|
|
|
|
if (data.dob !== undefined) updatePayload.dob = data.dob;
|
|
|
|
|
|
if (data.position !== undefined) updatePayload.position = data.position;
|
|
|
|
|
|
if (data.country !== undefined) updatePayload.country = data.country;
|
|
|
|
|
|
if (data.pro !== undefined) updatePayload.pro = data.pro;
|
|
|
|
|
|
if (data.rating !== undefined) updatePayload.rating = data.rating;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await players.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.organizations !== undefined) {
|
|
await players.setOrganizations(
|
|
|
|
data.organizations,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.players.getTableName(),
|
|
belongsToColumn: 'avatar',
|
|
belongsToId: players.id,
|
|
},
|
|
data.avatar,
|
|
options,
|
|
);
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.players.getTableName(),
|
|
belongsToColumn: 'portfolio',
|
|
belongsToId: players.id,
|
|
},
|
|
data.portfolio,
|
|
options,
|
|
);
|
|
|
|
|
|
return players;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const players = await db.players.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of players) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of players) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return players;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const players = await db.players.findByPk(id, options);
|
|
|
|
await players.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await players.destroy({
|
|
transaction
|
|
});
|
|
|
|
return players;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const players = await db.players.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!players) {
|
|
return players;
|
|
}
|
|
|
|
const output = players.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.stats_player = await players.getStats_player({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.avatar = await players.getAvatar({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.portfolio = await players.getPortfolio({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.organizations = await players.getOrganizations({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
globalAccess, options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
const user = (options && options.currentUser) || null;
|
|
const userOrganizations = (user && user.organizations?.id) || null;
|
|
|
|
|
|
|
|
if (userOrganizations) {
|
|
if (options?.currentUser?.organizationsId) {
|
|
where.organizationsId = options.currentUser.organizationsId;
|
|
}
|
|
}
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.organizations,
|
|
as: 'organizations',
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'avatar',
|
|
},
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'portfolio',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.gamer_tag) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'players',
|
|
'gamer_tag',
|
|
filter.gamer_tag,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.real_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'players',
|
|
'real_name',
|
|
filter.real_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.country) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'players',
|
|
'country',
|
|
filter.country,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.dobRange) {
|
|
const [start, end] = filter.dobRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
dob: {
|
|
...where.dob,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
dob: {
|
|
...where.dob,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.ratingRange) {
|
|
const [start, end] = filter.ratingRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
rating: {
|
|
...where.rating,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
rating: {
|
|
...where.rating,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.position) {
|
|
where = {
|
|
...where,
|
|
position: filter.position,
|
|
};
|
|
}
|
|
|
|
if (filter.pro) {
|
|
where = {
|
|
...where,
|
|
pro: filter.pro,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.organizations) {
|
|
const listItems = filter.organizations.split('|').map(item => {
|
|
return Utils.uuid(item)
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
organizationsId: {[Op.or]: listItems}
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.createdAtRange) {
|
|
const [start, end] = filter.createdAtRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (globalAccess) {
|
|
delete where.organizationsId;
|
|
}
|
|
|
|
|
|
const queryOptions = {
|
|
where,
|
|
include,
|
|
distinct: true,
|
|
order: filter.field && filter.sort
|
|
? [[filter.field, filter.sort]]
|
|
: [['createdAt', 'desc']],
|
|
transaction: options?.transaction,
|
|
logging: console.log
|
|
};
|
|
|
|
if (!options?.countOnly) {
|
|
queryOptions.limit = limit ? Number(limit) : undefined;
|
|
queryOptions.offset = offset ? Number(offset) : undefined;
|
|
}
|
|
|
|
try {
|
|
const { rows, count } = await db.players.findAndCountAll(queryOptions);
|
|
|
|
return {
|
|
rows: options?.countOnly ? [] : rows,
|
|
count: count
|
|
};
|
|
} catch (error) {
|
|
console.error('Error executing query:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async findAllAutocomplete(query, limit, offset, globalAccess, organizationId,) {
|
|
let where = {};
|
|
|
|
|
|
if (!globalAccess && organizationId) {
|
|
where.organizationId = organizationId;
|
|
}
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'players',
|
|
'gamer_tag',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.players.findAll({
|
|
attributes: [ 'id', 'gamer_tag' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['gamer_tag', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.gamer_tag,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|