From 5bf25c5dd11c9d3f5b74a9b6f913efb8f3c01f42 Mon Sep 17 00:00:00 2001 From: G Date: Sat, 20 Jul 2024 11:30:14 +0330 Subject: [PATCH] children entity changes --- src/db/migrations/1721227837097-new.ts | 14 ++ src/db/migrations/1721228640217-new.ts | 14 ++ src/db/migrations/1721228883744-new.ts | 14 ++ src/entities/children.entity.ts | 15 ++- src/features/children/children.controller.ts | 61 ++------- src/features/sync/sync.service.ts | 135 +++++++++++++++++++ 6 files changed, 203 insertions(+), 50 deletions(-) create mode 100644 src/db/migrations/1721227837097-new.ts create mode 100644 src/db/migrations/1721228640217-new.ts create mode 100644 src/db/migrations/1721228883744-new.ts diff --git a/src/db/migrations/1721227837097-new.ts b/src/db/migrations/1721227837097-new.ts new file mode 100644 index 000000000..942fada69 --- /dev/null +++ b/src/db/migrations/1721227837097-new.ts @@ -0,0 +1,14 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class New1721227837097 implements MigrationInterface { + name = 'New1721227837097' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "children_entity" ADD "familyCount" integer`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "children_entity" DROP COLUMN "familyCount"`); + } + +} diff --git a/src/db/migrations/1721228640217-new.ts b/src/db/migrations/1721228640217-new.ts new file mode 100644 index 000000000..be36684d4 --- /dev/null +++ b/src/db/migrations/1721228640217-new.ts @@ -0,0 +1,14 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class New1721228640217 implements MigrationInterface { + name = 'New1721228640217' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "children_entity" ADD "state" integer`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "children_entity" DROP COLUMN "state"`); + } + +} diff --git a/src/db/migrations/1721228883744-new.ts b/src/db/migrations/1721228883744-new.ts new file mode 100644 index 000000000..6a52aebf8 --- /dev/null +++ b/src/db/migrations/1721228883744-new.ts @@ -0,0 +1,14 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class New1721228883744 implements MigrationInterface { + name = 'New1721228883744' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "children_entity" ADD "schoolType" integer`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "children_entity" DROP COLUMN "schoolType"`); + } + +} diff --git a/src/entities/children.entity.ts b/src/entities/children.entity.ts index 18bc587f1..189182848 100644 --- a/src/entities/children.entity.ts +++ b/src/entities/children.entity.ts @@ -1,7 +1,11 @@ import { Entity, Column, Index, OneToMany, ManyToOne } from 'typeorm'; import { NeedEntity } from './need.entity'; import { BaseEntity } from './BaseEntity'; -import { EducationEnum, HousingEnum } from '../types/interfaces/interface'; +import { + EducationEnum, + HousingEnum, + SchoolTypeEnum, +} from '../types/interfaces/interface'; import { NgoEntity } from './ngo.entity'; import { ContributorEntity } from './contributor.entity'; @@ -34,6 +38,9 @@ export class ChildrenEntity extends BaseEntity { @Column({ nullable: true }) city: number; + @Column({ nullable: true }) + state: number; + @Column({ type: 'timestamptz', nullable: true }) confirmDate: Date; @@ -46,6 +53,9 @@ export class ChildrenEntity extends BaseEntity { @Column({ nullable: true }) education: EducationEnum; + @Column({ nullable: true }) + schoolType: SchoolTypeEnum; + @Column({ nullable: true }) existenceStatus: number; @@ -79,6 +89,9 @@ export class ChildrenEntity extends BaseEntity { @Column({ nullable: true }) sayFamilyCount: number; + @Column({ nullable: true }) + familyCount: number; + @Column({ nullable: true }) sayName: string; diff --git a/src/features/children/children.controller.ts b/src/features/children/children.controller.ts index 489b8be4c..9ba68c14e 100644 --- a/src/features/children/children.controller.ts +++ b/src/features/children/children.controller.ts @@ -56,7 +56,9 @@ import { NgoParams } from 'src/types/parameters/NgoParammeters'; import { capitalizeFirstLetter, convertFlaskToSayAppRoles, + convertFlaskToSayPanelRoles, convertFlaskToSayRoles, + formatDate, truncateString, } from 'src/utils/helpers'; import axios from 'axios'; @@ -66,6 +68,7 @@ import fs from 'fs'; import { CampaignService } from '../campaign/campaign.service'; import { File } from '@web-std/file'; import { ChildrenPreRegisterEntity } from 'src/entities/childrenPreRegister.entity'; +import { ObjectNotFound } from 'src/filters/notFound-expectation.filter'; @ApiTags('Children') @ApiSecurity('flask-access-token') @@ -181,7 +184,7 @@ export class ChildrenController { formData.append('city', String(preRegister.city)); formData.append('country', String(preRegister.country)); formData.append('phoneNumber', preRegister.phoneNumber); - formData.append('birthDate', String('2015-05-20')); + formData.append('birthDate', formatDate(preRegister.birthDate)); //'2015-05-20' formData.append( 'sayname_translations', JSON.stringify({ @@ -557,6 +560,7 @@ export class ChildrenController { @UsePipes(new ValidationPipe()) async preRegisterUpdateApproved( @Param('flaskChildId') flaskChildId: number, + @Body() body: { schoolType: SchoolTypeEnum; addedState: number }, @Req() req: Request, ) { const panelFlaskUserId = req.headers['panelFlaskUserId']; @@ -608,8 +612,8 @@ export class ChildrenController { phoneNumber: flaskChild.phoneNumber, address: flaskChild.address, country: Number(flaskChild.country), - city: Number(flaskChild.country), - state: Number(flaskChild.city), + city: Number(flaskChild.city), + state: Number(body.addedState), awakeUrl: flaskChild.awakeAvatarUrl, sleptUrl: flaskChild.sleptAvatarUrl, voiceUrl: flaskChild.voiceUrl, @@ -620,6 +624,7 @@ export class ChildrenController { }, housingStatus: Number(flaskChild.housingStatus), educationLevel: Number(flaskChild.education), + schoolType: body.schoolType, familyCount: Number(flaskChild.familyCount), }; console.log('\x1b[36m%s\x1b[0m', 'Updating the preRegister ...\n'); @@ -633,54 +638,12 @@ export class ChildrenController { preRegister = await this.childrenService.getChildrenPreRegisterByFlaskId( flaskChildId, ); - console.log(preRegister); - console.log('preRegister--------------------'); - - const childDetails = { - flaskId: flaskChild.id, - sayName: flaskChild.sayname_translations.en, - sayNameTranslations: flaskChild.sayname_translations, - nationality: flaskChild.nationality, - country: flaskChild.country, - city: flaskChild.city, - awakeAvatarUrl: flaskChild.awakeAvatarUrl, - sleptAvatarUrl: flaskChild.sleptAvatarUrl, - adultAvatarUrl: flaskChild.adult_avatar_url, - bioSummaryTranslations: flaskChild.bio_summary_translations, - bioTranslations: flaskChild.bio_translations, - voiceUrl: flaskChild.voiceUrl, - birthPlace: flaskChild.birthPlace, - housingStatus: flaskChild.housingStatus, - familyCount: flaskChild.familyCount, - sayFamilyCount: flaskChild.sayFamilyCount, - education: flaskChild.education, - created: flaskChild.created, - updated: flaskChild.updated, - isDeleted: flaskChild.isDeleted, - isConfirmed: flaskChild.isConfirmed, - flaskConfirmUser: flaskChild.confirmUser, - confirmDate: flaskChild.confirmDate, - existenceStatus: flaskChild.existence_status, - generatedCode: flaskChild.generatedCode, - isMigrated: flaskChild.isMigrated, - migratedId: flaskChild.migratedId, - birthDate: flaskChild.birthDate && new Date(flaskChild.birthDate), - migrateDate: flaskChild.migrateDate && new Date(flaskChild.migrateDate), - }; - const nestChild = await this.childrenService.getChildById(flaskChildId); - console.log( - '\x1b[36m%s\x1b[0m', - 'Updated the preRegister, Now updating Nest child ...\n', + return await this.syncService.syncChild( + flaskChild, + body.addedState, + body.schoolType, ); - - if (!nestChild) { - throw new ServerError('Hmmm... No child was found', 500); - } else if (nestChild && nestChild.updated !== flaskChild.updated) { - await this.childrenService.updateChild(childDetails, nestChild).then(); - console.log('\x1b[36m%s\x1b[0m', 'Child updated ...\n'); - } - return preRegister; } catch (e) { throw new ServerError(e.message, e.status); } diff --git a/src/features/sync/sync.service.ts b/src/features/sync/sync.service.ts index 346212591..896761c53 100644 --- a/src/features/sync/sync.service.ts +++ b/src/features/sync/sync.service.ts @@ -10,6 +10,7 @@ import { NeedTypeEnum, PanelContributors, PaymentStatusEnum, + SchoolTypeEnum, } from 'src/types/interfaces/interface'; import { NgoParams } from 'src/types/parameters/NgoParammeters'; import { PaymentParams } from 'src/types/parameters/PaymentParameters'; @@ -35,6 +36,8 @@ import { Need } from 'src/entities/flaskEntities/need.entity'; import { CreateReceiptDto } from 'src/types/dtos/CreateReceipt.dto'; import { CreatePaymentDto } from 'src/types/dtos/CreatePayment.dto'; import { CreateStatusDto } from 'src/types/dtos/CreateStatus.dto'; +import { Child } from 'src/entities/flaskEntities/child.entity'; +import { ChildrenEntity } from 'src/entities/children.entity'; @Injectable() export class SyncService { @@ -776,4 +779,136 @@ export class SyncService { child: nestChild, }; } + + async syncChild( + flaskChild: Child, + addedState: number, + schoolType: SchoolTypeEnum, + ) { + //--------------------------------------------Child------------------------------------- + let nestChild = await this.childrenService.getChildById(flaskChild.id); + + const childDetails = { + flaskId: flaskChild.id, + sayName: flaskChild.sayname_translations.en, + sayNameTranslations: flaskChild.sayname_translations, + nationality: flaskChild.nationality, + country: flaskChild.country, + city: flaskChild.city, + state: addedState, + awakeAvatarUrl: flaskChild.awakeAvatarUrl, + sleptAvatarUrl: flaskChild.sleptAvatarUrl, + adultAvatarUrl: flaskChild.adult_avatar_url, + bioSummaryTranslations: flaskChild.bio_summary_translations, + bioTranslations: flaskChild.bio_translations, + voiceUrl: flaskChild.voiceUrl, + birthPlace: flaskChild.birthPlace, + housingStatus: flaskChild.housingStatus, + familyCount: flaskChild.familyCount, + sayFamilyCount: flaskChild.sayFamilyCount, + education: flaskChild.education, + created: flaskChild.created, + updated: flaskChild.updated, + isDeleted: flaskChild.isDeleted, + isConfirmed: flaskChild.isConfirmed, + flaskConfirmUser: flaskChild.confirmUser, + confirmDate: flaskChild.confirmDate, + existenceStatus: flaskChild.existence_status, + generatedCode: flaskChild.generatedCode, + isMigrated: flaskChild.isMigrated, + migratedId: flaskChild.migratedId, + birthDate: flaskChild.birthDate && new Date(flaskChild.birthDate), + migrateDate: flaskChild.migrateDate && new Date(flaskChild.migrateDate), + schoolType, + flaskNgoId: flaskChild.id_ngo, + flaskSwId: flaskChild.id_social_worker, + }; + + if (!nestChild) { + //-------------------------------------------- Social worker------------------------------------- + let nestSocialWorker: AllUserEntity; + let swNgo: NgoEntity; + nestSocialWorker = await this.userService.getContributorByFlaskId( + flaskChild.id_social_worker, + PanelContributors.SOCIAL_WORKER, + ); + + const flaskSocialWorker = await this.userService.getFlaskSocialWorker( + flaskChild.id_social_worker, + ); + + const swDetails = { + typeId: flaskSocialWorker.type_id, + firstName: flaskSocialWorker.firstName, + lastName: flaskSocialWorker.lastName, + avatarUrl: flaskSocialWorker.avatar_url, + flaskUserId: flaskSocialWorker.id, + birthDate: + flaskSocialWorker.birth_date && + new Date(flaskSocialWorker.birth_date), + panelRole: PanelContributors.SOCIAL_WORKER, + userName: flaskSocialWorker.userName, + isActive: flaskSocialWorker.is_active, + }; + + if (!nestSocialWorker) { + swNgo = await this.syncContributorNgo(flaskSocialWorker); + console.log('\x1b[36m%s\x1b[0m', 'Creating a Social Worker ...\n'); + nestSocialWorker = await this.userService.createContributor( + swDetails, + swNgo, + ); + console.log('\x1b[36m%s\x1b[0m', 'Created a Social Worker ...\n'); + } else if (nestSocialWorker) { + swNgo = nestSocialWorker.contributions.find( + (c) => c.flaskUserId == nestSocialWorker.flaskUserId, + ).ngo; + await this.userService + .updateContributor(nestSocialWorker.id, swDetails) + .then(); + nestSocialWorker = await this.userService.getContributorByFlaskId( + flaskChild.id_social_worker, + PanelContributors.SOCIAL_WORKER, + ); + console.log('\x1b[36m%s\x1b[0m', 'Social Worker updated ...\n'); + } else { + swNgo = nestSocialWorker.contributions.find( + (c) => c.flaskUserId == nestSocialWorker.flaskUserId, + ).ngo; + console.log( + '\x1b[36m%s\x1b[0m', + 'Skipped Social Worker updating ...\n', + ); + } + // Create Child + console.log('\x1b[36m%s\x1b[0m', 'Creating a Child ...\n'); + + if (!nestSocialWorker || !nestSocialWorker.contributions) { + throw new ObjectNotFound( + 'Something went wrong while trying to create a child!', + ); + } + const childNgo = await this.ngoService.getNgoById( + nestSocialWorker.contributions.find( + (c) => c.flaskUserId == nestSocialWorker.flaskUserId, + ).flaskNgoId, + ); + nestChild = await this.childrenService.createChild( + childDetails, + childNgo, + nestSocialWorker.contributions.find( + (c) => c.flaskUserId == nestSocialWorker.flaskUserId, + ), + ); + console.log('\x1b[36m%s\x1b[0m', 'Created a Child ...\n'); + } else if (nestChild && nestChild.updated !== flaskChild.updated) { + await this.childrenService.updateChild(childDetails, nestChild).then(); + nestChild = await this.childrenService.getChildById(flaskChild.id); + + console.log('\x1b[36m%s\x1b[0m', 'Child updated ...\n'); + } else { + console.log('\x1b[36m%s\x1b[0m', 'Skipped Child updating ...\n'); + } + return nestChild; + } }