UnknownSec Bypass
403
:
/
var
/
www
/
stp-bhaktisagar-backend
/
src
/
controllers
/ [
drwxrwxr-x
]
Menu
Upload
Mass depes
Mass delete
Terminal
Info server
About
name :
api.controller.js
const commonModel = require("../models/common.model"); const audioModel = require("../models/audio.model"); const playlistModel = require("../models/playlist.model"); const videoModel = require("../models/video.model"); const bookModal = require("../models/book.model"); const path = require("path"); const fs = require("fs"); const moment = require("moment"); const { sendPushNotification, AndroidStyle, sendPushNotificationZenithentcare, } = require("../util/firebase"); const { addEventNotificationJobs, cancelEventNotificationJobs, sendEventNotification, } = require("../helpers/eventSchedule.helper"); exports.getVideo = async (req, res, next) => { let results = await videoModel.getVideos(); //commonModel.getAll("audio"); res.json({ status: true, message: "", data: results, }); }; exports.addVideo = async (req, res, next) => { let { id, items, ...body } = req.body; // if (!id) { // body.order_index = (await commonModel.maxNo("audio")) || 1; // } let result = await commonModel .insertOrUpdate("video", body, id && `where id=?`, id && [id]) .catch((err) => { res.json({ status: false, message: err.message, }); }); if (result) { let insertId = result.insertId; if (items) { if (id) { await videoModel.removeFromPlaylistById(id); insertId = id; } for (const plid of items) { let plData = await commonModel.getAll( "playlist", "*", "where type='video' AND id=?", [plid] ); let plVideo = plData[0].items ? plData[0].items.split(",") : []; plVideo = [insertId, ...plVideo].join(","); await commonModel.insertOrUpdate( "playlist", { items: plVideo }, "where type='video' AND id=?", [plid] ); } } res.json({ status: true, message: id ? "Updated Successfully." : "Added Successfully.", data: result, }); } }; exports.getVideoDetail = async (req, res, next) => { let id = req.params.id; let results = await commonModel.getAll("video", "*", "where id=?", [id]); let playlist = await videoModel.getPlaylistById(id); res.json({ status: !!results.length, message: !!results.length ? "" : "Data not found.", data: results?.[0], playlist, }); }; exports.deleteVideo = async (req, res, next) => { let id = req.params.id; let results = await commonModel.deleteById("video", "where id=?", [id]); await videoModel.removeFromPlaylistById(id); res.json({ status: true, message: "Deleted successfully", }); }; exports.getAudio = async (req, res, next) => { let results = await audioModel.getAudios(); //commonModel.getAll("audio"); res.json({ status: true, message: "", data: results, }); }; exports.addAudio = async (req, res, next) => { let { id, items, ...body } = req.body; // if (!id) { // body.order_index = (await commonModel.maxNo("audio")) || 1; // } let result = await commonModel .insertOrUpdate("audio", body, id && `where id=?`, id && [id]) .catch((err) => { res.json({ status: false, message: err.message, }); }); if (result) { let insertId = result.insertId; if (items) { if (id) { await audioModel.removeFromPlaylistById(id); insertId = id; } for (const plid of items) { let plData = await commonModel.getAll("playlist", "*", "where id=?", [ plid, ]); let plAudio = plData[0].items ? plData[0].items.split(",") : []; plAudio = [insertId, ...plAudio].join(","); await commonModel.insertOrUpdate( "playlist", { items: plAudio }, "where id=?", [plid] ); } } res.json({ status: true, message: id ? "Updated Successfully." : "Added Successfully.", data: result, }); } }; exports.getAudioDetail = async (req, res, next) => { let id = req.params.id; let results = await commonModel.getAll("audio", "*", "where id=?", [id]); let playlist = await audioModel.getPlaylistById(id); res.json({ status: !!results.length, message: !!results.length ? "" : "Data not found.", data: results?.[0], playlist, }); }; exports.deleteAudio = async (req, res, next) => { let id = req.params.id; let results = await commonModel.deleteById("audio", "where id=?", [id]); await audioModel.removeFromPlaylistById(id); res.json({ status: true, message: "Deleted successfully", }); }; exports.getPlaylist = async (req, res, next) => { let { type = "audio" } = req.query; let results = await commonModel.getAll("playlist", "*", "where type=?", [ type, ]); res.json({ status: true, message: "", data: results, }); }; exports.addPlaylist = async (req, res, next) => { let { id, collection_items, ...body } = req.body; let result = await commonModel .insertOrUpdate("playlist", body, id && "where id=?", id && [id]) .catch((err) => { res.json({ status: false, message: err.message, }); }); if (result) { let insertId = result.insertId; if (collection_items) { if (id) { await playlistModel.removeFromCollectionsById(id); insertId = id; } for (const cid of collection_items) { let cData = await commonModel.getAll("collections", "*", "where id=?", [ cid, ]); let colls = cData[0].items ? cData[0].items.split(",") : []; colls = [insertId, ...colls].join(","); await commonModel.insertOrUpdate( "collections", { items: colls }, "where id=?", [cid] ); } } res.json({ status: true, message: id ? "Updated Successfully." : "Added Successfully.", data: result, }); } }; exports.getPlaylistDetail = async (req, res, next) => { let id = req.params.id; let results = await commonModel.getAll("playlist", "*", "where id=?", [id]); let collections = await playlistModel.getCollectionsById(id); res.json({ status: !!results.length, message: !!results.length ? "" : "Data not found.", data: results?.[0], collections, }); }; exports.deletePlaylist = async (req, res, next) => { let id = req.params.id; let results = await commonModel.deleteById("playlist", "where id=?", [id]); await playlistModel.removeFromCollectionsById(id); res.json({ status: true, message: "Deleted successfully", }); }; exports.getCollections = async (req, res, next) => { let { type = "audio" } = req.query; let results = await commonModel.getAll("collections", "*", "where type=?", [ type, ]); res.json({ status: true, message: "", data: results, }); }; exports.addCollection = async (req, res, next) => { let { id, ...body } = req.body; let result = await commonModel .insertOrUpdate("collections", body, id && "where id=?", id && [id]) .catch((err) => { res.json({ status: false, message: err.message, }); }); if (result) { res.json({ status: true, message: id ? "Updated Successfully." : "Added Successfully.", data: result, }); } }; exports.getCollectionDetail = async (req, res, next) => { let id = req.params.id; let results = await commonModel.getAll("collections", "*", "where id=?", [ id, ]); res.json({ status: !!results.length, message: !!results.length ? "" : "Data not found.", data: results?.[0], }); }; exports.deleteCollection = async (req, res, next) => { let id = req.params.id; let results = await commonModel.deleteById("collections", "where id=?", [id]); res.json({ status: true, message: "Deleted successfully", }); }; exports.getAllAudio = async (req, res, next) => { let audio = await commonModel.getAll("audio"); let playlist = await commonModel.getAll( "playlist", "*", "where type='audio' and enable=1 order by order_index asc" ); let collections = await commonModel.getAll( "collections", "*", "where type='audio' and enable=1 order by id desc" ); let audios = playlist .reduce((r, p) => { if (p.type == "audio") { let items = p.items ? p.items.split(",") : []; return Array.from(new Set([...r, ...items])); } return r; }, []) .map((r) => { return audio.find((a) => a.id == r); }) .filter(Boolean); res.json({ status: true, message: "", audios, playlist, collections: collections, }); }; exports.getAllVideo = async (req, res, next) => { let track = await commonModel.getAll("video"); let playlist = await commonModel.getAll( "playlist", "*", "where type='video' and enable=1 order by order_index asc" ); let collections = await commonModel.getAll( "collections", "*", "where type='video' and enable=1 order by id desc" ); let tracks = playlist .reduce((r, p) => { if (p.type == "video") { let items = p.items ? p.items.split(",") : []; return Array.from(new Set([...r, ...items])); } return r; }, []) .map((r) => { return track.find((a) => a.id == r); }) .filter(Boolean); res.json({ status: true, message: "", videos: tracks, playlist, collections: collections, }); }; exports.getMetadata = async (req, res, next) => { let code = req.params.code; let results = await commonModel.getAll("metadata", "*", `where code=?`, [ code, ]); res.json({ status: true, message: "", data: results.reduce((d, r) => { return { ...d, [r.prop]: JSON.parse(r.value) }; }, {}), }); }; exports.addMetadata = async (req, res, next) => { let code = req.params.code; let { prop, value } = req.body; let results = await commonModel.getAll( "metadata", "*", `where code=? and prop=?`, [code, prop] ); let exist = !!results.length; let result = await commonModel .insertOrUpdate( "metadata", { code, prop, value: JSON.stringify(value) }, exist ? "where code=? and prop=?" : "", exist ? [code, prop] : null ) .catch((err) => { res.json({ status: false, message: err.message, }); }); if (result) { res.json({ status: true, message: exist ? "Updated Successfully." : "Added Successfully.", data: result, }); } }; exports.getImages = async (req, res, next) => { let dirPath = path.resolve("public/images"); let files = await fs.promises.readdir(dirPath).catch((err) => { res.json({ status: false, message: err.message, }); }); if (!files) { return; } files = files.map((file) => { const fullPath = path.join(dirPath, file); let stat = fs.statSync(fullPath); return { name: file, url: `${process.env.BASE_URL}/images/${file}`, extension: path.extname(file), size: stat.size, date: stat.ctime, mtime: stat.mtime, }; }); files.sort(function (a, b) { return -a.mtime.getTime() + b.mtime.getTime(); }); res.json({ status: true, message: "", data: files, }); }; exports.uploadImage = async (req, res, next) => { if (!req.file) { return res.json({ status: false, message: "Image required.", }); } res.json({ status: true, message: "Image Uploaded", data: { name: req.file.filename, url: `${process.env.BASE_URL}/images/${req.file.filename}`, }, }); }; exports.deleteImage = async (req, res, next) => { let name = req.params.name; let filePath = path.resolve("public/images", name); await fs.promises.unlink(filePath).catch((err) => {}); // if (!files) { // return; // } res.json({ status: true, message: "Deleted successfully", name, }); }; exports.getEvents = async (req, res, next) => { let results = await commonModel.getAll( "events", "*", "order by start_datetime asc" ); res.json({ status: true, message: "", data: results, }); }; exports.addEvent = async (req, res, next) => { let { id, ...body } = req.body; let result = await commonModel .insertOrUpdate("events", body, id && "where id=?", id && [id]) .catch((err) => { res.json({ status: false, message: err.message, }); }); if (result) { let results = await commonModel.getAll("events", "*", "where id=?", [ id ? id : result.insertId, ]); let { start_datetime, end_datetime } = results?.[0]; let today = moment(); if ( moment(start_datetime).isSameOrAfter(today) || moment(end_datetime).isSameOrAfter(today) ) { addEventNotificationJobs(results?.[0]); } else { cancelEventNotificationJobs(id ? id : result.insertId); } // console.log(start_datetime, end_datetime ); res.json({ status: true, message: id ? "Updated Successfully." : "Added Successfully.", data: result, }); } }; exports.getEventDetail = async (req, res, next) => { let id = req.params.id; let results = await commonModel.getAll("events", "*", "where id=?", [id]); res.json({ status: !!results.length, message: !!results.length ? "" : "Data not found.", data: results?.[0], }); }; exports.deleteEvent = async (req, res, next) => { let id = req.params.id; let results = await commonModel.deleteById("events", "where id=?", [id]); cancelEventNotificationJobs(id); res.json({ status: true, message: "Deleted successfully", }); }; exports.sendEventNotification = async (req, res, next) => { let id = req.params.id; let results = await commonModel.getAll("events", "*", "where id=?", [id]); if (results?.[0]) { sendEventNotification(results?.[0]); } res.json({ status: !!results.length, message: !!results.length ? "Notification Sent" : "Data not found.", data: results?.[0], }); }; exports.getBook = async (req, res, next) => { let results = await bookModal.getAll(); //commonModel.getAll("audio"); res.json({ status: true, message: "", data: results, }); }; exports.addBook = async (req, res, next) => { let { id, items, ...body } = req.body; // if (!id) { // body.order_index = (await commonModel.maxNo("audio")) || 1; // } let result = await commonModel .insertOrUpdate("book", body, id && `where id=?`, id && [id]) .catch((err) => { res.json({ status: false, message: err.message, }); }); if (result) { let insertId = result.insertId; if (items) { if (id) { await bookModal.removeFromCategoryById(id); insertId = id; } for (const plid of items) { let plData = await commonModel.getAll( "book_category", "*", "where id=?", [plid] ); let plVideo = plData[0].items ? plData[0].items.split(",") : []; plVideo = [insertId, ...plVideo].join(","); await commonModel.insertOrUpdate( "book_category", { items: plVideo }, "where id=?", [plid] ); } } res.json({ status: true, message: id ? "Updated Successfully." : "Added Successfully.", data: result, }); } }; exports.getBookDetail = async (req, res, next) => { let id = req.params.id; let results = await commonModel.getAll("book", "*", "where id=?", [id]); let category = await bookModal.getCategoryById(id); res.json({ status: !!results.length, message: !!results.length ? "" : "Data not found.", data: results?.[0], category, }); }; exports.deleteBook = async (req, res, next) => { let id = req.params.id; let results = await commonModel.deleteById("book", "where id=?", [id]); await bookModal.removeFromCategoryById(id); res.json({ status: true, message: "Deleted successfully", }); }; exports.getBookCategory = async (req, res, next) => { let {} = req.query; let results = await commonModel.getAll("book_category", "*"); res.json({ status: true, message: "", data: results, }); }; exports.addBookCategory = async (req, res, next) => { let { id, collection_items, ...body } = req.body; let result = await commonModel .insertOrUpdate("book_category", body, id && "where id=?", id && [id]) .catch((err) => { res.json({ status: false, message: err.message, }); }); if (result) { let insertId = result.insertId; if (collection_items) { if (id) { await bookModal.categoryRemoveFromCollectionsById(id); insertId = id; } for (const cid of collection_items) { let cData = await commonModel.getAll( "book_collections", "*", "where id=?", [cid] ); let colls = cData[0].items ? cData[0].items.split(",") : []; colls = [insertId, ...colls].join(","); await commonModel.insertOrUpdate( "book_collections", { items: colls }, "where id=?", [cid] ); } } res.json({ status: true, message: id ? "Updated Successfully." : "Added Successfully.", data: result, }); } }; exports.getBookCategoryDetail = async (req, res, next) => { let id = req.params.id; let results = await commonModel.getAll("book_category", "*", "where id=?", [ id, ]); let collections = await bookModal.getCollectionsById(id); res.json({ status: !!results.length, message: !!results.length ? "" : "Data not found.", data: results?.[0], collections, }); }; exports.deleteBookCategory = async (req, res, next) => { let id = req.params.id; let results = await commonModel.deleteById("book_category", "where id=?", [ id, ]); await bookModal.categoryRemoveFromCollectionsById(id); res.json({ status: true, message: "Deleted successfully", }); }; exports.getAllBook = async (req, res, next) => { let book = await commonModel.getAll("book"); let category = await commonModel.getAll( "book_category", "*", "where enable=1 order by order_index asc" ); let collections = await commonModel.getAll( "book_collections", "*", "where enable=1 order by id desc" ); let books = category .reduce((r, p) => { let items = p.items ? p.items.split(",") : []; return Array.from(new Set([...r, ...items])); }, []) .map((r) => { return book.find((a) => a.id == r); }) .filter(Boolean); res.json({ status: true, message: "", books: books, category, collections, }); }; exports.getBookCollections = async (req, res, next) => { let {} = req.query; let results = await commonModel.getAll("book_collections", "*"); res.json({ status: true, message: "", data: results, }); }; exports.addBookCollection = async (req, res, next) => { let { id, ...body } = req.body; let result = await commonModel .insertOrUpdate("book_collections", body, id && "where id=?", id && [id]) .catch((err) => { res.json({ status: false, message: err.message, }); }); if (result) { res.json({ status: true, message: id ? "Updated Successfully." : "Added Successfully.", data: result, }); } }; exports.getBookCollectionDetail = async (req, res, next) => { let id = req.params.id; let results = await commonModel.getAll( "book_collections", "*", "where id=?", [id] ); res.json({ status: !!results.length, message: !!results.length ? "" : "Data not found.", data: results?.[0], }); }; exports.deleteBookCollection = async (req, res, next) => { let id = req.params.id; let results = await commonModel.deleteById("book_collections", "where id=?", [ id, ]); res.json({ status: true, message: "Deleted successfully", }); }; exports.sendNotificationZenithentcare = async (req, res, next) => { let { title, tokens=[], body, data, topic, image } = req.body; // console.log(tokens, topic); if (!topic && !tokens?.length) { res.json({ status: false, message: "Token or Topic required.", }); return; } let result = await sendPushNotificationZenithentcare({ topic: topic, tokens: tokens, title: title, body: body, data: data, options: { android: { ...(image ? { style: { type: AndroidStyle.BIGPICTURE, picture: image, }, } : {}), }, }, }); res.json({ status: true, message: "Notification Sent", data: result, }); }; // sendPushNotification({ // topic:'public', // // tokens: [ // // "c6m3DIqWTYyThgGsc89X-b:APA91bFACyaCohI-6QrES6qRUdf07S-cx0sqtLC_Wg8q-iLiOT_U6Skw0S3EzlwvmSr9j406eiwhoTZmluU47giVaKFbDPcIADRYBakbvd1U_NnFtfb6tN5HPjETJJK_oxtAhaA-2MKm", // // ], // title: 'माघमेला सत्संग', // body: "माघमेला सत्संग 01 - 2006", // // data: { // // url:"https://stp.santgyaneshwarji.org/thumb/6.jpg" // // }, // options: { // android: { // // largeIcon: 'logo', // // style: { // // type: AndroidStyle.BIGPICTURE, // // picture: "https://stp.santgyaneshwarji.org/thumb/6.jpg", // // // summary:"AAnjkbv vw vjhw evj wehjvw evjhw vhe", // // // title:'ererter' // // }, // }, // }, // });
Copyright © 2025 - UnknownSec