Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"package:check": "npx npm-check-updates -i",
"generate:keys": "npx ts-node scripts/generate-keys.ts",
"migrate:fresh": "yarn migrate:remove && yarn migrate:seed",
"migrate:seed": "nestjs-command seed:country && nestjs-command seed:apikey && nestjs-command seed:role && nestjs-command seed:user && nestjs-command seed:settings && nestjs-command seed:termPolicy",
"migrate:remove": "nestjs-command remove:user && nestjs-command remove:country && nestjs-command remove:apikey && nestjs-command remove:role && nestjs-command remove:settings && nestjs-command remove:termPolicy",
"migrate:seed": "nestjs-command seed:country && nestjs-command seed:termPolicy && nestjs-command seed:apikey && nestjs-command seed:role && nestjs-command seed:user && nestjs-command seed:settings",
"migrate:remove": "nestjs-command remove:user && nestjs-command remove:country && nestjs-command remove:termPolicy && nestjs-command remove:apikey && nestjs-command remove:role && nestjs-command remove:settings",
"migrate:template": "nestjs-command migrate:emailTemplate && nestjs-command migrate:termPolicyTemplate",
"rollback:template": "nestjs-command rollback:emailTemplate && nestjs-command rollback:termPolicyTemplate"
},
Expand Down
2 changes: 2 additions & 0 deletions src/languages/en/termPolicy.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"get": "Get term and policy success",
"accept": "Term or policy accepted",
"accepted": "List of accepted terms and policies.",
"reject": "Term or policy rejected",
"pending": "List of pending terms and policies.",
"delete": "Term and policy deleted success.",
"update": "Update term and policy success.",
Expand All @@ -13,6 +14,7 @@
"notAccepted": "You need to accept {property} policy before you can use this feature.",
"inactive": "The term and policy is not active, most likely there's a newer version available",
"alreadyAccepted": "Term and policy has already been accepted",
"alreadyRejected": "Term and policy has already been rejected",
"newerVersionExist": "Newer version of the term and policy exists.",
"updateForbiddenStatusPublished": "You can't update a published term and policy, you need to create a new version.",
"requireAgreement": "Agreement for ${property} is required"
Expand Down
5 changes: 4 additions & 1 deletion src/migration/seeds/migration.term-policy.seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import { Types } from 'mongoose';
import { CountryDoc } from '@modules/country/repository/entities/country.entity';
import { CountryService } from '@modules/country/services/country.service';
import { ENUM_AWS_S3_ACCESSIBILITY } from '@modules/aws/enums/aws.enum';
import { TermPolicyAcceptanceService } from '@modules/term-policy/services/term-policy.acceptance.service';

