Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #211 from mgilangjanuar/staging
Browse files Browse the repository at this point in the history
Release v1.7.1
  • Loading branch information
mgilangjanuar authored Jan 17, 2022
2 parents 0c538bf + 0213178 commit d502ca3
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 50 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "teledrive",
"version": "1.7.0",
"version": "1.7.1",
"repository": "git@github.com:mgilangjanuar/teledrive.git",
"author": "M Gilang Januar <mgilangjanuar@gmail.com>",
"license": "MIT",
Expand Down
4 changes: 3 additions & 1 deletion server/.env-example
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ MIDTRANS_SERVER_KEY=

REDIS_URI=

IS_MAINTENANCE=
IS_MAINTENANCE=

UTILS_API_KEY=
4 changes: 2 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "server",
"version": "1.7.0",
"version": "1.7.1",
"main": "dist/index.js",
"license": "MIT",
"private": true,
Expand Down Expand Up @@ -83,4 +83,4 @@
"rimraf": "^3.0.2",
"typescript": "^4.4.2"
}
}
}
10 changes: 10 additions & 0 deletions server/src/api/middlewares/Key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { NextFunction, Request, Response } from 'express'

export async function AuthKey(req: Request, _: Response, next: NextFunction): Promise<any> {
const authkey = req.headers['token'] || req.query['token']
if (authkey !== process.env.UTILS_API_KEY) {
throw { status: 401, body: { error: 'Invalid key' } }
}

return next()
}
152 changes: 107 additions & 45 deletions server/src/api/v1/Users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { PayPal, SubscriptionDetails } from '../../service/PayPal'
import { buildSort, buildWhereQuery } from '../../utils/FilterQuery'
import { Endpoint } from '../base/Endpoint'
import { Auth, AuthMaybe } from '../middlewares/Auth'
import { AuthKey } from '../middlewares/Key'

