Skip to content

Commit

Permalink
optimising payment
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan-g committed Oct 13, 2024
1 parent 97dc06a commit 0b73519
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 67 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ src/features/midjourney/bad-images-to-remove.json
# Tests
/coverage
/.nyc_output
temp.json

# IDEs and editors
/.idea
Expand Down
8 changes: 4 additions & 4 deletions src/features/children/children.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
UpdateApprovedPreRegisterDto,
UpdatePreRegisterChildDto,
} from 'src/types/dtos/CreateChild.dto';
import { ValidateChildTsPipe } from './pipes/validate-child.ts/validate-child.ts.pipe';
import { ValidateChildPipe } from './pipes/validate-child.ts/validate-child.ts.pipe';
import {
FileFieldsInterceptor,
FileInterceptor,
Expand Down Expand Up @@ -121,7 +121,7 @@ export class ChildrenController {
@Req() req: Request,
@UploadedFile() voiceFile: Express.Multer.File,
@Param('id') id: string,
@Body(ValidateChildTsPipe) body: CreateFlaskChildDto,
@Body(ValidateChildPipe) body: CreateFlaskChildDto,
) {
const panelFlaskUserId = req.headers['panelFlaskUserId'];
const panelFlaskTypeId = req.headers['panelFlaskTypeId'];
Expand Down Expand Up @@ -412,7 +412,7 @@ export class ChildrenController {
async preRegisterPrepare(
@Req() req: Request,
@UploadedFile() voiceFile,
@Body(ValidateChildTsPipe) body: PreparePreRegisterChildDto,
@Body(ValidateChildPipe) body: PreparePreRegisterChildDto,
) {
const panelFlaskUserId = req.headers['panelFlaskUserId'];
const panelFlaskTypeId = req.headers['panelFlaskTypeId'];
Expand Down Expand Up @@ -560,7 +560,7 @@ export class ChildrenController {
async preRegisterUpdate(
@Req() req: Request,
@UploadedFile() voiceFile,
@Body(ValidateChildTsPipe) body: UpdatePreRegisterChildDto,
@Body(ValidateChildPipe) body: UpdatePreRegisterChildDto,
) {
const panelFlaskUserId = req.headers['panelFlaskUserId'];
const panelFlaskTypeId = req.headers['panelFlaskTypeId'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ArgumentMetadata, HttpException, HttpStatus, Injectable, PipeTransform
import { Children } from 'src/types/interfaces/Children';

@Injectable()
export class ValidateChildTsPipe implements PipeTransform {
export class ValidateChildPipe implements PipeTransform {
transform(value: Children, metadata: ArgumentMetadata) {
console.log("Validating Children...")

Expand Down
6 changes: 3 additions & 3 deletions src/features/midjourney/midjourney.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { rimraf } from 'rimraf';
import { isAuthenticated } from 'src/utils/auth';
import {
FlaskUserTypesEnum,
SUPER_ADMIN_ID,
SUPER_ADMIN_ID_PANEL,
} from 'src/types/interfaces/interface';
import { WalletExceptionFilter } from 'src/filters/wallet-exception.filter';
import { checkIfFileOrDirectoryExists } from 'src/utils/file';
Expand Down Expand Up @@ -241,7 +241,7 @@ export class MidjourneyController {
if (
!isAuthenticated(panelFlaskUserId, panelFlaskTypeId) ||
panelFlaskTypeId !== FlaskUserTypesEnum.SUPER_ADMIN ||
panelFlaskUserId !== SUPER_ADMIN_ID
panelFlaskUserId !== SUPER_ADMIN_ID_PANEL
) {
throw new WalletExceptionFilter(403, 'You Are not the Super admin');
}
Expand Down Expand Up @@ -285,7 +285,7 @@ export class MidjourneyController {
if (
!isAuthenticated(panelFlaskUserId, panelFlaskTypeId) ||
panelFlaskTypeId !== FlaskUserTypesEnum.SUPER_ADMIN ||
panelFlaskUserId !== SUPER_ADMIN_ID
panelFlaskUserId !== SUPER_ADMIN_ID_PANEL
) {
throw new WalletExceptionFilter(403, 'You Are not the Super admin');
}
Expand Down
18 changes: 11 additions & 7 deletions src/features/need/need.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
FlaskUserTypesEnum,
NeedTypeEnum,
ProductStatusEnum,
SUPER_ADMIN_ID,
SUPER_ADMIN_ID_PANEL,
} from 'src/types/interfaces/interface';
import config from 'src/config';
import { daysDifference, timeDifference } from 'src/utils/helpers';
Expand Down Expand Up @@ -176,10 +176,14 @@ export class NeedController {
) {
const panelFlaskUserId = req.headers['panelFlaskUserId'];
const panelFlaskTypeId = req.headers['panelFlaskTypeId'];

if (!isAuthenticated(panelFlaskUserId, panelFlaskTypeId)) {
throw new ForbiddenException(401, 'You Are not authorized!');
}
return await this.needService.getNeedByFlaskId(needFlaskId);

const theNeed = await this.needService.getNeedByFlaskId(needFlaskId)

return theNeed;
}

@Get(`relation/:needFlaskId`)
Expand Down Expand Up @@ -356,7 +360,7 @@ export class NeedController {
}

const superAdmin = await this.userService.getUserByFlaskId(
SUPER_ADMIN_ID,
SUPER_ADMIN_ID_PANEL,
);

const validatedNeed = await validateNeed(fetchedNeed, superAdmin);
Expand Down Expand Up @@ -559,7 +563,7 @@ export class NeedController {
if (
!isAuthenticated(panelFlaskUserId, panelFlaskTypeId) ||
panelFlaskTypeId !== FlaskUserTypesEnum.SUPER_ADMIN ||
panelFlaskUserId !== SUPER_ADMIN_ID
panelFlaskUserId !== SUPER_ADMIN_ID_PANEL
) {
throw new ForbiddenException('You Are not the Super admin');
}
Expand All @@ -577,7 +581,7 @@ export class NeedController {
if (
!isAuthenticated(panelFlaskUserId, panelFlaskTypeId) ||
panelFlaskTypeId !== FlaskUserTypesEnum.SUPER_ADMIN ||
panelFlaskUserId !== SUPER_ADMIN_ID
panelFlaskUserId !== SUPER_ADMIN_ID_PANEL
) {
throw new ForbiddenException('You Are not the Super admin');
}
Expand All @@ -596,7 +600,7 @@ export class NeedController {
if (
!isAuthenticated(panelFlaskUserId, panelFlaskTypeId) ||
panelFlaskTypeId !== FlaskUserTypesEnum.SUPER_ADMIN ||
panelFlaskUserId !== SUPER_ADMIN_ID
panelFlaskUserId !== SUPER_ADMIN_ID_PANEL
) {
throw new ForbiddenException('You Are not the Super admin');
}
Expand All @@ -619,7 +623,7 @@ export class NeedController {
if (
!isAuthenticated(panelFlaskUserId, panelFlaskTypeId) ||
panelFlaskTypeId !== FlaskUserTypesEnum.SUPER_ADMIN ||
panelFlaskUserId !== SUPER_ADMIN_ID
panelFlaskUserId !== SUPER_ADMIN_ID_PANEL
) {
throw new ForbiddenException('You Are not the Super admin');
}
Expand Down
8 changes: 3 additions & 5 deletions src/features/payment/payment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,19 @@ export class PaymentController {
Authorization: token,
},
};
try {
try {
// create flask payment
const { data } = await axios.post(
'https://api.sayapp.company/api/v2/payment',
{
need_id: Number(body.needId),
amount: Number(body.amount),
donate: Number(body.donation),
use_credit: Boolean(body.useCredit),
use_credit: body.useCredit,
gateWay: Number(body.gateWay),
},
configs,
);
console.log(data);
return data;
} catch (e) {
console.log(e);
Expand Down Expand Up @@ -121,12 +120,11 @@ export class PaymentController {
'https://api.sayapp.company/api/v2/mycart/payment',
{
donation: Number(body.donation),
use_credit: Boolean(body.useCredit),
use_credit: body.useCredit,
gateWay: Number(body.gateWay),
},
configs,
);
console.log(data);
return data;
} catch (e) {
console.log(e);
Expand Down
4 changes: 2 additions & 2 deletions src/features/payment/payment.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export class PaymentModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(PaymentMiddleware)
.exclude({ path: 'payment/verify', method: RequestMethod.GET })
.exclude({ path: 'payment/verify/cart', method: RequestMethod.GET })
.exclude({ path: 'payment/new/cart', method: RequestMethod.POST })
.exclude({ path: 'payment/verify(.*)', method: RequestMethod.GET })
.forRoutes(PaymentController);
}
}
4 changes: 2 additions & 2 deletions src/features/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ServerError } from '../../filters/server-exception.filter';
import {
FlaskUserTypesEnum,
SAYPlatformRoles,
SUPER_ADMIN_ID,
SUPER_ADMIN_ID_PANEL,
} from 'src/types/interfaces/interface';
import {
convertFlaskToSayRoles,
Expand Down Expand Up @@ -420,7 +420,7 @@ export class UserController {
if (
!isAuthenticated(panelFlaskUserId, panelFlaskTypeId) ||
panelFlaskTypeId !== FlaskUserTypesEnum.SUPER_ADMIN ||
panelFlaskUserId !== SUPER_ADMIN_ID
panelFlaskUserId !== SUPER_ADMIN_ID_PANEL
) {
throw new ForbiddenException('You Are not the Super admin');
}
Expand Down
4 changes: 2 additions & 2 deletions src/features/wallet/wallet.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
FlaskUserTypesEnum,
PanelContributors,
SAYPlatformRoles,
SUPER_ADMIN_ID,
SUPER_ADMIN_ID_PANEL,
SwSignatureResult,
eEthereumNetworkChainId,
} from '../../types/interfaces/interface';
Expand Down Expand Up @@ -842,7 +842,7 @@ export class WalletController {
if (
!isAuthenticated(panelFlaskUserId, panelFlaskTypeId) ||
panelFlaskTypeId !== FlaskUserTypesEnum.SUPER_ADMIN ||
panelFlaskUserId !== SUPER_ADMIN_ID
panelFlaskUserId !== SUPER_ADMIN_ID_PANEL
) {
throw new WalletExceptionFilter(403, 'You Are not the Super admin');
}
Expand Down
1 change: 1 addition & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async function startServer() {
'Access-Control-Allow-Origin',
'flaskUserId',
'flaskId',
'flaskDappId'
],

methods: ['GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS'],
Expand Down
3 changes: 2 additions & 1 deletion src/types/interfaces/interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const SAY_DAPP_ID = 208;
export const SUPER_ADMIN_ID = 25;
export const SUPER_ADMIN_ID_PANEL = 25;
export const SUPER_ADMIN_ID_DAPP = 115;
export enum eEthereumNetworkChainId {
mainnet = 1,
goerli = 5,
Expand Down
48 changes: 24 additions & 24 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,44 @@ import config from 'src/config';
import { ServerError } from 'src/filters/server-exception.filter';
import { SocialWorkerAPIApi, UserAPIApi } from 'src/generated-sources/openapi';
import { convertFlaskToSayRoles, timeDifference } from './helpers';
import { FlaskUserTypesEnum } from 'src/types/interfaces/interface';
import {
FlaskUserTypesEnum,
} from 'src/types/interfaces/interface';

export async function updateFlaskCacheAuthentication(req, logger: Logger) {
logger.warn('Passing through MiddleWare...');

const accessToken = req.headers['authorization'];
const requestFlaskId = Number(req.headers['flaskid']);
const requestDappFlaskId = Number(req.headers['flaskdappid']);
const requestPanelFlaskId = Number(req.headers['flaskid']);

if (!accessToken || !requestFlaskId) {
console.log(requestDappFlaskId);
console.log(accessToken);
if (!accessToken || (!requestPanelFlaskId && !requestDappFlaskId)) {
throw new ForbiddenException('Access Token and the ID is required!');
}


try {
// for Dapp
if (
String(req.headers.origin) === 'http://localhost:3002' ||
String(req.headers.origin) === 'https://dapp.saydao.org' ||
String(req.headers.origin) === 'https://beta.sayapp.company'
) {
if (requestDappFlaskId) {
// If in Cache
logger.log('fetching dapp cache token...');
let fetched = config().dataCache.fetchDappAuthentication(requestFlaskId);
let fetched = config().dataCache.fetchDappAuthentication(requestDappFlaskId);
if (fetched) {
logger.log('fetched dapp cache token...');
if (
timeDifference(fetched.createdAt, new Date()).mm >= 1 ||
fetched.token !== accessToken
) {
logger.warn('removing old user token...');
config().dataCache.deleteDappAccessToken(requestFlaskId);
config().dataCache.expireDappAccessToken(requestDappFlaskId);
req.headers['dappFlaskUserId'] = '';
req.headers['flaskId'] = '';
fetched = config().dataCache.fetchDappAuthentication(requestFlaskId);
fetched = config().dataCache.fetchDappAuthentication(requestDappFlaskId);
} else {
logger.log('Got the cache token!...');
req.headers['dappFlaskUserId'] = requestFlaskId;
req.headers['dappFlaskUserId'] = requestDappFlaskId;
return;
}
}
Expand Down Expand Up @@ -69,29 +72,23 @@ export async function updateFlaskCacheAuthentication(req, logger: Logger) {
}
}
// for panel
else if (
String(req.headers.origin) === 'http://localhost:3000' ||
String(req.headers.origin) === 'http://localhost:8002' ||
String(req.headers.origin) === 'https://nest.saydao.org' ||
!req.headers.origin ||
String(req.headers.origin) === 'https://panel.saydao.org'
) {
else if (requestPanelFlaskId) {
logger.log('fetching panel cache token...');
let fetched = config().dataCache.fetchPanelAuthentication(requestFlaskId);
let fetched = config().dataCache.fetchPanelAuthentication(requestPanelFlaskId);
if (fetched) {
if (
timeDifference(fetched.createdAt, new Date()).mm > 1 ||
fetched.token !== accessToken
) {
logger.warn('removing old user token...');
config().dataCache.deletePanelAccessToken(requestFlaskId);
config().dataCache.expirePanelAccessToken(requestPanelFlaskId);
req.headers['panelFlaskTypeId'] = '';
req.headers['panelFlaskUserId'] = '';
req.headers['flaskId'] = '';
fetched = config().dataCache.fetchPanelAuthentication(requestFlaskId);
fetched = config().dataCache.fetchPanelAuthentication(requestPanelFlaskId);
} else {
logger.log('Got the cache token!...');
req.headers['panelFlaskUserId'] = requestFlaskId;
req.headers['panelFlaskUserId'] = requestPanelFlaskId;
req.headers['panelFlaskTypeId'] = fetched.flaskUserType;
return;
}
Expand All @@ -104,7 +101,7 @@ export async function updateFlaskCacheAuthentication(req, logger: Logger) {
const flaskApi = new SocialWorkerAPIApi();
const socialWorker = await flaskApi.apiV2SocialworkersIdGet(
accessToken,
Number(requestFlaskId),
Number(requestPanelFlaskId),
);
if (!socialWorker) {
throw new ForbiddenException('You Do not have Access!');
Expand Down Expand Up @@ -140,6 +137,9 @@ export function isAuthenticated(
userType: FlaskUserTypesEnum,
): boolean {
console.log('checking authentication...');
console.log(flaskUserId);
console.log(userType);

if (userType === FlaskUserTypesEnum.FAMILY) {
const dappAuthentication =
config().dataCache.fetchDappAuthentication(flaskUserId);
Expand Down
20 changes: 10 additions & 10 deletions src/utils/dataCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,16 @@ export default class DataCache {
}
};

deletePanelAccessToken = (flaskSwId: number) =>
(this.authentication[String(flaskSwId)] = {
...this.authentication[String(flaskSwId)],
isExpired: true,
});
deleteDappAccessToken = (flaskFamilyId: number) =>
(this.authentication[String(flaskFamilyId)] = {
...this.authentication[String(flaskFamilyId)],
isExpired: true,
});
expirePanelAccessToken = (flaskSwId: number) =>
(this.authentication[String(flaskSwId)] = {
...this.authentication[String(flaskSwId)],
isExpired: true,
});
expireDappAccessToken = (flaskFamilyId: number) =>
(this.authentication[String(flaskFamilyId)] = {
...this.authentication[String(flaskFamilyId)],
isExpired: true,
});

// panel analytic scatter chart
roleScatteredData() {
Expand Down
Loading

0 comments on commit 0b73519

Please sign in to comment.