Skip to content

Commit

Permalink
Createcampaign (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
waelhanfi04 authored Dec 27, 2023
2 parents 20c7ff9 + 3c44669 commit 4373ab0
Show file tree
Hide file tree
Showing 3 changed files with 422 additions and 75 deletions.
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 @@ const {
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.createUserFromExternalWallet = async (req, res) => {
}
}

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

exports.externalSocialAccounts = async (req, res) => {
try {
const user = await UserExternalWallet.findOne({
Expand Down Expand Up @@ -666,40 +684,244 @@ module.exports.externalVerifyLink = async (req, response) => {
}
}

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 },
{ $set: campaign.values },
{ 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,
})

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

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

// 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)

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()
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 responseHandler.makeResponseError(
res,
401,
'Limit participation reached'
)
}


let promExist = await CampaignLink.exists({
id_campaign: hash,
idPost,
Expand All @@ -712,7 +934,7 @@ module.exports.externalApply = async (req, res) => {
'Link already sent'
)
}

req.body.network = campaignDetails.token.type
if (typeSN == 5) {
var linkedinProfile = await LinkedinProfile.findOne(
Expand Down Expand Up @@ -786,10 +1008,7 @@ module.exports.externalApply = async (req, res) => {
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 @@ module.exports.externalApply = async (req, res) => {
500,
err.message ? err.message : err.error
)
}
}
}
Loading

0 comments on commit 4373ab0

Please sign in to comment.