@Endpoint.API()
export class Users {
Expand Down Expand Up @@ -48,6 +49,112 @@ export class Users {
return res.send({ usage })
}

@Endpoint.GET('/', { middlewares: [Auth] })
public async find(req: Request, res: Response): Promise<any> {
const { sort, offset, limit, ...filters } = req.query
const [users, length] = await Model.createQueryBuilder('users')
.select('users.username')
.where(buildWhereQuery(filters) || 'true')
.skip(Number(offset) || undefined)
.take(Number(limit) || undefined)
.orderBy(buildSort(sort as string))
.getManyAndCount()
return res.send({ users, length })
}

@Endpoint.PATCH('/me/settings', { middlewares: [Auth] })
public async settings(req: Request, res: Response): Promise<any> {
const { settings } = req.body
if (settings.theme === 'dark' && (!req.user.plan || req.user.plan === 'free')) {
throw { status: 402, body: { error: 'You need to upgrade your plan to use dark theme' } }
}
req.user.settings = {
...req.user.settings || {},
...settings
}
await req.user.save()
return res.send({ settings: req.user?.settings })
}

@Endpoint.POST('/me/delete', { middlewares: [Auth] })
public async remove(req: Request, res: Response): Promise<any> {
const { reason, agreement } = req.body
if (agreement !== 'permanently removed') {
throw { status: 400, body: { error: 'Invalid agreement' } }
}
if (reason) {
await axios.post(`https://api.telegram.org/bot${process.env.TG_BOT_TOKEN}/sendMessage`, {
chat_id: process.env.TG_BOT_OWNER_ID,
text: `😭 ${req.user.name} (@${req.user.username}) removed their account.\n\nReason: ${reason}`
})
}
await Files.delete({ user_id: req.user.id })
await req.user.remove()
const success = await req.tg.invoke(new Api.auth.LogOut())
return res.clearCookie('authorization').clearCookie('refreshToken').send({ success })
}

@Endpoint.POST('/me/paymentSync', { middlewares: [Auth] })
public async paymentSync(req: Request, res: Response): Promise<any> {
type Payment = { subscription_id?: string, midtrans_id?: string, plan?: string }
let result: Payment = null
try {
const { data } = await axios.get<{ payment: Payment }>(`https://teledriveapp.com/api/v1/users/${req.user.tg_id}/payment`, {
headers: { token: process.env.UTILS_API_KEY }
})
if (data.payment.plan && data.payment.plan !== 'free') {
result = data.payment
}
} catch (error) {
// ignore
}
if (!result) {
try {
const { data } = await axios.get<{ payment: Payment }>(`https://us.teledriveapp.com/api/v1/users/${req.user.tg_id}/payment`, {
headers: { token: process.env.UTILS_API_KEY }
})
if (data.payment.plan && data.payment.plan !== 'free') {
result = data.payment
}
} catch (error) {
// ignore
}
}
if (!result) {
try {
const { data } = await axios.get<{ payment: Payment }>(`https://ge.teledriveapp.com/api/v1/users/${req.user.tg_id}/payment`, {
headers: { token: process.env.UTILS_API_KEY }
})
if (data.payment.plan && data.payment.plan !== 'free') {
result = data.payment
}
} catch (error) {
// ignore
}
}
if (result) {
req.user.subscription_id = result?.subscription_id
req.user.midtrans_id = result?.midtrans_id
req.user.plan = result?.plan as any
await req.user.save()
}
return res.status(202).send({ accepted: true })
}

@Endpoint.GET('/:tgId/payment', { middlewares: [AuthKey] })
public async payment(req: Request, res: Response): Promise<any> {
const { tgId } = req.params
const user = await Model.findOne({ where: { tg_id: tgId } })
if (!user) {
throw { status: 404, body: { error: 'User not found' } }
}
return res.send({ payment: {
subscription_id: user.subscription_id,
midtrans_id: user.midtrans_id,
plan: user.plan
} })
}

@Endpoint.GET('/:username/:param?', { middlewares: [Auth] })
public async retrieve(req: Request, res: Response): Promise<any> {
const { username, param } = req.params
Expand Down Expand Up @@ -124,51 +231,6 @@ export class Users {
return res.send({ user })
}

@Endpoint.GET('/', { middlewares: [Auth] })
public async find(req: Request, res: Response): Promise<any> {
const { sort, offset, limit, ...filters } = req.query
const [users, length] = await Model.createQueryBuilder('users')
.select('users.username')
.where(buildWhereQuery(filters) || 'true')
.skip(Number(offset) || undefined)
.take(Number(limit) || undefined)
.orderBy(buildSort(sort as string))
.getManyAndCount()
return res.send({ users, length })
}

@Endpoint.PATCH('/me/settings', { middlewares: [Auth] })
public async settings(req: Request, res: Response): Promise<any> {
const { settings } = req.body
if (settings.theme === 'dark' && (!req.user.plan || req.user.plan === 'free')) {
throw { status: 402, body: { error: 'You need to upgrade your plan to use dark theme' } }
}
req.user.settings = {
...req.user.settings || {},
...settings
}
await req.user.save()
return res.send({ settings: req.user?.settings })
}

@Endpoint.POST('/me/delete', { middlewares: [Auth] })
public async remove(req: Request, res: Response): Promise<any> {
const { reason, agreement } = req.body
if (agreement !== 'permanently removed') {
throw { status: 400, body: { error: 'Invalid agreement' } }
}
if (reason) {
await axios.post(`https://api.telegram.org/bot${process.env.TG_BOT_TOKEN}/sendMessage`, {
chat_id: process.env.TG_BOT_OWNER_ID,
text: `😭 ${req.user.name} (@${req.user.username}) removed their account.\n\nReason: ${reason}`
})
}
await Files.delete({ user_id: req.user.id })
await req.user.remove()
const success = await req.tg.invoke(new Api.auth.LogOut())
return res.clearCookie('authorization').clearCookie('refreshToken').send({ success })
}

// @Endpoint.USE('/upgradePlans', { middlewares: [Auth] })
// public async upgradePlans(req: Request, res: Response): Promise<any> {
// if (req.user.username !== 'mgilangjanuar') {
Expand Down
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web",
"version": "1.7.0",
"version": "1.7.1",
"private": true,
"dependencies": {
"@ideasio/add-to-homescreen-react": "^1.0.10",
Expand Down
15 changes: 15 additions & 0 deletions web/src/pages/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ const Login: React.FC<Props> = ({ me }) => {
const { password } = formLogin.getFieldsValue()
try {
const { data } = await req.post('/auth/login', { ...needPassword ? { password } : { phoneNumber, phoneCode, phoneCodeHash } })
try {
req.post('/users/me/paymentSync')
} catch (error) {
// ignore
}
setLoadingLogin(false)
notification.success({
message: 'Success',
Expand Down Expand Up @@ -120,6 +125,11 @@ const Login: React.FC<Props> = ({ me }) => {
const { password } = formLoginQRCode.getFieldsValue()
setLoadingLogin(true)
const { data } = await req.post('/auth/qrCodeSignIn', { password, session: qrCode?.session })
try {
req.post('/users/me/paymentSync')
} catch (error) {
// ignore
}
notification.success({
message: 'Success',
description: `Welcome back, ${data.user.name || data.user.username}! Please wait a moment...`
Expand Down Expand Up @@ -170,6 +180,11 @@ const Login: React.FC<Props> = ({ me }) => {
'Authorization': `Bearer ${qrCode.accessToken}`
} }).then(({ data }) => {
if (data?.user) {
try {
req.post('/users/me/paymentSync')
} catch (error) {
// ignore
}
notification.success({
message: 'Success',
description: `Welcome back, ${data.user.name || data.user.username}! Please wait a moment...`
Expand Down

0 comments on commit d502ca3

Please sign in to comment.