Skip to content

Commit

Permalink
preregister updated
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan-g committed Oct 21, 2024
1 parent 0b73519 commit 789bf58
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 136 deletions.
1 change: 1 addition & 0 deletions src/features/campaign/campaign.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,5 @@ export class CampaignController {
await this.campaignService.sendNewsLetter(body);
}
}

}
264 changes: 188 additions & 76 deletions src/features/children/children.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
UploadedFiles,
Delete,
Res,
BadRequestException,
} from '@nestjs/common';
import { ApiHeader, ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';
import { ChildrenService } from './children.service';
Expand Down Expand Up @@ -63,7 +64,7 @@ import {
} from 'src/utils/helpers';
import axios from 'axios';
import { AllUserEntity } from 'src/entities/user.entity';
import { checkIfFileOrDirectoryExists, moveFile } from 'src/utils/file';
import { checkIfDirectoryExists, getCurrentFilenames, moveFile, renameFile } from 'src/utils/file';
import fs from 'fs';
import { CampaignService } from '../campaign/campaign.service';
import { File } from '@web-std/file';
Expand All @@ -87,7 +88,7 @@ export class ChildrenController {
private locationService: LocationService,
private downloadService: DownloadService,
private campaignService: CampaignService,
) {}
) { }
@Get(`preregister/:childFlaskId`)
@ApiOperation({ description: 'Get child preregister' })
async getChildPreregister(
Expand Down Expand Up @@ -285,35 +286,6 @@ export class ChildrenController {
}
}

@Delete(`preregister/:id`)
@ApiOperation({ description: 'Delete a pre register' })
async deletePreRegister(@Req() req: Request, @Param('id') id: string) {
const panelFlaskUserId = req.headers['panelFlaskUserId'];
const panelFlaskTypeId = req.headers['panelFlaskTypeId'];
if (
!isAuthenticated(panelFlaskUserId, panelFlaskTypeId) ||
!(
panelFlaskTypeId === FlaskUserTypesEnum.SUPER_ADMIN ||
panelFlaskTypeId === FlaskUserTypesEnum.ADMIN
)
) {
throw new ForbiddenException('You Are not the Super admin');
}

try {
const preRegister = await this.childrenService.getChildPreRegisterById(
id,
);

if (preRegister.status === PreRegisterStatusEnum.CONFIRMED) {
throw new ForbiddenException('This Child has been confirmed');
}
await this.childrenService.deletePreRegister(preRegister.id);
} catch (e) {
throw new ServerError(e.message, e.status);
}
}

