Skip to content
Merged
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
1 change: 1 addition & 0 deletions backend/src/api/report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default (app) => {
app.post(`/tenant/:tenantId/report`, safeWrap(require('./reportCreate').default))
app.post(`/tenant/:tenantId/report/query`, safeWrap(require('./reportQuery').default))
app.put(`/tenant/:tenantId/report/:id`, safeWrap(require('./reportUpdate').default))
app.post(`/tenant/:tenantId/report/:id/duplicate`, safeWrap(require('./reportDuplicate').default))
app.post(`/tenant/:tenantId/report/import`, safeWrap(require('./reportImport').default))
app.delete(`/tenant/:tenantId/report`, safeWrap(require('./reportDestroy').default))
app.get(
Expand Down
18 changes: 18 additions & 0 deletions backend/src/api/report/reportDuplicate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Permissions from '../../security/permissions'
import track from '../../segment/track'
import ReportService from '../../services/reportService'
import PermissionChecker from '../../services/user/permissionChecker'

export default async (req, res) => {
new PermissionChecker(req).validateHas(Permissions.values.reportCreate)

const payload = await new ReportService(req).duplicate(req.params.id)

track(
'Report Duplicated',
{ id: payload.id, name: payload.name, public: payload.public },
{ ...req },
)

await req.responseHandler.success(req, res, payload)
}
1 change: 0 additions & 1 deletion backend/src/database/repositories/reportRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ class ReportRepository {
const include = []

const currentTenant = SequelizeRepository.getCurrentTenant(options)

const record = await options.database.report.findOne({
where: {
id,
Expand Down
53 changes: 53 additions & 0 deletions backend/src/services/reportService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,59 @@ export default class ReportService {
}
}

async duplicate(id) {
const transaction = await SequelizeRepository.createTransaction(this.options)

try {
const report = await ReportRepository.findById(id, {
...this.options,
transaction,
})

let duplicatedReport = await ReportRepository.create(
{
name: `${report.name} (copy)`,
public: false,
widgets: [],
},
{
...this.options,
transaction,
},
)

for (const widget of report.widgets) {
await WidgetRepository.create(
{
settings: widget.settings,
title: widget.title,
type: widget.type,
report: duplicatedReport.id,
},
{
...this.options,
transaction,
},
)
}

duplicatedReport = await ReportRepository.findById(duplicatedReport.id, {
...this.options,
transaction,
})

await SequelizeRepository.commitTransaction(transaction)

return duplicatedReport
} catch (error) {
await SequelizeRepository.rollbackTransaction(transaction)

SequelizeRepository.handleUniqueFieldError(error, this.options.language, 'report')

throw error
}
}

async update(id, data) {
const transaction = await SequelizeRepository.createTransaction(this.options)

Expand Down
34 changes: 33 additions & 1 deletion frontend/src/modules/report/components/report-dropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@
><i class="ri-link mr-1"></i>Copy Public
Url</el-dropdown-item
>
<el-dropdown-item
v-if="showDuplicateReport"
:command="{
action: 'reportDuplicate',
report: report
}"
><i class="ri-file-copy-line mr-1"></i>Duplicate
Report</el-dropdown-item
>
<el-divider
v-if="showEditReport || showViewReportPublic"
class="border-gray-200 !my-2"
Expand All @@ -75,6 +84,7 @@ import Message from '@/shared/message/message'
import AuthCurrentTenant from '@/modules/auth/auth-current-tenant'
import { ReportPermissions } from '@/modules/report/report-permissions'
import ConfirmDialog from '@/shared/dialog/confirm-dialog.js'
import { ReportService } from '@/modules/report/report-service'

export default {
name: 'AppReportDropdown',
Expand All @@ -94,6 +104,10 @@ export default {
showViewReportPublic: {
type: Boolean,
default: true
},
showDuplicateReport: {
type: Boolean,
default: true
}
},
computed: {
Expand All @@ -112,7 +126,8 @@ export default {
},
methods: {
...mapActions({
doDestroy: 'report/doDestroy'
doDestroy: 'report/doDestroy',
doCreate: 'report/doCreate'
}),
async doDestroyWithConfirm(id) {
try {
Expand All @@ -131,11 +146,28 @@ export default {
// no
}
},
async doDuplicate(id) {
ReportService.duplicate(id)
.then((duplicate) => {
this.$router.push({
name: 'reportEdit',
params: {
id: duplicate.id
}
})
Message.success('Report duplicated successfuly')
})
.catch(() => {
Message.error('Error duplicating report')
})
},
async handleCommand(command) {
if (command.action === 'reportDelete') {
return await this.doDestroyWithConfirm(
command.report.id
)
} else if (command.action === 'reportDuplicate') {
return await this.doDuplicate(command.report.id)
} else if (command.action === 'reportPublicUrl') {
return await this.copyToClipboard(command.report.id)
} else {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/modules/report/pages/report-form-page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
:show-view-report="false"
:show-view-report-public="false"
:show-edit-report="false"
:show-duplicate-report="false"
></app-report-dropdown>
</div>
</div>
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/modules/report/report-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ export class ReportService {
return response.data
}

static async duplicate(id) {
const tenantId = AuthCurrentTenant.get()

const response = await authAxios.post(
`/tenant/${tenantId}/report/${id}/duplicate`
)

return response.data
}

static async destroyAll(ids) {
const params = {
ids
Expand Down