fix: apply link ignore outdate
Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -23,7 +23,7 @@ import {
|
||||
BaseCrudModuleType,
|
||||
} from '~/transformers/crud-factor.transformer'
|
||||
|
||||
import { AduitReasonDto, LinkDto } from './link.dto'
|
||||
import { AuditReasonDto, LinkDto } from './link.dto'
|
||||
import { LinkModel, LinkState } from './link.model'
|
||||
import { LinkService } from './link.service'
|
||||
|
||||
@@ -122,7 +122,7 @@ export class LinkController {
|
||||
@HttpCode(201)
|
||||
async sendReasonByEmail(
|
||||
@Param() params: MongoIdDto,
|
||||
@Body() body: AduitReasonDto,
|
||||
@Body() body: AuditReasonDto,
|
||||
) {
|
||||
const { id } = params
|
||||
const { reason, state } = body
|
||||
|
||||
@@ -8,7 +8,7 @@ export class LinkDto extends LinkModel {
|
||||
author: string
|
||||
}
|
||||
|
||||
export class AduitReasonDto {
|
||||
export class AuditReasonDto {
|
||||
@IsString({ message: '请输入审核理由' })
|
||||
reason: string
|
||||
|
||||
|
||||
@@ -34,21 +34,48 @@ export class LinkService {
|
||||
return this.linkModel
|
||||
}
|
||||
async applyForLink(model: LinkModel) {
|
||||
try {
|
||||
const doc = await this.model.create({
|
||||
const existedDoc = await this.model
|
||||
.findOne({
|
||||
url: model.url,
|
||||
})
|
||||
.lean()
|
||||
|
||||
let nextModel: LinkModel
|
||||
if (existedDoc) {
|
||||
switch (existedDoc.state) {
|
||||
case LinkState.Pass:
|
||||
case LinkState.Audit:
|
||||
throw new BadRequestException('请不要重复申请友链哦')
|
||||
|
||||
case LinkState.Banned:
|
||||
throw new BadRequestException('您的友链已被禁用,请联系管理员')
|
||||
case LinkState.Reject:
|
||||
case LinkState.Outdate:
|
||||
nextModel = await this.model
|
||||
.findOneAndUpdate(
|
||||
{ _id: existedDoc._id },
|
||||
{
|
||||
$set: {
|
||||
state: LinkState.Audit,
|
||||
},
|
||||
},
|
||||
{ new: true },
|
||||
)
|
||||
.lean()
|
||||
}
|
||||
} else {
|
||||
nextModel = await this.model.create({
|
||||
...model,
|
||||
type: LinkType.Friend,
|
||||
state: LinkState.Audit,
|
||||
})
|
||||
|
||||
process.nextTick(() => {
|
||||
this.eventManager.broadcast(BusinessEvents.LINK_APPLY, doc, {
|
||||
scope: EventScope.TO_SYSTEM_ADMIN,
|
||||
})
|
||||
})
|
||||
} catch (err) {
|
||||
throw new BadRequestException('请不要重复申请友链哦')
|
||||
}
|
||||
|
||||
process.nextTick(() => {
|
||||
this.eventManager.broadcast(BusinessEvents.LINK_APPLY, nextModel, {
|
||||
scope: EventScope.TO_SYSTEM_ADMIN,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async approveLink(id: string) {
|
||||
|
||||
8
test/mock/modules/user.mock.ts
Normal file
8
test/mock/modules/user.mock.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { defineProvider } from 'test/helper/defineProvider'
|
||||
|
||||
import { UserService } from '~/modules/user/user.service'
|
||||
|
||||
export const userProvider = defineProvider({
|
||||
provide: UserService,
|
||||
useValue: {},
|
||||
})
|
||||
88
test/src/modules/link/link.controller.e2e-spec.ts
Normal file
88
test/src/modules/link/link.controller.e2e-spec.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { createE2EApp } from 'test/helper/create-e2e-app'
|
||||
import { gatewayProviders } from 'test/mock/modules/gateway.mock'
|
||||
import { userProvider } from 'test/mock/modules/user.mock'
|
||||
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter'
|
||||
import { ReturnModelType } from '@typegoose/typegoose'
|
||||
|
||||
import { OptionModel } from '~/modules/configs/configs.model'
|
||||
import { ConfigsService } from '~/modules/configs/configs.service'
|
||||
import {
|
||||
LinkController,
|
||||
LinkControllerCrud,
|
||||
} from '~/modules/link/link.controller'
|
||||
import { LinkModel, LinkState } from '~/modules/link/link.model'
|
||||
import { LinkService } from '~/modules/link/link.service'
|
||||
import { AssetService } from '~/processors/helper/helper.asset.service'
|
||||
import { EmailService } from '~/processors/helper/helper.email.service'
|
||||
import { EventManagerService } from '~/processors/helper/helper.event.service'
|
||||
import { HttpService } from '~/processors/helper/helper.http.service'
|
||||
import { SubPubBridgeService } from '~/processors/redis/subpub.service'
|
||||
|
||||
describe('Test LinkController(E2E)', () => {
|
||||
const proxy = createE2EApp({
|
||||
controllers: [LinkController, LinkControllerCrud],
|
||||
models: [LinkModel, OptionModel],
|
||||
providers: [
|
||||
...gatewayProviders,
|
||||
LinkService,
|
||||
ConfigsService,
|
||||
EmailService,
|
||||
HttpService,
|
||||
EventManagerService,
|
||||
userProvider,
|
||||
SubPubBridgeService,
|
||||
AssetService,
|
||||
EventEmitter2,
|
||||
],
|
||||
async pourData(modelMap) {
|
||||
const linkModel = modelMap.get(LinkModel)
|
||||
|
||||
;(linkModel.model as ReturnModelType<typeof LinkModel>).create({
|
||||
url: 'https://innei.ren',
|
||||
name: 'innei',
|
||||
avatar: 'https://innei.ren/avatar.png',
|
||||
description: 'innei',
|
||||
state: LinkState.Outdate,
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
it('should change state to audit', async () => {
|
||||
const app = proxy.app
|
||||
const res = await app.inject({
|
||||
method: 'post',
|
||||
url: '/links/audit',
|
||||
payload: {
|
||||
url: 'https://innei.ren',
|
||||
name: 'innnnn',
|
||||
author: 'innei',
|
||||
avatar: 'https://innei.ren/avatar.png',
|
||||
description: 'innei',
|
||||
},
|
||||
})
|
||||
expect(res.statusCode).toBe(204)
|
||||
})
|
||||
|
||||
it('apply link repeat should throw', async () => {
|
||||
const app = proxy.app
|
||||
const res = await app.inject({
|
||||
method: 'post',
|
||||
url: '/links/audit',
|
||||
payload: {
|
||||
url: 'https://innei.ren',
|
||||
name: 'innnnn',
|
||||
author: 'innei',
|
||||
avatar: 'https://innei.ren/avatar.png',
|
||||
description: 'innei',
|
||||
},
|
||||
})
|
||||
expect(res.json()).toMatchInlineSnapshot(`
|
||||
{
|
||||
"error": "Bad Request",
|
||||
"message": "请不要重复申请友链哦",
|
||||
"statusCode": 400,
|
||||
}
|
||||
`)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user