Skip to content

Commit

Permalink
admin apis
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan-g committed Sep 24, 2023
1 parent 431e0e0 commit beba7a7
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 136 deletions.
239 changes: 117 additions & 122 deletions src/features/need/need.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
ForbiddenException,
Get,
Param,
Patch,
Req,
} from '@nestjs/common';
import { ApiHeader, ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';
Expand All @@ -12,16 +11,12 @@ import { NeedService } from './need.service';
import { isAuthenticated } from 'src/utils/auth';
import {
FlaskUserTypesEnum,
PanelContributors,
PaymentStatusEnum,
SAYPlatformRoles,
SUPER_ADMIN_ID,
} from 'src/types/interfaces/interface';
import config from 'src/config';
import { convertFlaskToSayRoles, daysDifference } from 'src/utils/helpers';
import { NeedStatusUpdatesAPIApi } from 'src/generated-sources/openapi';
import { SyncService } from '../sync/sync.service';
import { ServerError } from 'src/filters/server-exception.filter';

@ApiTags('Needs')
@ApiSecurity('flask-access-token')
Expand Down Expand Up @@ -252,127 +247,127 @@ export class NeedController {
}

// temp
@Patch(`contributors/`)
async updateNeedContributor(@Req() req: Request) {
const panelFlaskUserId = req.headers['panelFlaskUserId'];
const panelFlaskTypeId = req.headers['panelFlaskTypeId'];
if (
!isAuthenticated(panelFlaskUserId, panelFlaskTypeId) ||
panelFlaskTypeId !== FlaskUserTypesEnum.SUPER_ADMIN
) {
throw new ForbiddenException(403, 'You Are not the Super admin');
}
const accessToken = config().dataCache.fetchPanelAuthentication(25).token;
// @Patch(`contributors/`)
// async updateNeedContributor(@Req() req: Request) {
// const panelFlaskUserId = req.headers['panelFlaskUserId'];
// const panelFlaskTypeId = req.headers['panelFlaskTypeId'];
// if (
// !isAuthenticated(panelFlaskUserId, panelFlaskTypeId) ||
// panelFlaskTypeId !== FlaskUserTypesEnum.SUPER_ADMIN
// ) {
// throw new ForbiddenException(403, 'You Are not the Super admin');
// }
// const accessToken = config().dataCache.fetchPanelAuthentication(25).token;

const allNeeds = await this.needService.getNeeds();
const filteredNeeds = allNeeds.filter(
(n) =>
!n.purchaser ||
!n.auditor ||
n.purchaser.flaskUserId === 28 ||
n.auditor.flaskUserId === 28,
);
let purchaserId: number;
try {
for await (const need of filteredNeeds) {
const flaskNeed = await this.needService.getFlaskNeed(need.flaskId);
const statusApi = new NeedStatusUpdatesAPIApi();
const statuses = await statusApi.apiV2NeedStatusUpdatesGet(
accessToken,
null,
null,
null,
flaskNeed.id,
);
// const allNeeds = await this.needService.getNeeds();
// const filteredNeeds = allNeeds.filter(
// (n) =>
// !n.purchaser ||
// !n.auditor ||
// n.purchaser.flaskUserId === 28 ||
// n.auditor.flaskUserId === 28,
// );
// let purchaserId: number;
// try {
// for await (const need of filteredNeeds) {
// const flaskNeed = await this.needService.getFlaskNeed(need.flaskId);
// const statusApi = new NeedStatusUpdatesAPIApi();
// const statuses = await statusApi.apiV2NeedStatusUpdatesGet(
// accessToken,
// null,
// null,
// null,
// flaskNeed.id,
// );

if (!statuses) {
// we do not have a history of purchaser id before implementing our new features
if (new Date(flaskNeed.doneAt).getFullYear() < 2023) {
purchaserId = 31; // Nyaz
}
if (
new Date(flaskNeed.doneAt).getFullYear() === 2023 &&
new Date(flaskNeed.doneAt).getMonth() <= 3
) {
purchaserId = 21; // Neda
}
} else {
purchaserId = statuses.find(
(s) => s.oldStatus === PaymentStatusEnum.COMPLETE_PAY,
)?.swId;
}
// if (!statuses) {
// // we do not have a history of purchaser id before implementing our new features
// if (new Date(flaskNeed.doneAt).getFullYear() < 2023) {
// purchaserId = 31; // Nyaz
// }
// if (
// new Date(flaskNeed.doneAt).getFullYear() === 2023 &&
// new Date(flaskNeed.doneAt).getMonth() <= 3
// ) {
// purchaserId = 21; // Neda
// }
// } else {
// purchaserId = statuses.find(
// (s) => s.oldStatus === PaymentStatusEnum.COMPLETE_PAY,
// )?.swId;
// }

let purchaser = await this.userService.getContributorByFlaskId(
purchaserId,
PanelContributors.PURCHASER,
);
// let purchaser = await this.userService.getContributorByFlaskId(
// purchaserId,
// PanelContributors.PURCHASER,
// );

if (!purchaser) {
const flaskPurchaser = await this.userService.getFlaskSocialWorker(
purchaserId,
);
// if (!purchaser) {
// const flaskPurchaser = await this.userService.getFlaskSocialWorker(
// purchaserId,
// );

const purchaserDetails = {
typeId: flaskPurchaser.type_id,
firstName: flaskPurchaser.firstName,
lastName: flaskPurchaser.lastName,
avatarUrl: flaskPurchaser.avatar_url,
flaskUserId: flaskPurchaser.id,
birthDate:
flaskPurchaser.birth_date && new Date(flaskPurchaser.birth_date),
panelRole: PanelContributors.AUDITOR,
userName: flaskPurchaser.userName,
};
const purchaserNgo = await this.syncService.syncContributorNgo(
flaskPurchaser,
);
console.log('\x1b[36m%s\x1b[0m', 'Creating an auditor ...\n');
purchaser = await this.userService.createContributor(
purchaserDetails,
purchaserNgo,
);
}
let auditor = await this.userService.getContributorByFlaskId(
flaskNeed.confirmUser,
PanelContributors.AUDITOR,
);
if (!auditor) {
const flaskAuditor = await this.userService.getFlaskSocialWorker(
flaskNeed.confirmUser,
);
// const purchaserDetails = {
// typeId: flaskPurchaser.type_id,
// firstName: flaskPurchaser.firstName,
// lastName: flaskPurchaser.lastName,
// avatarUrl: flaskPurchaser.avatar_url,
// flaskUserId: flaskPurchaser.id,
// birthDate:
// flaskPurchaser.birth_date && new Date(flaskPurchaser.birth_date),
// panelRole: PanelContributors.AUDITOR,
// userName: flaskPurchaser.userName,
// };
// const purchaserNgo = await this.syncService.syncContributorNgo(
// flaskPurchaser,
// );
// console.log('\x1b[36m%s\x1b[0m', 'Creating an auditor ...\n');
// purchaser = await this.userService.createContributor(
// purchaserDetails,
// purchaserNgo,
// );
// }
// let auditor = await this.userService.getContributorByFlaskId(
// flaskNeed.confirmUser,
// PanelContributors.AUDITOR,
// );
// if (!auditor) {
// const flaskAuditor = await this.userService.getFlaskSocialWorker(
// flaskNeed.confirmUser,
// );

const auditorDetails = {
typeId: flaskAuditor.type_id,
firstName: flaskAuditor.firstName,
lastName: flaskAuditor.lastName,
avatarUrl: flaskAuditor.avatar_url,
flaskUserId: flaskAuditor.id,
birthDate:
flaskAuditor.birth_date && new Date(flaskAuditor.birth_date),
panelRole: PanelContributors.AUDITOR,
userName: flaskAuditor.userName,
};
const auditorNgo = await this.syncService.syncContributorNgo(
flaskAuditor,
);
console.log('\x1b[36m%s\x1b[0m', 'Creating an auditor ...\n');
auditor = await this.userService.createContributor(
auditorDetails,
auditorNgo,
);
}
if (!auditor || !purchaser) {
throw new ServerError('huuuuh');
}
console.log(flaskNeed);
await this.needService.updateNeedContributors(
need.id,
auditor,
purchaser,
);
}
} catch (e) {
console.log(e);
}
}
// const auditorDetails = {
// typeId: flaskAuditor.type_id,
// firstName: flaskAuditor.firstName,
// lastName: flaskAuditor.lastName,
// avatarUrl: flaskAuditor.avatar_url,
// flaskUserId: flaskAuditor.id,
// birthDate:
// flaskAuditor.birth_date && new Date(flaskAuditor.birth_date),
// panelRole: PanelContributors.AUDITOR,
// userName: flaskAuditor.userName,
// };
// const auditorNgo = await this.syncService.syncContributorNgo(
// flaskAuditor,
// );
// console.log('\x1b[36m%s\x1b[0m', 'Creating an auditor ...\n');
// auditor = await this.userService.createContributor(
// auditorDetails,
// auditorNgo,
// );
// }
// if (!auditor || !purchaser) {
// throw new ServerError('huuuuh');
// }
// console.log(flaskNeed);
// await this.needService.updateNeedContributors(
// need.id,
// auditor,
// purchaser,
// );
// }
// } catch (e) {
// console.log(e);
// }
// }
}
38 changes: 35 additions & 3 deletions src/features/wallet/wallet.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,23 @@ export class WalletController {
) {
throw new WalletExceptionFilter(401, 'You are not the admin');
}
return await this.walletService.getSignatures();
try {
const X_LIMIT = parseInt(req.headers['x-limit']);
const X_TAKE = parseInt(req.headers['x-take']);
const limit = X_LIMIT > 100 ? 100 : X_LIMIT;
const page = X_TAKE * limit;

const allSignatures = await this.walletService.getSignatures({
skip: page,
take: limit,
});
return {
signatures: allSignatures[0],
total: allSignatures[1],
};
} catch (e) {
throw new ServerError(e.message);
}
}

@Get(`signatures/:flaskUserId`)
Expand All @@ -786,12 +802,28 @@ export class WalletController {
@Req() req: Request,
@Param('flaskUserId') flaskUserId: number,
) {

const panelFlaskUserId = Number(req.headers['panelFlaskUserId']);
if (panelFlaskUserId !== Number(flaskUserId)) {
throw new WalletExceptionFilter(401, 'You only can get your signatures');
}
return await this.walletService.getUserSignatures(panelFlaskUserId);

const X_LIMIT = parseInt(req.headers['x-limit']);
const X_TAKE = parseInt(req.headers['x-take']);
const limit = X_LIMIT > 100 ? 100 : X_LIMIT;
const page = X_TAKE * limit;

const userSignatures = await this.walletService.getUserSignatures(
{
skip: page,
take: limit,
},
panelFlaskUserId,
);

return {
signatures: userSignatures[0],
total: userSignatures[1],
};
}

@Delete(`signature/:signature`)
Expand Down
Loading

0 comments on commit beba7a7

Please sign in to comment.