@Post(`preregister`)
@ApiOperation({ description: 'Create children pre register' })
@UseInterceptors(
Expand All @@ -326,7 +298,7 @@ export class ChildrenController {
),
)
@UsePipes(new ValidationPipe())
async preRegisterAdd(
async preRegisterAdminCreate(
@Req() req: Request,
@UploadedFiles()
files: {
Expand All @@ -350,66 +322,194 @@ export class ChildrenController {
throw new ForbiddenException('You Are not the Authorized!');
}

if (!files) {
throw new ServerError('No files were uploaded!');
if (!files || !files.awakeFile || !files.sleptFile) {
throw new BadRequestException('No files were uploaded!');
}

// for local purposes - organized folders and files
if (process.env.NODE_ENV === 'development') {
const newChildFolder = `../../Docs/children${
Number(body.sex) === SexEnum.MALE ? '/boys/' : '/girls/'
}organized/${capitalizeFirstLetter(body.sayNameEn)}-${body.sayNameFa}`;

const originalAwakeGirl = `../../Docs/children/girls/${
files.awakeFile[0].filename.split('-s-')[0]
}.png`;
const originalAwakeBoy = `../../Docs/children/boys/${
files.awakeFile[0].filename.split('-s-')[0]
}.png`;
const originalSleptGirl = `../../Docs/children/girls/${
files.sleptFile[0].filename.split('-s-')[0]
}.png`;
const originalSleptBoy = `../../Docs/children/boys/${
files.sleptFile[0].filename.split('-s-')[0]
}.png`;
const preRegistersByName = await this.childrenService.getPreChildrenByName(body.sayNameFa)

if (preRegistersByName && preRegistersByName.length > 0) {
throw new BadRequestException('Can not have similar names!');
}

const originalAwakeGirl = `../../Docs/children/girls/${files.awakeFile[0].filename.split('-s-')[0]
}.png`;
const originalAwakeBoy = `../../Docs/children/boys/${files.awakeFile[0].filename.split('-s-')[0]
}.png`;
const originalSleptGirl = `../../Docs/children/girls/${files.sleptFile[0].filename.split('-s-')[0]
}.png`;
const originalSleptBoy = `../../Docs/children/boys/${files.sleptFile[0].filename.split('-s-')[0]
}.png`;


const newAwakeName = `awake-${body.sayNameEn.toLowerCase()}.png`;
const newSleepName = `sleep-${body.sayNameEn.toLowerCase()}.png`;

if (
checkIfFileOrDirectoryExists(originalAwakeGirl) ||
checkIfFileOrDirectoryExists(originalAwakeBoy)
) {
if (!checkIfFileOrDirectoryExists(newChildFolder)) {
fs.mkdirSync(newChildFolder);
try {

let preRegister: ChildrenPreRegisterEntity
if (
checkIfDirectoryExists(originalAwakeGirl) ||
checkIfDirectoryExists(originalAwakeBoy)
) {
preRegister = await this.childrenService.createPreRegisterChild(
files.awakeFile[0].filename,
files.sleptFile[0].filename,
{ fa: body.sayNameFa, en: body.sayNameEn },
body.sex,
);

const newChildFolder = `../../Docs/children${Number(body.sex) === SexEnum.MALE ? '/boys/' : '/girls/'
}organized/${capitalizeFirstLetter(body.sayNameEn)}_${preRegister.id}`;

if (!checkIfDirectoryExists(newChildFolder)) {
console.log('Creating the child organized folder ...');
fs.mkdirSync(newChildFolder);
if (!checkIfDirectoryExists(newChildFolder)) {
throw new ServerError('could not find the folder');
}
}
if (Number(body.sex) === SexEnum.MALE) {
moveFile(originalAwakeBoy, `${newChildFolder}/${newAwakeName}`);
moveFile(originalSleptBoy, `${newChildFolder}/${newSleepName}`);
}
if (Number(body.sex) === SexEnum.FEMALE) {
moveFile(originalAwakeGirl, `${newChildFolder}/${newAwakeName}`);
moveFile(originalSleptGirl, `${newChildFolder}/${newSleepName}`);
}
} else {
throw new ServerError('could not find the file');
}
if (Number(body.sex) === SexEnum.MALE) {
moveFile(originalAwakeBoy, `${newChildFolder}/${newAwakeName}`);
moveFile(originalSleptBoy, `${newChildFolder}/${newSleepName}`);
return preRegister;


} catch (e) {
throw new ServerError(e.msg);
}
}
}

@Delete(`preregister/:id`)
@ApiOperation({ description: 'Delete a pre register' })
async deletePreRegister(@Req() req: Request, @Param('id') id: string) {
const panelFlaskUserId = req.headers['panelFlaskUserId'];
const panelFlaskTypeId = req.headers['panelFlaskTypeId'];
if (
!isAuthenticated(panelFlaskUserId, panelFlaskTypeId) ||
!(
panelFlaskTypeId === FlaskUserTypesEnum.SUPER_ADMIN ||
panelFlaskTypeId === FlaskUserTypesEnum.ADMIN
)
) {
throw new ForbiddenException('You Are not the Super admin');
}

try {
const preRegister = await this.childrenService.getChildPreRegisterById(
id,
);

if (preRegister.status === PreRegisterStatusEnum.CONFIRMED) {
throw new ForbiddenException('This Child has been confirmed');
}
await this.childrenService.deletePreRegister(preRegister.id);
} catch (e) {
throw new ServerError(e.message, e.status);
}
}

@Get(`complete-delete`)
@ApiOperation({
description: 'After delete we need to move back the avatars to their folders',
})
@UsePipes(new ValidationPipe())
async preRegisterCompleteDelete(@Req() req: Request) {
const panelFlaskUserId = req.headers['panelFlaskUserId'];
const panelFlaskTypeId = req.headers['panelFlaskTypeId'];
if (
!isAuthenticated(panelFlaskUserId, panelFlaskTypeId) ||
!(
panelFlaskTypeId === FlaskUserTypesEnum.SUPER_ADMIN ||
panelFlaskTypeId === FlaskUserTypesEnum.ADMIN
)
) {
throw new ForbiddenException('You Are not the Authorized!');
}
// for local purposes - organized folders and files
if (process.env.NODE_ENV === 'development') {
try {
const token =
config().dataCache.fetchPanelAuthentication(panelFlaskUserId).token;
const configs = {
headers: {
'Content-Type': 'multipart/form-data',
Authorization: token,
contentType: false,
flaskId: panelFlaskUserId,
'X-TAKE': 0,
'X-LIMIT': 100,
},
};
const result1 = await axios.get(
`https://nest.saydao.org/api/dao/children/preregister/all/${PreRegisterStatusEnum.PRE_REGISTERED}`,
configs,
);
const result2 = await axios.get(
`https://nest.saydao.org/api/dao/children/preregister/all/${PreRegisterStatusEnum.CONFIRMED}`,
configs,
);
const result3 = await axios.get(
`https://nest.saydao.org/api/dao/children/preregister/all/${PreRegisterStatusEnum.NOT_REGISTERED}`,
configs,
);
const allPreRegisters = result1.data.data.concat(result2.data.data).concat(result3.data.data);

// remove the path which has a preregister id. return the paths left in array -> those which were deleted, ...
function removeItemOnce(arr: any[], value: string) {
const index = arr.indexOf(value);
if (index > -1) {
arr.splice(index, 1);
}
return arr;
}
if (Number(body.sex) === SexEnum.FEMALE) {
moveFile(originalAwakeGirl, `${newChildFolder}/${newAwakeName}`);
moveFile(originalSleptGirl, `${newChildFolder}/${newSleepName}`);
let path: string;
const boysFiles = fs.readdirSync(`../../Docs/children/boys/organized`);
const girlsFiles = fs.readdirSync(`../../Docs/children/girls/organized`);
const filesDir = boysFiles.concat(girlsFiles);

for (const p of allPreRegisters) {
path = filesDir.find(
(d) => d.split(`_`)[1] === p.id
);
if (!path) {
// This one is deleted and we need to restore the child folder
continue
} else {
removeItemOnce(filesDir, path)
}
}
} else {
throw new ServerError('could not find the file');

if (girlsFiles.length + boysFiles.length !== (boysFiles.concat(girlsFiles)).length) {
throw new ServerError('The arrays are different');
}

return { "FoldersToBeManaged": filesDir };
} catch (e) {
console.log(e);
throw new ServerError(e.message);
}
}
return await this.childrenService.createPreRegisterChild(
files.awakeFile[0].filename,
files.sleptFile[0].filename,
{ fa: body.sayNameFa, en: body.sayNameEn },
body.sex,
);
}

@ApiOperation({
description: 'NGO / SW add a child by updating pre register',
})
@Patch(`preregister/prepare`)
@Patch(`preregister/assign`)
@UsePipes(new ValidationPipe())
@UseInterceptors(FileInterceptor('voiceFile', voiceStorage))
async preRegisterPrepare(
async preRegisterAssignChild(
@Req() req: Request,
@UploadedFile() voiceFile,
@Body(ValidateChildPipe) body: PreparePreRegisterChildDto,
Expand Down Expand Up @@ -521,7 +621,7 @@ export class ChildrenController {
if (!nestSocialWorker || !ngo || !contributor) {
throw new ServerError('This social worker has not contributed yet');
}
return await this.childrenService.preRegisterPrepare(
return await this.childrenService.preRegisterAssignChild(
candidate.id,
{
phoneNumber: body.phoneNumber,
Expand Down Expand Up @@ -862,8 +962,8 @@ export class ChildrenController {
const found = names.filter((n) =>
lang === 'en'
? n.en.toUpperCase() === newName.toUpperCase() ||
(n.en &&
n.en.slice(-3).toUpperCase() === newName.slice(-3).toUpperCase())
(n.en &&
n.en.slice(-3).toUpperCase() === newName.slice(-3).toUpperCase())
: n.fa === newName || (n.fa && n.fa.slice(-3) === newName.slice(-3)),
);

Expand Down Expand Up @@ -979,13 +1079,25 @@ export class ChildrenController {
return await this.childrenService.getChildNeedsSummery(token, childId);
}

@Get(`no-need`)
@ApiOperation({ description: 'Get children with no need' })
async childrenWithNoNeed(@Req() req: Request) {
const panelFlaskUserId = req.headers['panelFlaskUserId'];
const panelFlaskTypeId = req.headers['panelFlaskTypeId'];
if (!isAuthenticated(panelFlaskUserId, panelFlaskTypeId)) {
throw new ForbiddenException('You Are not authorized');
}

return await this.campaignService.childrenWithNoNeed();
}

@Get('/network')
getAvailableContributions(@Req() req: Request) {
const dappFlaskUserId = req.headers['dappFlaskUserId'];
if (!isAuthenticated(dappFlaskUserId, FlaskUserTypesEnum.FAMILY)) {
throw new ForbiddenException('You Are not authorized');
}
return this.childrenService.gtTheNetwork();
return this.childrenService.getTheNetwork();
}

@Get('avatars/images/:fileName')
Expand Down
Loading

0 comments on commit 789bf58

Please sign in to comment.