@Injectable()
export class MigrationTermPolicySeed {
constructor(
private readonly countryService: CountryService,
private readonly termPolicyService: TermPolicyService
private readonly termPolicyService: TermPolicyService,
private readonly termPolicyAcceptanceService: TermPolicyAcceptanceService
) {}

@Command({
Expand Down Expand Up @@ -100,6 +102,7 @@ export class MigrationTermPolicySeed {
async remove(): Promise<void> {
try {
await this.termPolicyService.deleteMany();
await this.termPolicyAcceptanceService.deleteMany()
} catch (err: any) {
throw new Error(err);
}
Expand Down
17 changes: 16 additions & 1 deletion src/migration/seeds/migration.user.seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { MessageService } from '@common/message/services/message.service';
import { ENUM_PASSWORD_HISTORY_TYPE } from '@modules/password-history/enums/password-history.enum';
import { SessionService } from '@modules/session/services/session.service';
import { VerificationService } from '@modules/verification/services/verification.service';
import { TermPolicyAcceptanceService } from '@modules/term-policy/services/term-policy.acceptance.service';
import { ENUM_TERM_POLICY_TYPE } from '@modules/term-policy/enums/term-policy.enum';
import { ENUM_MESSAGE_LANGUAGE } from '@common/message/enums/message.enum';

@Injectable()
export class MigrationUserSeed {
Expand All @@ -28,7 +31,8 @@ export class MigrationUserSeed {
private readonly activityService: ActivityService,
private readonly messageService: MessageService,
private readonly sessionService: SessionService,
private readonly verificationService: VerificationService
private readonly verificationService: VerificationService,
private readonly termPolicyAcceptanceService: TermPolicyAcceptanceService
) {}

@Command({
Expand Down Expand Up @@ -129,6 +133,17 @@ export class MigrationUserSeed {
type: ENUM_PASSWORD_HISTORY_TYPE.SIGN_UP,
}),
this.verificationService.verify(verification),
this.termPolicyAcceptanceService.createAcceptances(
user._id,
[
ENUM_TERM_POLICY_TYPE.TERM,
ENUM_TERM_POLICY_TYPE.PRIVACY,
ENUM_TERM_POLICY_TYPE.COOKIES,
ENUM_TERM_POLICY_TYPE.MARKETING,
],
ENUM_MESSAGE_LANGUAGE.EN,
country._id
),
];

await Promise.all(promises);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class TermPolicyPublicController {
private readonly paginationService: PaginationService
) {}

@ResponsePaging('termPolicy.accepted')
@ResponsePaging('termPolicy.list')
@TermPolicyPublicListDoc()
@ApiKeyProtected()
@Get('/list')
Expand Down
73 changes: 71 additions & 2 deletions src/modules/term-policy/controllers/term-policy.user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ApiTags } from '@nestjs/swagger';
import {
BadRequestException,
Body,
Controller,
Controller, Delete,
Get,
InternalServerErrorException,
NotFoundException,
Expand Down Expand Up @@ -30,13 +30,14 @@ import { ENUM_POLICY_ROLE_TYPE } from '@modules/policy/enums/policy.enum';
import { TermPolicyAcceptanceResponseDto } from '@modules/term-policy/dtos/response/term-policy-acceptance.response.dto';
import {
TermPolicyUserAcceptDoc,
TermPolicyUserAcceptedDoc,
TermPolicyUserAcceptedDoc, TermPolicyUserRejectDoc,
} from '@modules/term-policy/docs/term-policy.user.doc';
import { UserService } from '@modules/user/services/user.service';
import { TERM_POLICY_ACCEPTANCE_DEFAULT_AVAILABLE_ORDER_BY } from '@modules/term-policy/constants/term-policy.list.constant';
import { DatabaseService } from '@common/database/services/database.service';
import { ENUM_APP_STATUS_CODE_ERROR } from '@app/enums/app.status-code.enum';
import { TermPolicyAcceptanceService } from '@modules/term-policy/services/term-policy.acceptance.service';
import { TermPolicyRejectRequestDto } from '@modules/term-policy/dtos/request/term-policy.reject.request.dto';

@ApiTags('modules.user.term-policy')
@Controller({
Expand Down Expand Up @@ -141,4 +142,72 @@ export class TermPolicyUserController {

return;
}

@TermPolicyUserRejectDoc()
@Response('termPolicy.reject')
@PolicyRoleProtected(ENUM_POLICY_ROLE_TYPE.USER)
@UserProtected()
@AuthJwtAccessProtected()
@ApiKeyProtected()
@Delete('/reject')
async reject(
@AuthJwtPayload('user') userId: string,
@Body() { type, country }: TermPolicyRejectRequestDto
): Promise<void> {
const user = await this.userService.findOneById(userId);
if (!user.termPolicy[type.toLowerCase()]) {
throw new BadRequestException({
statusCode: ENUM_TERM_POLICY_STATUS_CODE_ERROR.ALREADY_REJECTED,
message: 'termPolicy.error.alreadyRejected',
});
}

const policy = await this.termPolicyService.findOnePublished(
type,
country
);
if (!policy) {
throw new NotFoundException({
statusCode: ENUM_TERM_POLICY_STATUS_CODE_ERROR.NOT_FOUND,
message: 'termPolicy.error.notFound',
});
}

const session = await this.databaseService.createTransaction();

try {
const acceptance = await this.termPolicyAcceptanceService.findOne(
{
user: userId,
termPolicy: policy._id,
},
{ session }
);

const deleted = await this.termPolicyAcceptanceService.softDelete(
acceptance,
{ session }
);
if (!deleted) {
throw new NotFoundException({
statusCode: ENUM_TERM_POLICY_STATUS_CODE_ERROR.NOT_FOUND,
message: 'termPolicy.error.notFound',
});
}

await this.userService.rejectTermPolicy(user, type, { session });

await this.databaseService.commitTransaction(session);
} catch (err: unknown) {
await this.databaseService.abortTransaction(session);

throw new InternalServerErrorException({
statusCode: ENUM_APP_STATUS_CODE_ERROR.UNKNOWN,
message: 'http.serverError.internalServerError',
_error: err,
});
}

return;
}
}
21 changes: 21 additions & 0 deletions src/modules/term-policy/docs/term-policy.user.doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ENUM_DOC_REQUEST_BODY_TYPE } from '@common/doc/enums/doc.enum';
import { TermPolicyAcceptRequestDto } from '@modules/term-policy/dtos/request/term-policy.accept.request.dto';
import { TermPolicyAcceptanceResponseDto } from '@modules/term-policy/dtos/response/term-policy-acceptance.response.dto';
import { applyDecorators } from '@nestjs/common';
import { TermPolicyRejectRequestDto } from '@modules/term-policy/dtos/request/term-policy.reject.request.dto';

export function TermPolicyUserAcceptedDoc(): MethodDecorator {
return applyDecorators(
Expand Down Expand Up @@ -51,3 +52,23 @@ export function TermPolicyUserAcceptDoc(): MethodDecorator {
DocResponse('termPolicy.accept')
);
}

export function TermPolicyUserRejectDoc(): MethodDecorator {
return applyDecorators(
Doc({
summary: 'User rejects term or policy',
}),
DocAuth({
jwtAccessToken: true,
xApiKey: true,
}),
DocGuard({
role: true,
}),
DocRequest({
bodyType: ENUM_DOC_REQUEST_BODY_TYPE.JSON,
dto: TermPolicyRejectRequestDto,
}),
DocResponse('termPolicy.reject')
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { PickType, ApiProperty } from '@nestjs/swagger';
import { TermPolicyCreateRequestDto } from '@modules/term-policy/dtos/request/term-policy.create.request.dto';
import { ENUM_TERM_POLICY_TYPE } from '@modules/term-policy/enums/term-policy.enum';
import { IsEnum, IsNotEmpty } from 'class-validator';

export class TermPolicyRejectRequestDto extends PickType(
TermPolicyCreateRequestDto,
['country' ]
) {
@ApiProperty({
description: 'Type of the terms policy',
example: ENUM_TERM_POLICY_TYPE.COOKIES,
enum: ENUM_TERM_POLICY_TYPE,
required: true,
})
@IsEnum([ENUM_TERM_POLICY_TYPE.COOKIES, ENUM_TERM_POLICY_TYPE.MARKETING])
@IsNotEmpty()
readonly type: ENUM_TERM_POLICY_TYPE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export enum ENUM_TERM_POLICY_STATUS_CODE_ERROR {
EXIST = 6101,
INVALID_STATUS = 6102,
ALREADY_ACCEPTED = 6103,
ALREADY_REJECTED = 6107,
REQUIRED_ACCEPTANCE = 6104,
PREDEFINED_REQUIRED_ACCEPTANCE_NOT_FOUND = 6105,
AT_LEAST_ONE_DOCUMENT_REQUIRED = 6106,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
IDatabaseFindAllOptions,
IDatabaseCreateOptions,
IDatabaseCreateOptions, IDatabaseDeleteOptions,
IDatabaseFindAllOptions, IDatabaseFindOneOptions,
IDatabaseGetTotalOptions,
IDatabaseSoftDeleteOptions,
} from '@common/database/interfaces/database.interface';
import { ENUM_TERM_POLICY_TYPE } from '@modules/term-policy/enums/term-policy.enum';
import { ENUM_MESSAGE_LANGUAGE } from '@common/message/enums/message.enum';
Expand All @@ -13,40 +14,62 @@ import {
} from '@modules/term-policy/interfaces/term-policy.acceptance.interface';

export interface ITermPolicyAcceptanceService {
findOne(
find: Record<string, any>,
options?: IDatabaseFindOneOptions
): Promise<TermPolicyAcceptanceDoc>;

findAllByUser(
user: string,
options?: IDatabaseFindAllOptions
): Promise<ITermPolicyAcceptanceDoc[]>;

getTotalUser(
user: string,
options?: IDatabaseGetTotalOptions
): Promise<number>;

create(
user: string,
termPolicy: string,
options?: IDatabaseCreateOptions
): Promise<TermPolicyAcceptanceDoc>;

findAll(
find?: Record<string, any>,
options?: IDatabaseFindAllOptions
): Promise<ITermPolicyAcceptanceDoc[]>;

getTotal(
find?: Record<string, any>,
options?: IDatabaseGetTotalOptions
): Promise<number>;

mapList(
policies: (ITermPolicyAcceptanceDoc | ITermPolicyAcceptanceEntity)[]
): TermPolicyAcceptanceResponseDto[];

createMany(
user: string,
termPolicies: Array<string>,
options?: IDatabaseCreateOptions
): Promise<boolean>;

createAcceptances(
user: string,
termPolicyTypes: ENUM_TERM_POLICY_TYPE[],
language: ENUM_MESSAGE_LANGUAGE,
country: string,
options?: IDatabaseCreateOptions
): Promise<void>;

softDelete(
repository: TermPolicyAcceptanceDoc,
options?: IDatabaseSoftDeleteOptions
): Promise<TermPolicyAcceptanceDoc>;

deleteMany(
find?: Record<string, any>,
options?: IDatabaseDeleteOptions
): Promise<void>;
}
Loading