Skip to content

Commit

Permalink
children entity changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan-g committed Jul 20, 2024
1 parent 0c7af49 commit 5bf25c5
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 50 deletions.
14 changes: 14 additions & 0 deletions src/db/migrations/1721227837097-new.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class New1721227837097 implements MigrationInterface {
name = 'New1721227837097'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "children_entity" ADD "familyCount" integer`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "children_entity" DROP COLUMN "familyCount"`);
}

}
14 changes: 14 additions & 0 deletions src/db/migrations/1721228640217-new.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class New1721228640217 implements MigrationInterface {
name = 'New1721228640217'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "children_entity" ADD "state" integer`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "children_entity" DROP COLUMN "state"`);
}

}
14 changes: 14 additions & 0 deletions src/db/migrations/1721228883744-new.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class New1721228883744 implements MigrationInterface {
name = 'New1721228883744'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "children_entity" ADD "schoolType" integer`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "children_entity" DROP COLUMN "schoolType"`);
}

}
15 changes: 14 additions & 1 deletion src/entities/children.entity.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;

Expand All @@ -46,6 +53,9 @@ export class ChildrenEntity extends BaseEntity {
@Column({ nullable: true })
education: EducationEnum;

@Column({ nullable: true })
schoolType: SchoolTypeEnum;

@Column({ nullable: true })
existenceStatus: number;

Expand Down Expand Up @@ -79,6 +89,9 @@ export class ChildrenEntity extends BaseEntity {
@Column({ nullable: true })
sayFamilyCount: number;

@Column({ nullable: true })
familyCount: number;

@Column({ nullable: true })
sayName: string;

Expand Down
61 changes: 12 additions & 49 deletions src/features/children/children.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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')
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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,
Expand All @@ -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');
Expand All @@ -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);
}
Expand Down
135 changes: 135 additions & 0 deletions src/features/sync/sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 5bf25c5

Please sign in to comment.