Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Createcampaign #381

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
299 changes: 259 additions & 40 deletions controllers/external.controller.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
const { responseHandler } = require('../helpers/response-handler')
const makeResponseData = responseHandler.makeResponseData
const makeResponseError = responseHandler.makeResponseError
const multer = require('multer')
const { ObjectId } = require('mongodb')

const storageCover = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/')
},
filename: (req, file, cb) => {
cb(
null,
new Date().toISOString().replace(/:/g, '-') + file.originalname
)
},
})
const {
Campaigns,
GoogleProfile,
LinkedinProfile,
TwitterProfile,
FbPage,
TikTokProfile,
UserExternalWallet,
CampaignLink,
Campaigns,
FbProfile
FbProfile,
} = require('../model/index')
var fs = require('fs')

var rp = require('axios')
const { oauth } = require('../conf/config')
const { oauth } = require('../conf/config')
const { filterLinks } = require('../web3/campaigns')
const {
verifyYoutube,
Expand All @@ -29,13 +45,11 @@
answerAbos,
getPromApplyStats,
} = require('../manager/oracles')
const { create } = require('ipfs-http-client')
var mongoose = require('mongoose')
const Grid = require('gridfs-stream')

const {
getLinkedinLinkInfo,
influencersLinks,
} = require('../web3/campaigns')


const { getLinkedinLinkInfo, influencersLinks } = require('../web3/campaigns')

exports.createUserFromExternalWallet = async (req, res) => {
try {
Expand Down Expand Up @@ -73,6 +87,10 @@
}
}

exports.campaignsPictureUploadExternal = multer({
storage: storageCover,
}).single('cover')

