diff --git a/.gitignore b/.gitignore index 086260421..9e8aadd01 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,7 @@ lerna-debug.log* # images /uploads midjourney.json +src/features/midjourney/bad-images-to-remove.json # OS .DS_Store diff --git a/nest-cli.json b/nest-cli.json index d9e26c586..d92aaf6fa 100644 --- a/nest-cli.json +++ b/nest-cli.json @@ -5,7 +5,8 @@ "compilerOptions": { "assets": [ "features/campaign/templates/**/*", - "features/children/**/*" + "features/children/**/*", + "features/midjourney/**/*" ], "watchAssets": true } diff --git a/src/entities/user.entity.ts b/src/entities/user.entity.ts index 393b7cec4..8a6f4b9e0 100644 --- a/src/entities/user.entity.ts +++ b/src/entities/user.entity.ts @@ -68,8 +68,4 @@ export class AllUserEntity extends BaseEntity { @Column({ default: true }) monthlyCampaign: boolean; - @OneToMany(() => CampaignEntity, (e) => e.receivers, { - eager: false, - }) - receivedEmails: CampaignEntity[]; } diff --git a/src/features/family/family.controller.ts b/src/features/family/family.controller.ts index f9a5e2be0..c52a970b4 100644 --- a/src/features/family/family.controller.ts +++ b/src/features/family/family.controller.ts @@ -4,6 +4,7 @@ import { Get, Param, Patch, + Query, Req, } from '@nestjs/common'; import { FamilyService } from './family.service'; @@ -47,7 +48,26 @@ export class FamilyController { private childrenService: ChildrenService, private needService: NeedService, private paymentService: PaymentService, - ) { } + ) {} + + @Get('search') + async searchUsers( + @Req() req: Request, + @Query('q') query: string, + ): Promise { + const panelFlaskUserId = req.headers['panelFlaskUserId']; + const panelFlaskTypeId = req.headers['panelFlaskTypeId']; + if ( + !isAuthenticated(panelFlaskUserId, panelFlaskTypeId) || + panelFlaskTypeId !== FlaskUserTypesEnum.SUPER_ADMIN + ) { + throw new ForbiddenException('You Are not the Super admin'); + } + if (query.length > 3) { + const users = await this.familyService.searchUsers(query); + return { users }; + } + } @Get(`my/children/:familyUserId`) @ApiOperation({ description: 'Get my children' }) diff --git a/src/features/family/family.service.ts b/src/features/family/family.service.ts index fd936012f..80a1c688f 100644 --- a/src/features/family/family.service.ts +++ b/src/features/family/family.service.ts @@ -33,6 +33,18 @@ export class FamilyService { private flaskUserFamilyRepository: Repository, ) {} + async searchUsers(query: string): Promise { + return this.flaskUserRepository + .createQueryBuilder('user') + .where( + 'user.userName ILIKE :query OR user.emailAddress ILIKE :query OR user.phone_number ILIKE :query OR user.firstName ILIKE :query', + { + query: `%${query}%`, + }, + ) + .getMany(); + } + async getFamilyMembers(familyId: number): Promise { return await this.flaskFamilyRepository .createQueryBuilder('family') diff --git a/src/features/midjourney/midjourney.controller.ts b/src/features/midjourney/midjourney.controller.ts index b671024d9..2489c954f 100644 --- a/src/features/midjourney/midjourney.controller.ts +++ b/src/features/midjourney/midjourney.controller.ts @@ -25,6 +25,8 @@ import { } from 'src/types/interfaces/interface'; import { WalletExceptionFilter } from 'src/filters/wallet-exception.filter'; import { checkIfFileOrDirectoryExists } from 'src/utils/file'; +import fs from 'fs'; +import path from 'path'; @ApiTags('Midjourney') @Controller('midjourney') @@ -34,7 +36,7 @@ export class MidjourneyController { private readonly downloadService: DownloadService, private readonly needService: NeedService, private readonly familyService: FamilyService, - ) { } + ) {} @Get(`db/all`) @ApiSecurity('flask-access-token') @@ -231,8 +233,49 @@ export class MidjourneyController { }); } - @Delete(`images/:flaskNeedId`) + @Delete(`bad/images`) @ApiOperation({ description: 'Delete folder of need images' }) + async deleteBadImages(@Req() req: Request) { + const panelFlaskUserId = req.headers['panelFlaskUserId']; + const panelFlaskTypeId = req.headers['panelFlaskTypeId']; + if ( + !isAuthenticated(panelFlaskUserId, panelFlaskTypeId) || + panelFlaskTypeId !== FlaskUserTypesEnum.SUPER_ADMIN || + panelFlaskUserId !== SUPER_ADMIN_ID + ) { + throw new WalletExceptionFilter(403, 'You Are not the Super admin'); + } + let ids; + const filePath = path.join(__dirname, 'bad-images-to-remove.txt'); + fs.readFile(filePath, 'utf8', (err, data) => { + if (err) { + console.error('Error reading the file:', err); + return; + } + // Step 2: Parse the data (assuming it's a newline-separated list) + ids = data.trim().split('\n'); + }); + + const list = []; + for await (const id of ids) { + const need = await this.needService.getNeedByFlaskId(id); + if (need) { + await this.needService.updateNeedMidjourney(need.id, ''); + } + + const path = `../midjourney-bot/main/need-images/need-${id}`; + if (checkIfFileOrDirectoryExists(path)) { + await rimraf(path); + list.push(path); + } else { + console.log(`Folder does not exist. Skipping...`); + } + } + return list; + } + + @Delete(`bad/images/:flaskNeedId`) + @ApiOperation({ description: 'Add need Id to list of delete candidate' }) async deleteSignature( @Req() req: Request, @Param('flaskNeedId') flaskNeedId: number, @@ -246,23 +289,34 @@ export class MidjourneyController { ) { throw new WalletExceptionFilter(403, 'You Are not the Super admin'); } + const filePath = 'src/features/midjourney/bad-images-to-remove.json'; - const path = `../midjourney-bot/main/need-images/need-${flaskNeedId}`; - if (checkIfFileOrDirectoryExists(path)) { - const result = await rimraf(path); - const need = await this.needService.getNeedByFlaskId(flaskNeedId); - await this.needService.updateNeedMidjourney(need.id, ''); + fs.readFile(filePath, 'utf8', (err, data) => { + if (err) { + console.error('Error reading the file:', err); + return; + } + try { + // Parse the JSON data + const jsonData = JSON.parse(data); - return { - result, - flaskNeedId, - message: `${flaskNeedId} is deleted`, - }; - } else { - return { - flaskNeedId, - message: `Folder does not exist`, - }; - } + // Add a new element to the list + jsonData.push(Number(flaskNeedId)); + + // Convert the updated data back to JSON format + const updatedJsonData = JSON.stringify(jsonData, null, 2); + + // Write the updated JSON data back to the file + fs.writeFile(filePath, updatedJsonData, 'utf8', (err) => { + if (err) { + console.error('Error writing file:', err); + } else { + console.log('File updated successfully!'); + } + }); + } catch (parseError) { + console.error('Error parsing JSON:', parseError); + } + }); } } diff --git a/src/features/midjourney/midjourney.module.ts b/src/features/midjourney/midjourney.module.ts index 8ca94815b..056edec2a 100644 --- a/src/features/midjourney/midjourney.module.ts +++ b/src/features/midjourney/midjourney.module.ts @@ -67,6 +67,7 @@ export class MidjourneyModule implements NestModule { consumer .apply(MidjourneyMiddleware) .exclude('midjourney/images/:flaskNeedId/:index') + .exclude('midjourney/bad/images/') .forRoutes(MidjourneyController); } } diff --git a/src/features/midjourney/midjourney.service.ts b/src/features/midjourney/midjourney.service.ts index 6f3ff026f..4de98c7a9 100644 --- a/src/features/midjourney/midjourney.service.ts +++ b/src/features/midjourney/midjourney.service.ts @@ -66,9 +66,9 @@ export class MidjourneyService { flaskId: n.id, needRetailerImg: n.img, prompt: - 'write word "SAY" over an unbearably cute, 3d isometric ' + + 'write word "SAY" over a cute, 3d isometric ' + n.name_translations.en + - ',cartoon soft pastel colors illustration, clay render, blender 3d, physically based rendering, soft and light background, pastel background, colorful, toy like proportions --fast', + ',cartoon soft pastel colors illustration, clay render, blender 3d, physically based rendering, soft and light background, pastel background, colorful, toy like proportions', }; list.push(data); listOfIds.push(n.id); @@ -76,7 +76,7 @@ export class MidjourneyService { console.log(listOfIds); } }); - config().dataCache.storeMidjourny(list); + config().dataCache.storeMidjourney(list); if (checkIfFileOrDirectoryExists('../midjourney-bot/midjourney.json')) { deleteFile('../midjourney-bot/midjourney.json'); } diff --git a/src/features/user/user.controller.ts b/src/features/user/user.controller.ts index aa3aa8867..e0e514ecd 100644 --- a/src/features/user/user.controller.ts +++ b/src/features/user/user.controller.ts @@ -52,7 +52,7 @@ export class UserController { private walletService: WalletService, private ipfsService: IpfsService, private ngoService: NgoService, - ) { } + ) {} @Get(`all`) @ApiOperation({ description: 'Get all users' }) @@ -259,7 +259,9 @@ export class UserController { // to avoid seeing an SW created need by the NGO supervisor in the fourth column / Could not find ypu role / when signing socialWorkerId = - role === SAYPlatformRoles.NGO_SUPERVISOR ? panelFlaskUserId : socialWorkerId; + role === SAYPlatformRoles.NGO_SUPERVISOR + ? panelFlaskUserId + : socialWorkerId; delivered = await this.needService.getDeliveredNeeds( { @@ -370,10 +372,10 @@ export class UserController { paidCount === max ? paidCount : notPaidCount === max - ? notPaidCount - : purchasedCount === max - ? purchasedCount - : deliveredCount, + ? notPaidCount + : purchasedCount === max + ? purchasedCount + : deliveredCount, meta: { paid: paidCount, notPaid: notPaidCount, @@ -416,5 +418,4 @@ export class UserController { theUser.contributions.forEach((c) => list.push(c.id)); return await this.userService.deleteOneContributor(theUser.id, list); } - } diff --git a/src/features/user/user.service.ts b/src/features/user/user.service.ts index ae51a9e47..18d3d27c2 100644 --- a/src/features/user/user.service.ts +++ b/src/features/user/user.service.ts @@ -24,7 +24,8 @@ export class UserService { private flaskSocialWorkerRepository: Repository, @InjectRepository(User, 'flaskPostgres') private flaskUserRepository: Repository, - ) { } + ) {} + getFlaskSwIds(): Promise { return this.flaskSocialWorkerRepository.find({ diff --git a/src/utils/dataCache.ts b/src/utils/dataCache.ts index acc746114..440b849ac 100644 --- a/src/utils/dataCache.ts +++ b/src/utils/dataCache.ts @@ -77,7 +77,7 @@ export default class DataCache { }; }; - storeMidjourny = (list: any[]) => { + storeMidjourney = (list: any[]) => { list.forEach((e) => this.midjourneyList.push(e)); }; diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index b9dbcf408..4aa18a07e 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -243,7 +243,6 @@ export function persianDay(value: Date) { ); } - export function persianMonth(value: Date) { if (!value) { return null; @@ -923,7 +922,7 @@ export function fetchCampaginCode( type: CampaignTypeEnum, ) { const today = new Date(); - const englishMonth = today.getMonth(); + const englishMonth = today.getMonth(); // If it is January, getMonth() will return 0 const englishYear = today.getFullYear(); return `${type}:${name}-${englishMonth}/${englishYear}`; }