const { HWClanService } = require('../services'); const asyncHandler = require("../middlewares/asyncHandler"); const ErrorResponse = require("../utils/errorResponse"); var mongoose = require('mongoose'), User = mongoose.model('User'); module.exports.createClan = asyncHandler(async (req, res, next) => { try { const { id, title, country, description, disbanding, frameId, icon, level, members, membersCount, minLevel, ownerId, serverId, topActivity, topDungeon } = req.body; const author = await User.findById(req.payload.id).then(function (user) { return user._id; }); const clan = await HWClanService.createClan({ id, title, country, description, disbanding, frameId, icon, level, members, membersCount, minLevel, ownerId, serverId, topActivity, topDungeon, author }); //console.log(clan); res.status(201).json({ message: 'Clan created successfully', data: clan.toJSONFor(), }); } catch (err) { return next(new ErrorResponse(`Something went wrong while creating clan - ${err.message}`, 500)); } }); module.exports.getAllClans = asyncHandler(async (req, res, next) => { try { let query = {}; let { limit = 25, offset = 0 } = req.query; if (typeof req.query.title !== 'undefined') { const rgx = new RegExp(`.*${req.query.title}.*`); query = { $or: [ { title: { $regex: rgx, $options: "i" } }, { id: { $regex: rgx, $options: "i" } }, ], } } const data = await HWClanService.getAllClans(query, limit, offset); res.status(200).json({ data: { clans: data.clans.map(function (clan) { return clan.toJSONFor(); }), clansCount: data.clansCount } }); } catch (err) { return next(new ErrorResponse(`Something went wrong while fetching clans - ${err.message}`, 500)); } }); module.exports.getClan = asyncHandler(async (req, res, next) => { try { //console.log("Clan controller getClan"); const { clanId } = req.params; let clan = await HWClanService.getClanById(clanId); if (!clan) { return next(new ErrorResponse("Clan not found", 404)); } res.status(200).json({ clan: clan.toJSONFor() }); } catch (err) { return next(new ErrorResponse(`Something went wrong while fetching clan - ${err.message}`, 500)); } }); module.exports.updateClan = asyncHandler(async (req, res, next) => { try { //console.log("Clan controller updateClan", req.payload); const { clanId } = req.params; let clan = await HWClanService.getClanById(clanId); //console.log(req.body.clan, clan); if (req.payload.id !== clan.author._id.toString()) { return next(new ErrorResponse("Unauthorized", 401)); } Object.assign(clan, req.body.clan); let data = await HWClanService.updateClan(clan); //res.status(200).json({ clan: data.toJSONFor() }); res.status(200).json({ message: 'Clan updated successfully', clan: data.toJSONFor() }); } catch (err) { return next(new ErrorResponse(`Something went wrong while updating clan - ${err.message}`, 500)); } }); module.exports.deleteClan = asyncHandler(async (req, res, next) => { try { const { clanId } = req.params; let clan = await HWClanService.getClanById(clanId); //console.log(clanId, clan.author._id.toString(), req.payload.id); if (req.payload.id !== clan.author._id.toString()) { return next(new ErrorResponse("Unauthorized", 401)); } let data = await HWClanService.deleteClan(clan); res.status(200).json({ message: 'Clan deleted successfully', data: data.id }); } catch (err) { return next(new ErrorResponse(`Something went wrong while deleting clan - ${err.message}`, 500)); } }); /* module.exports.deleteArticle = asyncHandler(async (req, res, next) => { const { slug } = req.params; const { loggedUser } = req; const article = await Article.findOne({ where: { slug: slug }, include: includeOptions, }); if (!article) { return next(new ErrorResponse("Article not found", 404)); } if (article.authorId !== loggedUser.id) { return next(new ErrorResponse("Unauthorized", 401)); } await article.destroy(); res.status(200).json({ article }); }); */