2024-11-08 20:27:06 +05:30
|
|
|
import mongoose from "mongoose";
|
2024-11-12 19:47:58 +05:30
|
|
|
import { createPollByData, deletePollById, findPollById, findPollsByCreatorId, updatePollVoteCount } from "../repositories/poll.repo.js";
|
|
|
|
|
import { createVote, findVoteByPollIdAndUserId } from "../repositories/vote.repo.js";
|
2024-11-08 20:27:06 +05:30
|
|
|
|
|
|
|
|
export async function createPollService(title, description, options, userId) {
|
|
|
|
|
try {
|
|
|
|
|
const optionsData = options.map(option => ({
|
|
|
|
|
name: option,
|
2024-11-12 19:47:58 +05:30
|
|
|
_id : new mongoose.Types.ObjectId(),
|
|
|
|
|
voteCount : 0
|
2024-11-08 20:27:06 +05:30
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
|
title,
|
|
|
|
|
description,
|
|
|
|
|
options : optionsData,
|
|
|
|
|
creatorId: userId
|
|
|
|
|
}
|
|
|
|
|
const poll = await createPollByData(data);
|
|
|
|
|
return poll;
|
|
|
|
|
}
|
|
|
|
|
catch(err){
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
2024-11-09 20:53:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getPollDataService(pollId) {
|
|
|
|
|
try {
|
|
|
|
|
const poll = await findPollById(pollId);
|
|
|
|
|
if (!poll) {
|
|
|
|
|
throw {
|
|
|
|
|
statusCode: 404,
|
|
|
|
|
message: "Poll not found"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const {creatorId, ...pollData} = poll._doc;
|
|
|
|
|
const {username, _id, ...creatorData} = creatorId._doc;
|
|
|
|
|
return {pollData, creatorData : {username, _id}};
|
|
|
|
|
}
|
|
|
|
|
catch(err){
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
2024-11-09 21:18:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getAllCreatedPollsService(id) {
|
|
|
|
|
try {
|
|
|
|
|
const polls = await findPollsByCreatorId(id);
|
|
|
|
|
return polls;
|
|
|
|
|
}
|
2024-11-09 22:31:19 +05:30
|
|
|
catch(err){
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function deletePollService(pollId, user) {
|
|
|
|
|
try {
|
|
|
|
|
const userId = user._id;
|
|
|
|
|
const poll = await findPollById(pollId);
|
|
|
|
|
if (!poll) {
|
|
|
|
|
throw {
|
|
|
|
|
statusCode: 404,
|
|
|
|
|
message: "Poll not found"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
console.log(poll?.creatorId._id.toString(), userId.toString(), user);
|
|
|
|
|
if (poll.creatorId._id.toString() !== userId.toString()) {
|
|
|
|
|
throw {
|
|
|
|
|
statusCode: 401,
|
|
|
|
|
message: "Unauthorized"
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-10 15:44:02 +05:30
|
|
|
const deletedPoll = await deletePollById(pollId);
|
2024-11-09 22:31:19 +05:30
|
|
|
return deletedPoll;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-12 19:47:58 +05:30
|
|
|
catch(err){
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function createVoteService(pollId, userId, optionId) {
|
|
|
|
|
try {
|
|
|
|
|
// vote already voted
|
|
|
|
|
const vote = await findVoteByPollIdAndUserId(pollId, userId);
|
|
|
|
|
if (vote) {
|
|
|
|
|
throw {
|
|
|
|
|
statusCode: 400,
|
|
|
|
|
message: "You have already voted on this poll"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
console.log(pollId, userId, optionId);
|
|
|
|
|
const poll = await findPollById(pollId);
|
|
|
|
|
if (!poll) {
|
|
|
|
|
throw {
|
|
|
|
|
statusCode: 404,
|
|
|
|
|
message: "Poll not found"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const updatedPollData = await updatePollVoteCount(pollId, optionId);
|
|
|
|
|
const createdVote = await createVote(pollId, userId, optionId);
|
|
|
|
|
console.log(updatedPollData)
|
|
|
|
|
return createVote;
|
|
|
|
|
|
|
|
|
|
}
|
2024-11-09 21:18:01 +05:30
|
|
|
catch(err){
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
2024-11-08 20:27:06 +05:30
|
|
|
}
|