full app
This commit is contained in:
@@ -278,7 +278,7 @@ async function getRankedExplorePosts ({
|
||||
const userIds = [...new Set(posts.map((p) => String(p.user_id)))]
|
||||
const users = await UserModel.find({ _id: { $in: userIds } })
|
||||
.select(
|
||||
'_id user_name first_name last_name expertise sub_expertise province city user_level show_location profile_image is_verified verify_badge user_score rate blocked_users'
|
||||
'_id user_name first_name last_name expertise sub_expertise province city user_level show_location profile_image is_verified verify_badge user_score rate blocked_users cooperation_type cooperation_abroad lat lng'
|
||||
)
|
||||
.lean()
|
||||
|
||||
@@ -442,7 +442,7 @@ async function getPredictedTrendPosts ({
|
||||
const userIds = [...new Set(scored.map((p) => String(p.user_id)))]
|
||||
const users = await UserModel.find({ _id: { $in: userIds } })
|
||||
.select(
|
||||
'_id user_name first_name last_name expertise sub_expertise province city user_level show_location profile_image is_verified verify_badge user_score rate blocked_users'
|
||||
'_id user_name first_name last_name expertise sub_expertise province city user_level show_location profile_image is_verified verify_badge user_score rate blocked_users cooperation_type cooperation_abroad lat lng'
|
||||
)
|
||||
.lean()
|
||||
|
||||
@@ -1313,7 +1313,7 @@ const getPostsWeb = async (req, res, next) => {
|
||||
const userIds = [...new Set(posts.map((p) => String(p.user_id)))]
|
||||
const users = await UserModel.find({ _id: { $in: userIds } })
|
||||
.select(
|
||||
'_id user_name first_name last_name expertise sub_expertise province city user_level show_location profile_image is_verified verify_badge user_score rate lat lng blocked_users'
|
||||
'_id user_name first_name last_name expertise sub_expertise province city user_level show_location profile_image is_verified verify_badge user_score rate lat lng blocked_users cooperation_type cooperation_abroad'
|
||||
)
|
||||
.lean()
|
||||
|
||||
@@ -1338,7 +1338,7 @@ const getPostsWeb = async (req, res, next) => {
|
||||
.lean()
|
||||
const userIds = [...new Set(explorePosts.map((p) => String(p.user_id)))]
|
||||
const usersMap = await UserModel.find({ _id: { $in: userIds } })
|
||||
.select('_id user_name first_name last_name expertise sub_expertise province city user_level show_location profile_image is_verified verify_badge user_score rate blocked_users')
|
||||
.select('_id user_name first_name last_name expertise sub_expertise province city user_level show_location profile_image is_verified verify_badge user_score rate blocked_users cooperation_type cooperation_abroad lat lng')
|
||||
.lean()
|
||||
const usersById = Object.fromEntries(usersMap.map((u) => [String(u._id), u]))
|
||||
|
||||
@@ -1422,7 +1422,7 @@ const getPostsWeb = async (req, res, next) => {
|
||||
}
|
||||
}
|
||||
|
||||
const userSelectFields = '_id user_name first_name last_name expertise sub_expertise province city user_level show_location profile_image is_verified verify_badge user_score rate pinned_post_ids'
|
||||
const userSelectFields = '_id user_name first_name last_name expertise sub_expertise province city user_level show_location profile_image is_verified verify_badge user_score rate pinned_post_ids cooperation_type cooperation_abroad lat lng'
|
||||
let users = await UserModel.find(userFilter)
|
||||
.select(userSelectFields)
|
||||
.lean()
|
||||
|
||||
54
controllers/application/users/referralController.js
Normal file
54
controllers/application/users/referralController.js
Normal file
@@ -0,0 +1,54 @@
|
||||
const crypto = require('crypto')
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
|
||||
// بدون حروف/ارقامِ بههمشبیه (0/O، 1/I/L) — چون این کد قراره دستی هم کپی/تایپ بشه
|
||||
const CODE_ALPHABET = 'ABCDEFGHJKMNPQRSTUVWXYZ23456789'
|
||||
const CODE_LENGTH = 8
|
||||
const MAX_ATTEMPTS = 5
|
||||
|
||||
function generateCode () {
|
||||
const bytes = crypto.randomBytes(CODE_LENGTH)
|
||||
let code = ''
|
||||
for (let i = 0; i < CODE_LENGTH; i++) {
|
||||
code += CODE_ALPHABET[bytes[i] % CODE_ALPHABET.length]
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
||||
/**
|
||||
* کدِ معرفیِ کاربرِ لاگینشده رو برمیگردونه؛ اگه هنوز نداشته باشه (referral_code
|
||||
* روی همهی کاربرهای قدیمی null هست چون این فیلد قبلاً مرده بود) با retry-on-collision
|
||||
* یکی میسازه. لینکِ نهاییِ اشتراکگذاری رو فرانت (وب/اندروید) با همین کد میسازه:
|
||||
* https://modstagram.com/users/<username>?ref=<code>
|
||||
*/
|
||||
const getReferralCode = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.user._id
|
||||
const user = await UserModel.findById(userId).select('referral_code')
|
||||
if (!user) return res.status(404).json({ error: true, message: 'کاربر یافت نشد' })
|
||||
|
||||
if (user.referral_code) {
|
||||
return res.json({ referral_code: user.referral_code })
|
||||
}
|
||||
|
||||
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
|
||||
const code = generateCode()
|
||||
try {
|
||||
user.referral_code = code
|
||||
await user.save()
|
||||
return res.json({ referral_code: code })
|
||||
} catch (error) {
|
||||
// E11000 duplicate key — کد بهطور اتفاقی قبلاً برای یه کاربر دیگه ساخته شده؛
|
||||
// بینهایتبعیده (32^8 حالت) ولی retry ساده و بیضرره
|
||||
if (error?.code === 11000) continue
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
return res.status(500).json({ error: true, message: 'ساخت کد معرفی ناموفق بود، دوباره تلاش کنید' })
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { getReferralCode }
|
||||
Reference in New Issue
Block a user