Add ability to delete a Site
This commit is contained in:
parent
e18e00a6e5
commit
234db553b1
@ -94,6 +94,23 @@ export interface IAPIDef {
|
||||
}
|
||||
response: ISite | undefined
|
||||
}
|
||||
put: {
|
||||
params: {
|
||||
teamId: number
|
||||
id: number
|
||||
}
|
||||
body: {
|
||||
name?: string
|
||||
domain?: string
|
||||
}
|
||||
response: ISite
|
||||
}
|
||||
delete: {
|
||||
params: {
|
||||
teamId: number
|
||||
id: number
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
'/teams/:teamId/sites': {
|
||||
|
||||
@ -1,9 +1,14 @@
|
||||
import {ISite} from '@rondo/common'
|
||||
import {ISiteCreateParams} from './ISiteCreateParams'
|
||||
import {ISiteUpdateParams} from './ISiteUpdateParams'
|
||||
|
||||
export interface ISiteService {
|
||||
create(params: ISiteCreateParams): Promise<ISite>
|
||||
|
||||
update(params: ISiteUpdateParams): Promise<ISite>
|
||||
|
||||
remove(params: {id: number, teamId: number}): Promise<void>
|
||||
|
||||
findOne(id: number, teamId: number): Promise<ISite | undefined>
|
||||
|
||||
findByUser(userId: number): Promise<ISite[]>
|
||||
|
||||
6
packages/server/src/site/ISiteUpdateParams.ts
Normal file
6
packages/server/src/site/ISiteUpdateParams.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export interface ISiteUpdateParams {
|
||||
id: number
|
||||
name?: string
|
||||
domain?: string
|
||||
teamId: number
|
||||
}
|
||||
@ -81,4 +81,35 @@ describe('team', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('PUT /teams/:teamId/sites/:id', () => {
|
||||
it('updates site belonging to a team', async () => {
|
||||
const site = await createSite(t, 'test.example.com')
|
||||
const response = await t.put('/teams/:teamId/sites/:id', {
|
||||
params: {
|
||||
id: site.id,
|
||||
teamId: site.teamId,
|
||||
},
|
||||
})
|
||||
.send({
|
||||
name: site.name,
|
||||
domain: 'test2.example.com',
|
||||
})
|
||||
.expect(200)
|
||||
expect(response.body.domain).toEqual('test2.example.com')
|
||||
})
|
||||
})
|
||||
|
||||
describe('DELETE /teams/:teamId/sites/:id', () => {
|
||||
it('deletes a site', async () => {
|
||||
const site = await createSite(t, 'test.example.com')
|
||||
await t.delete('/teams/:teamId/sites/:id', {
|
||||
params: {
|
||||
id: site.id,
|
||||
teamId: site.teamId,
|
||||
},
|
||||
})
|
||||
.expect(200)
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
@ -53,6 +53,39 @@ export class SiteRoutes extends BaseRoute<IAPIDef> {
|
||||
})
|
||||
})
|
||||
|
||||
t.put('/teams/:teamId/sites/:id', async req => {
|
||||
const {name, domain} = req.body
|
||||
const id = Number(req.params.id)
|
||||
const teamId = Number(req.params.teamId)
|
||||
|
||||
await this.permissions.belongsToTeam({
|
||||
teamId,
|
||||
userId: req.user!.id,
|
||||
})
|
||||
|
||||
return this.siteService.update({
|
||||
id,
|
||||
teamId,
|
||||
name,
|
||||
domain,
|
||||
})
|
||||
})
|
||||
|
||||
t.delete('/teams/:teamId/sites/:id', async req => {
|
||||
const id = Number(req.params.id)
|
||||
const teamId = Number(req.params.teamId)
|
||||
|
||||
await this.permissions.belongsToTeam({
|
||||
teamId,
|
||||
userId: req.user!.id,
|
||||
})
|
||||
|
||||
return this.siteService.remove({
|
||||
id,
|
||||
teamId,
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import {BaseService} from '../services/BaseService'
|
||||
import {ISiteCreateParams} from './ISiteCreateParams'
|
||||
import {ISiteUpdateParams} from './ISiteUpdateParams'
|
||||
import {ISiteService} from './ISiteService'
|
||||
import {Site} from '../entities/Site'
|
||||
|
||||
@ -11,6 +12,29 @@ export class SiteService extends BaseService implements ISiteService {
|
||||
return this.getRepository(Site).save(params)
|
||||
}
|
||||
|
||||
async update({teamId, id, name, domain}: ISiteUpdateParams) {
|
||||
// TODO validate params.domain
|
||||
|
||||
await this.getRepository(Site)
|
||||
.update({
|
||||
id,
|
||||
teamId,
|
||||
}, {
|
||||
name,
|
||||
domain,
|
||||
})
|
||||
|
||||
return (await this.findOne(id, teamId))!
|
||||
}
|
||||
|
||||
async remove({id, teamId}: {id: number, teamId: number}) {
|
||||
await this.getRepository(Site)
|
||||
.delete({
|
||||
id,
|
||||
teamId,
|
||||
})
|
||||
}
|
||||
|
||||
async findOne(id: number, teamId: number) {
|
||||
return this.getRepository(Site).findOne({
|
||||
where: {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user