</> Code samples - xScorts

We take great pride in following best practices while coding so that the end result is consistent, descriptive, and easy to interpret.

Hence building on top of the script you purchase from us is easy. You can always customize things yourself or reach out to us and let us bring your vision to life.

Feel free to check out some example code snippets below.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 import { Controller, Injectable, HttpCode, HttpStatus, UsePipes, ValidationPipe, Get, Param, Query, HttpException, Put, Body, UseGuards, Request, Post } from '@nestjs/common'; import { omit } from 'lodash'; import { DataResponse, PageableData } from 'src/kernel'; import { Roles } from 'src/modules/auth'; import { AuthGuard, RoleGuard } from 'src/modules/auth/guards'; import { ContactPayload } from 'src/modules/contact/payloads'; import { MailerService } from 'src/modules/mailer'; import { PERFORMER_STATUSES, UNALLOWED_SELF_UPDATE_FIELDS } from '../constants'; import { PerformerDto, IPerformerResponse } from '../dtos'; import { PerformerSearchPayload, PerformerUpdatePayload } from '../payloads'; import { PerformerService, PerformerSearchService } from '../services'; @Injectable() @Controller('performers') export class PerformerController { constructor( private readonly performerService: PerformerService, private readonly performerSearchService: PerformerSearchService, private readonly mailService: MailerService ) {} @Get('/search') @HttpCode(HttpStatus.OK) @UsePipes(new ValidationPipe({ transform: true })) async usearch( @Query() req: PerformerSearchPayload ): Promise<DataResponse<PageableData<IPerformerResponse>>> { req.status = 'active'; // default sort is score // req.sort = 'popular'; const data = await this.performerSearchService.search(req); return DataResponse.ok(data); } @Get('/me') @HttpCode(HttpStatus.OK) @Roles('performer') @UseGuards(RoleGuard) @UsePipes(new ValidationPipe({ transform: true })) async getMyProfile(@Request() req: any): Promise<DataResponse<any>> { const { authUser, jwtToken } = req; const performer = await this.performerService.findByUserId( authUser.sourceId ); const details = await this.performerService.getDetails(performer._id, { responseDocument: true, jwtToken }); if (details.status === PERFORMER_STATUSES.INACTIVE) { throw new HttpException('This account is suspended', 403); } return DataResponse.ok(details.toResponse(true)); } @Get('/:username') @HttpCode(HttpStatus.OK) async getDetails( @Param('username') performerUsername: string ): Promise<DataResponse<Partial<PerformerDto>>> { const data = await this.performerService.findByUsername(performerUsername); if (data.status !== PERFORMER_STATUSES.ACTIVE) { throw new HttpException('This account is suspended', 403); } return DataResponse.ok(data.toPublicDetailsResponse()); } @Put('/') @HttpCode(HttpStatus.OK) @Roles('performer') @UseGuards(RoleGuard) @UsePipes(new ValidationPipe({ transform: true })) async updateMyProfile( @Body() payload: PerformerUpdatePayload, @Request() req: any ): Promise<DataResponse<Partial<PerformerDto>>> { const { sourceId } = req.authUser; const performer = await this.performerService.findByUserId(sourceId); const data = await this.performerService.update( performer._id, // remove unwanted fields omit(payload, UNALLOWED_SELF_UPDATE_FIELDS) ); return DataResponse.ok(data); } @Get('/:username/related') @HttpCode(HttpStatus.OK) async getRelated( @Param('username') username: string ): Promise<DataResponse<any>> { const data = await this.performerService.getRelated(username); return DataResponse.ok(data); } @Post('/:username/contact') @HttpCode(HttpStatus.OK) @UseGuards(AuthGuard) async contact( @Param('username') username: string, @Body() payload: ContactPayload ): Promise<DataResponse<any>> { const performer = await this.performerService.findByUsername(username); if (!performer || performer.status !== 'active') { return DataResponse.error(new Error("Model doesn't exists")); } await this.mailService.send({ subject: `New contact from ${payload.name}`, to: performer.email, data: payload, template: 'contact-performer' }); return DataResponse.ok(true); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 import { Injectable, forwardRef, Inject } from '@nestjs/common'; import { Model } from 'mongoose'; import { EntityNotFoundException, QueueEventService } from 'src/kernel'; import { ObjectId } from 'mongodb'; import { FileService } from 'src/modules/file/services'; import { FileDto } from 'src/modules/file'; import { UserDto } from 'src/modules/user/dtos'; import { REF_TYPE } from 'src/modules/file/constants'; import { isObjectId } from 'src/kernel/helpers/string.helper'; import { CategoryService } from 'src/modules/category/services'; import { EVENT } from 'src/kernel/constants'; import { UserService } from 'src/modules/user/services'; import { PerformerDto } from '../dtos'; import { UsernameExistedException } from '../exceptions'; import { PerformerModel } from '../models'; import { PerformerCreatePayload, PerformerUpdatePayload } from '../payloads'; import { PERFORMER_MODEL_PROVIDER } from '../providers'; import { PERFORMER_CHANNEL } from '../constants'; @Injectable() export class PerformerService { constructor( @Inject(forwardRef(() => CategoryService)) private readonly categoryService: CategoryService, @Inject(PERFORMER_MODEL_PROVIDER) private readonly Performer: Model<PerformerModel>, private readonly fileService: FileService, @Inject(forwardRef(() => UserService)) private readonly userService: UserService, private readonly queueEventService: QueueEventService ) {} public async findById(id: string | ObjectId): Promise<PerformerDto> { const model = await this.Performer.findById(id); if (!model) return null; return new PerformerDto(model); } public async findByEmail(email: string): Promise<PerformerDto> { if (!email) { return null; } const model = await this.Performer.findOne({ email: email.toLowerCase() }); if (!model) return null; return new PerformerDto(model); } public async create( payload: PerformerCreatePayload, user?: UserDto ): Promise<PerformerDto> { const data = { ...payload, updatedAt: new Date(), createdAt: new Date() } as any; const userNameCheck = await this.Performer.countDocuments({ username: payload.username.trim() }); if (userNameCheck) { throw new UsernameExistedException(); } const usernameCheck2 = await this.userService.findByUsername( payload.username.trim() ); if (usernameCheck2) { throw new UsernameExistedException(); } if (user) { data.createdBy = user._id; } data.username = data.username.trim(); data.email = data.email ? data.email.toLowerCase() : null; if (!data.name) data.name = data.username; const performer = await this.Performer.create(data); // update / create auth await this.queueEventService.publish({ channel: PERFORMER_CHANNEL, eventName: EVENT.CREATED, data: { performer: performer.toObject(), payload } }); return new PerformerDto(performer); } public async update( id: string | ObjectId, payload: Partial<PerformerUpdatePayload> ): Promise<any> { const performer = await this.Performer.findById(id); if (!performer) { throw new EntityNotFoundException(); } const data = { ...payload, updatedAt: new Date() } as any; if (!data.name) { data.name = [data.firstName || '', data.lastName || ''].join(' '); } if (data.username && data.username.trim() !== performer.username) { const usernameCheck = await this.Performer.countDocuments({ username: data.username.trim(), _id: { $ne: performer._id } }); if (usernameCheck) { throw new UsernameExistedException(); } const usernameCheck2 = await this.userService.findByUsername( payload.username.trim() ); if (usernameCheck2._id.toString() !== performer.userId?.toString()) { throw new UsernameExistedException(); } data.username = data.username.trim(); } data.categoryIds = data.categoryIds || []; if (!data.name) data.name = data.username; await this.Performer.updateOne({ _id: id }, data); // update / create auth await this.queueEventService.publish({ channel: PERFORMER_CHANNEL, eventName: EVENT.UPDATED, data: { performer: performer.toObject(), payload } }); return { updated: true }; } public async updateAvatar(performer: PerformerDto, file: FileDto) { await this.Performer.updateOne( { _id: performer._id }, { avatarId: file._id, avatarPath: file.path } ); await this.fileService.addRef(file._id, { itemId: performer._id, itemType: REF_TYPE.PERFORMER }); const p = await this.Performer.findById(performer._id); await this.queueEventService.publish({ channel: PERFORMER_CHANNEL, eventName: 'avatarUpdated', data: { performer: p.toObject(), file } }); return file; } public async updateWelcomeVideo(user: PerformerDto, file: FileDto) { await this.Performer.updateOne( { _id: user._id }, { welcomeVideoId: file._id, welcomeVideoPath: file.path } ); await this.fileService.addRef(file._id, { itemId: user._id, itemType: REF_TYPE.PERFORMER }); return file; } public async findByUserId(userId: string | ObjectId) { return this.Performer.findOne({ userId }); } public async calculateScore(performerId) { const performer = performerId instanceof this.Performer ? performerId : await this.Performer.findById(performerId); if (!performer) return 0; let score = 0; if (performer.vip) score += 1; if (performer.verified) score += 1; return score; } }