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

merge feature/change-email into develop #133

Merged
merged 8 commits into from
Oct 19, 2024
8 changes: 8 additions & 0 deletions src/app/api/_data/data-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ export async function updateUser(contact: IAccount): Promise<boolean> {
return result
}

export async function updateUserEmail(contact: IAccount): Promise<boolean> {
const filter = { _id: getObjectId(contact.id) }
const updateData = { email: contact.email }
const setValue = { $set: updateData }
const result: boolean = await updateOneCommon(DB_CHATTERPAY_NAME, SCHEMA_USERS, filter, setValue)
return result
}

export async function getWalletNfts(wallet: string): Promise<INFT[] | undefined> {
const client = await getClientPromise()
const db = client.db(DB_CHATTERPAY_NAME)
Expand Down
4 changes: 3 additions & 1 deletion src/app/api/_hooks/api-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export const endpoints = {
root: getFullUIEndpoint('app'),
user: {
id: (id: string) => getFullUIEndpoint(`user/${id}`),
update: (id: string) => getFullUIEndpoint(`user/${id}`)
update: (id: string) => getFullUIEndpoint(`user/${id}`),
code: (id: string) => getFullUIEndpoint(`user/${id}/code`),
updateEmail: (id: string) => getFullUIEndpoint(`user/${id}/email`)
},
wallet: {
balance: (id: string) => getFullUIEndpoint(`wallet/${id}/balance`),
Expand Down
19 changes: 19 additions & 0 deletions src/app/api/v1/_common/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { BOT_API_TOKEN } from 'src/config-global'

import { post, endpoints } from '../../_hooks/api-resolver'

// ----------------------------------------------------------------------

export async function send2FACode(phone: string, code: number, codeMsg: string) {
const botSendMsgEndpoint = endpoints.backend.sendMessage()
const botSendMsgData = {
data_token: BOT_API_TOKEN,
channel_user_id: phone,
message: codeMsg.replace('{2FA_CODE}', code.toString())
}
console.info('botSendMsgData', botSendMsgData)
const botSendMsgResult = await post(botSendMsgEndpoint, botSendMsgData)
console.info('botSendMsgResult', botSendMsgResult)

return true
}
45 changes: 3 additions & 42 deletions src/app/api/v1/auth/code/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { NextRequest, NextResponse } from 'next/server'

import { post, endpoints } from 'src/app/api/_hooks/api-resolver'
import { getUserByPhone, updateUserCode } from 'src/app/api/_data/data-service'
import { getIpFromRequest, validateRecaptcha } from 'src/app/api/_utils/request-utils'
import {
getUserByPhone,
updateUserCode,
getLastConversacionUserId
} from 'src/app/api/_data/data-service'
import {
BOT_API_URL,
BOT_API_TOKEN,
Expand All @@ -15,7 +10,8 @@ import {
} from 'src/config-global'

import { IAccount } from 'src/types/account'
import { LastUserConversation } from 'src/types/chat'

import { send2FACode } from '../../_common/common'

// ----------------------------------------------------------------------

Expand Down Expand Up @@ -118,38 +114,3 @@ export async function POST(req: NextRequest) {
})
}
}

// ----------------------------------------------------------------------

async function send2FACode(phone: string, code: number, codeMsg: string) {
let phoneToMsg = phone

// Search last conversationIn User in bot with last 8 phone-digits
console.info('entered send2FACode', phone, code)
const lastUserConversation: LastUserConversation = await getLastConversacionUserId(phone)
console.info('lastUserConversation', phone, lastUserConversation)

if (!lastUserConversation) {
console.info('lastUserConversation NOT_FOUND, using phone:', phone)
} else {
phoneToMsg = lastUserConversation.channel_user_id
console.info(
'lastUserConversation FOUND, using channel_user_id as phone:',
lastUserConversation.channel_user_id,
lastUserConversation.id
)
}

// Send 2FA code by whatsapp with operator-reply endpoint
const botSendMsgEndpoint = endpoints.backend.sendMessage()
const botSendMsgData = {
data_token: BOT_API_TOKEN,
channel_user_id: phoneToMsg,
message: codeMsg.replace('{2FA_CODE}', code.toString())
}
console.info('botSendMsgData', botSendMsgData)
const botSendMsgResult = await post(botSendMsgEndpoint, botSendMsgData)
console.info('botSendMsgResult', botSendMsgResult)

return true
}
85 changes: 85 additions & 0 deletions src/app/api/v1/user/[id]/code/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { NextRequest, NextResponse } from 'next/server'

import { getUserByPhone, updateUserCode } from 'src/app/api/_data/data-service'
import { BOT_API_URL, BOT_API_TOKEN, botApiWappEnabled } from 'src/config-global'

import { IAccount } from 'src/types/account'

import { send2FACode } from '../../../_common/common'

// ----------------------------------------------------------------------

export async function POST(req: NextRequest) {
try {
const { phone, codeMsg }: { phone: string; codeMsg: string; recaptchaToken: string } =
await req.json()

if (!phone || !codeMsg) {
return new NextResponse(
JSON.stringify({
code: 'INVALID_REQUEST_PARAMS',
error: 'Missing phone number or codeMsg in request body'
}),
{
status: 400,
headers: { 'Content-Type': 'application/json' }
}
)
}

if (!BOT_API_URL || !BOT_API_TOKEN) {
return new NextResponse(JSON.stringify({ error: `Backend API or Token not set.` }), {
status: 404,
headers: { 'Content-Type': 'application/json' }
})
}

const user: IAccount | undefined = await getUserByPhone(phone)
if (!user) {
return new NextResponse(
JSON.stringify({ code: 'USER_NOT_FOUND', error: 'user not found with that phone number' }),
{
status: 404,
headers: { 'Content-Type': 'application/json' }
}
)
}

// Generate and store 2FA code
const code: number = Math.floor(Math.random() * (999999 - 100000 + 1)) + 100000
await updateUserCode(user.id, code)

// Send 2FA code to user'whatsapp
let botSentCodeResult: boolean = true
if (botApiWappEnabled) {
console.info('calling send2FACode SYNC', phone, code)
botSentCodeResult = await send2FACode(phone, code, codeMsg)

if (!botSentCodeResult) {
return new NextResponse(
JSON.stringify({
code: 'USER_NOT_FOUND',
error: 'user not found with that phone number'
}),
{
status: 404,
headers: { 'Content-Type': 'application/json' }
}
)
}
}

const finalResult: { phone: string; sent: boolean } = {
phone,
sent: botSentCodeResult
}

return NextResponse.json(finalResult)
} catch (ex) {
console.error(ex)
return new NextResponse(JSON.stringify({ error: 'Error in authentication' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
})
}
}
94 changes: 94 additions & 0 deletions src/app/api/v1/user/[id]/email/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { NextRequest, NextResponse } from 'next/server'

import { getUserById, updateUserCode, updateUserEmail } from 'src/app/api/_data/data-service'

import { IAccount } from 'src/types/account'

// ----------------------------------------------------------------------

type IParams = {
id: string
}

type IBody = {
phone: string
code: string
recaptchaToken: string
email: string
}
export async function POST(req: NextRequest, { params }: { params: IParams }) {
try {
const { id } = params
const { phone, code, email }: IBody = await req.json()

if (!id) {
return new NextResponse(
JSON.stringify({
code: 'INVALID_REQUEST_PARAMS',
error: 'Missing parameters in path params'
}),
{
status: 400,
headers: { 'Content-Type': 'application/json' }
}
)
}

if (!email || !code || !phone) {
return new NextResponse(
JSON.stringify({
code: 'INVALID_REQUEST_PARAMS',
error: 'Missing email, code and phone in request body'
}),
{
status: 400,
headers: { 'Content-Type': 'application/json' }
}
)
}

const user: IAccount | undefined = await getUserById(id)
if (!user) {
return new NextResponse(
JSON.stringify({ code: 'USER_NOT_FOUND', error: 'user not found with that id' }),
{
status: 404,
headers: { 'Content-Type': 'application/json' }
}
)
}

if (!user.code || code.toString() !== user.code.toString()) {
return new NextResponse(JSON.stringify({ code: 'INVALID_CODE', error: 'invalid code' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
})
}

user.email = email
const reult: boolean = await updateUserEmail(user)

if (!reult) {
return new NextResponse(
JSON.stringify({
code: 'USER_UPDATE_ERROR',
error: 'user update error'
}),
{
status: 400,
headers: { 'Content-Type': 'application/json' }
}
)
}

updateUserCode(user.id, undefined)

return NextResponse.json({ reult })
} catch (ex) {
console.error(ex)
return new NextResponse(JSON.stringify({ error: 'Error in update user' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
})
}
}
17 changes: 15 additions & 2 deletions src/app/api/v1/user/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,24 @@ export async function POST(req: NextRequest, { params }: { params: IParams }) {
const { id } = params
const { name }: { name: string } = await req.json()

if (!name || !id) {
if (!id) {
return new NextResponse(
JSON.stringify({
code: 'INVALID_REQUEST_PARAMS',
error: 'Missing parameters in path params'
}),
{
status: 400,
headers: { 'Content-Type': 'application/json' }
}
)
}

if (!name) {
return new NextResponse(
JSON.stringify({
code: 'INVALID_REQUEST_PARAMS',
error: 'Missing id or name in request body'
error: 'Missing name in request body'
}),
{
status: 400,
Expand Down
11 changes: 11 additions & 0 deletions src/app/dashboard/user/account/email/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { EmailEditView } from 'src/sections/account/view'

// ----------------------------------------------------------------------

export const metadata = {
title: 'Account - Email'
}

export default function AccountEmailPage() {
return <EmailEditView />
}
25 changes: 0 additions & 25 deletions src/app/dashboard/user/wallet/[walletId]/nfts/[nftId]/page_

This file was deleted.

Loading