exports.externalSocialAccounts = async (req, res) => {
try {
const user = await UserExternalWallet.findOne({
Expand Down Expand Up @@ -666,44 +684,248 @@
}
}

module.exports.externalSaveCampaign = async (req, res) => {
try {
let campaign = req.body
const user = await UserExternalWallet.findOne({ walletId: req.address })

campaign.idNode = user.UserId
campaign.createdAt = Date.now()
campaign.updatedAt = Date.now()
campaign.type = 'draft'
let draft = await Campaigns.create(campaign)
return responseHandler.makeResponseData(res, 200, 'success', draft)
} catch (err) {
return responseHandler.makeResponseError(
res,
500,
err.message ? err.message : err.error
)
}
}

module.exports.externalVerifyExpiredToken = (req, res) => {
return responseHandler.makeResponseData(res, 200, 'success', true)
}

module.exports.externalApply = async (req, res) => {
module.exports.externalAccount = async (req, res) => {
try {
if (req.user) {
let {
password,
secureCode,
secret,
newEmail,
fireBaseAccessToken,
...user
} = req.user.toObject()

return makeResponseData(res, 200, 'success', user)
} else {
return makeResponseError(res, 204, 'user not found')
}
} catch (err) {
return makeResponseError(
res,
500,
err.message ? err.message : err.error
)
}
}

module.exports.externalUpdate = async (req, res) => {
try {
let campaign = req.body
campaign.updatedAt = Date.now()
let updatedCampaign = await Campaigns.findOneAndUpdate(
{ _id: req.params.id, idNode: req.body.userId },

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.
{ $set: campaign.values },

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.
{ new: true }
)

if (updatedCampaign) {
return responseHandler.makeResponseData(
res,
200,
'updated',
updatedCampaign
)
} else {
return responseHandler.makeResponseError(
res,
204,
'Campaign not found'
)
}
} catch (err) {
return responseHandler.makeResponseError(
res,
500,
err.message ? err.message : err.error
)
}
}

module.exports.externalUploadPictureToIPFS = async (req, res) => {
// using IPFS
try {
const user = await UserExternalWallet.findOne({
walletId: req.address,
if (req.file) {
const { id } = req.params

// SEARCH COMPAIGN ID
const campaign = await Campaigns.findOne({
_id: id,
idNode: req.body.userId,
})

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.

if (campaign) {
// IPFS CONNECTION
const ipfs = await ipfsConnect()

// READ FILE
const x = fs.readFileSync(req.file.path)

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.

// ADD TO IPFS
let buffer = Buffer.from(x)
let result = await ipfs.add({ content: buffer })

// REMOVE FILE FROM UPLOADS DIR
fs.unlinkSync('uploads/' + req.file.filename)

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.

return responseHandler.makeResponseData(res, 200, result, true)
} else
return responseHandler.makeResponseData(
res,
400,
'campaign not found / you are not the owner',
false
)
} else
return responseHandler.makeResponseData(
res,
400,
'required picture',
false
)
} catch (err) {
return responseHandler.makeResponseError(
res,
500,
err.message ? err.message : err.error
)
}
}
const ipfsConnect = async () => {
const auth =
'Basic ' +
Buffer.from(
process.env.IPFS_PROJECT_ID + ':' + process.env.IPFS_SECRET_KEY
).toString('base64')

const ipfs = await create({
host: process.env.IPFS_INFURA,
port: process.env.IPFS_INFURA_PORT,
protocol: process.env.IPFS_INFURA_PROTOCOL,
headers: {
authorization: auth,
},
})
var id = user.UserId
// var pass = req.body.pass
var {
linkedinId,
idCampaign,
typeSN,
idPost,
idUser,
title,
pass,
linkedinUserId,
signature
} = req.body
let [prom, date, hash] = [{}, Math.floor(Date.now() / 1000), req.body.hash]
var campaignDetails = await Campaigns.findOne({ hash }).lean()
let limit = campaignDetails.limit;
let userWallet = user.walletId;
let numberParticipation = await CampaignLink.find({ id_campaign: hash,id_wallet:userWallet}).count()
if (limit > 0 && limit === numberParticipation){
return ipfs
}

module.exports.externalAddKits = async (req, res) => {
try {
let file = req.file // Use singular 'file' instead of 'files'

let links =
typeof req.body.link === 'string'
? [req.body.link] // Wrap the single link in an array
: req.body.link

let idCampaign = ObjectId(req.body.campaign)

if (file) {
// Update the single file
await gfsKit.files.updateOne(
{ _id: file.id },
{
$set: {
campaign: {
$ref: 'campaign',
$id: idCampaign,
$db: 'atayen',
},
},
}
)
}

if (links) {
await Promise.all(
links.map((link) => {
return gfsKit.files.insertOne({
campaign: {
$ref: 'campaign',
$id: idCampaign,
$db: 'atayen',
},
link: link,
})
})
)
}

return responseHandler.makeResponseData(res, 200, 'Kit uploaded', false)
} catch (err) {
return responseHandler.makeResponseError(
res,
401,
'Limit participation reached'
500,
err.message ? err.message : err.error
)
}
}
module.exports.externalApply = async (req, res) => {
try {
const user = await UserExternalWallet.findOne({
walletId: req.address,
})
var id = user.UserId
// var pass = req.body.pass
var {
linkedinId,
idCampaign,
typeSN,
idPost,
idUser,
title,
pass,
linkedinUserId,
signature,
} = req.body
let [prom, date, hash] = [
{},
Math.floor(Date.now() / 1000),
req.body.hash,
]
var campaignDetails = await Campaigns.findOne({ hash }).lean()

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.
let limit = campaignDetails.limit
let userWallet = user.walletId
let numberParticipation = await CampaignLink.find({
id_campaign: hash,
id_wallet: userWallet,
}).count()

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.
if (limit > 0 && limit === numberParticipation) {
return responseHandler.makeResponseError(
res,
401,
'Limit participation reached'
)
}


let promExist = await CampaignLink.exists({
id_campaign: hash,
idPost,
})

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.
This query object depends on a
user-provided value
.

if (promExist) {
return responseHandler.makeResponseError(
Expand All @@ -712,11 +934,11 @@
'Link already sent'
)
}

req.body.network = campaignDetails.token.type
if (typeSN == 5) {
var linkedinProfile = await LinkedinProfile.findOne(
{ userId: id, ...(linkedinId && { linkedinId }) },

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.
{ refreshToken: 1, accessToken: 1 }
).lean()
var linkedinInfo = await getLinkedinLinkInfo(
Expand Down Expand Up @@ -786,10 +1008,7 @@
prom.shares = socialOracle?.shares || 0
prom.media_url = media_url || socialOracle?.media_url


await CampaignLink.updateOne({ _id: insert._id }, { $set: prom })


await CampaignLink.updateOne({ _id: insert._id }, { $set: prom })

return responseHandler.makeResponseData(res, 200, 'success', prom)
} catch (err) {
Expand All @@ -798,5 +1017,5 @@
500,
err.message ? err.message : err.error
)
}
}
}
Loading
Loading