Skip to content

Commit

Permalink
ts ignore removed
Browse files Browse the repository at this point in the history
  • Loading branch information
MohitPatel1 committed May 10, 2024
1 parent 14c7eb0 commit 9eff770
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
23 changes: 11 additions & 12 deletions src/controller/messageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import { delay as delayMS } from "@whiskeysockets/baileys";
const messageController = {
send: async (req:Request, res:Response, ) => {
try {
const sessionId = req.query.sessionId as string;
const { jid, type = 'number', message, options } = req.body;
const newMsg = { text : `Following msg was sent to the ${jid.slice(2,12)} \n ${message.text}` }
// @ts-ignore
const session = getSession(req.query.sessionId);
const session = getSession(sessionId);
const exists = await jidExist(session, jid, type);
if (!exists) return res.status(400).json({ error: 'JID does not exists' });
console.log('sending message')
// console.log();
const result = await session.sendMessage(jid, message, options);
const msgSelf = await session.sendMessage(session.user.id, newMsg, options)
res.status(200).json({result, msgSelf});
res.status(200).json(result);

} catch (error) {
const message = 'An error occured during message send';
Expand All @@ -26,10 +26,10 @@ const messageController = {
}
},
sendBulk: async(req:Request, res:Response) => {
let sessionId:any = req.query.sessionId;
const session = getSession(sessionId)!;
let sessionId = req.query.sessionId as string;
const session = getSession(sessionId);
const results: { index: number; result: proto.WebMessageInfo | undefined }[] = [];
const selfMsgResults : { index: number; selfMsgresult: proto.WebMessageInfo | undefined }[] = [];
// const selfMsgResults : { index: number; selfMsgresult: proto.WebMessageInfo | undefined }[] = [];
const errors: { index: number; error: string }[] = [];
for (const [
index,
Expand All @@ -42,22 +42,21 @@ const messageController = {
continue;
}

if (index > 0) await delayMS(delay);
await delayMS(delay);
const newMsg = { text : ` \n Following msg was sent to the ${jid.slice(2,12)} \n \n ${message.text}` }
const result = await session.sendMessage(jid, message, options);
const selfMsgresult = await session.sendMessage(session.user.id, newMsg, options);
// const selfMsgresult = await session.sendMessage(session.user.id, newMsg, options);
results.push({ index, result });
selfMsgResults.push({ index, selfMsgresult });
// selfMsgResults.push({ index, selfMsgresult });
} catch (e) {
const message = 'An error occured during message send';
logger.error(e, message);
errors.push({ index, error: message });
}
}

}
res
.status(req.body.length !== 0 && errors.length === req.body.length ? 500 : 200)
.json({ results, selfMsgResults, errors });
.json({ results, errors });
},
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var corsOptions : any = process.env.CORSOPTIONS ||
{
origin: '*',
methods: [ 'GET', 'POST', 'PUT', 'DELETE' ],
allowedHeaders: [ 'Content-Type', 'authorization' ],
allowedHeaders: [ 'Content-Type', 'authorization','auth_token' ],
credentials: true
};

Expand Down
1 change: 1 addition & 0 deletions src/middleware/RequestValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const requestValidator:RequestHandler = (req, res, next) => {
// console.log("req body",{body:req.body,params:req.params, query:req.query})
const errors = validationResult(req);
if(!errors.isEmpty()) return res.status(400).json({ errors: errors.array() });
console.info("No errors found in validation")
next();
};

Expand Down
8 changes: 4 additions & 4 deletions src/middleware/validateSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { sessionExist } from "../wa";


const validateSession:RequestHandler = (req,res,next)=>{
// console.log("hello")
// console.log(req.query);
// @ts-ignore
if(!sessionExist(req.query.sessionId)){
const sessionId = req.query.sessionId as string | undefined;
if(!sessionExist( sessionId )){
return res.status(404).json({ error: 'Session not found' });
}else{
console.info("session validation successful")
}
next();
};
Expand Down
8 changes: 6 additions & 2 deletions src/wa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ const shouldReconnect = (sessionId: string) => {
return false;
};

export const sessionExist = (sessionId: string) => {
return sessions.has(sessionId);
export const sessionExist = (sessionId: string | undefined ) => {
if(sessionId){
return sessions.has(sessionId);
}else{
return false
}
};

export const getSession = (sessionId: string) => {
Expand Down

0 comments on commit 9eff770

Please sign in to comment.