Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#237] Fixed long file names bug for exporting #240

Merged
merged 1 commit into from Apr 21, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 18 additions & 8 deletions app/Services/ExpenseExportService.js
Expand Up @@ -2,6 +2,8 @@ const Drive = use('Drive')
const yaml = require('js-yaml')
const PDFDocument = require('pdfkit')
const createCsvWriter = require('csv-writer').createObjectCsvStringifier
const Hash = use('Hash')

class ExpenseExportService {
async export(tickets, type) {
switch (type) {
Expand All @@ -18,8 +20,10 @@ class ExpenseExportService {

async exportCSV(tickets) {
tickets = tickets.toJSON()
var filename_ids = tickets.map((ticket) => ticket.id)
const filepath = 'ticket_' + JSON.stringify(filename_ids) + '.csv'

const filename_ids = tickets.map((ticket) => ticket.id)
const filename_hash = await Hash.make(JSON.stringify(filename_ids))
const filepath = 'ticket_' + filename_hash.substr(0, 16) + '.csv'

const exists = await Drive.exists(filepath)
if (exists) {
Expand Down Expand Up @@ -56,8 +60,10 @@ class ExpenseExportService {

async exportPDF(tickets) {
tickets = tickets.toJSON()
var filename_ids = tickets.map((ticket) => ticket.id)
const filepath = 'ticket_' + JSON.stringify(filename_ids) + '.pdf'

const filename_ids = tickets.map((ticket) => ticket.id)
const filename_hash = await Hash.make(JSON.stringify(filename_ids))
const filepath = 'ticket_' + filename_hash.substr(0, 16) + '.pdf'

const exists = await Drive.exists(filepath)
if (exists) {
Expand Down Expand Up @@ -94,8 +100,10 @@ class ExpenseExportService {

async exportJSON(tickets) {
tickets = tickets.toJSON()
var filename_ids = tickets.map((ticket) => ticket.id)
const filepath = 'ticket_' + JSON.stringify(filename_ids) + '.json'

const filename_ids = tickets.map((ticket) => ticket.id)
const filename_hash = await Hash.make(JSON.stringify(filename_ids))
const filepath = 'ticket_' + filename_hash.substr(0, 16) + '.json'

const exists = await Drive.exists(filepath)
if (exists) {
Expand All @@ -108,8 +116,10 @@ class ExpenseExportService {

async exportYAML(tickets) {
tickets = tickets.toJSON()
var filename_ids = tickets.map((ticket) => ticket.id)
const filepath = 'ticket_' + JSON.stringify(filename_ids) + '.yml'

const filename_ids = tickets.map((ticket) => ticket.id)
const filename_hash = await Hash.make(JSON.stringify(filename_ids))
const filepath = 'ticket_' + filename_hash.substr(0, 16) + '.yml'

const exists = await Drive.exists(filepath)
if (exists) {
Expand Down
9 changes: 0 additions & 9 deletions test/functional/controllers/export-ticket-controller.spec.js
Expand Up @@ -73,7 +73,6 @@ test('Assert can export as pdf', async ({ client }) => {

response.assertStatus(200)
response.assertHeader('content-type', 'application/pdf')
response.assertHeader('content-disposition', 'attachment; filename="ticket_[' + ticket1.id + '].pdf"')
})

test('Assert can export as csv', async ({ client }) => {
Expand All @@ -85,7 +84,6 @@ test('Assert can export as csv', async ({ client }) => {

response.assertStatus(200)
response.assertHeader('content-type', 'text/csv; charset=UTF-8')
response.assertHeader('content-disposition', 'attachment; filename="ticket_[' + ticket1.id + '].csv"')
})

test('Assert can export as JSON', async ({ client, assert }) => {
Expand All @@ -97,7 +95,6 @@ test('Assert can export as JSON', async ({ client, assert }) => {

response.assertStatus(200)
response.assertHeader('content-type', 'application/json; charset=UTF-8')
response.assertHeader('content-disposition', 'attachment; filename="ticket_[' + ticket1.id + '].json"')
assert.include(response.text, '"id": ' + ticket1.id)
assert.include(response.text, '"user_id": ' + ticket1.user_id)
assert.include(response.text, '"title": "' + ticket1.title + '"')
Expand All @@ -113,7 +110,6 @@ test('Assert can export as YAML', async ({ client, assert }) => {

response.assertStatus(200)
response.assertHeader('content-type', 'text/yaml; charset=UTF-8')
response.assertHeader('content-disposition', 'attachment; filename="ticket_[' + ticket1.id + '].yml"')
assert.include(response.text, 'id: ' + ticket1.id)
assert.include(response.text, 'user_id: ' + ticket1.user_id)
assert.include(response.text, 'title: ' + ticket1.title)
Expand All @@ -139,10 +135,6 @@ test('Assert a admin user can export tickets that belong to anyone in the organi

response.assertStatus(200)
response.assertHeader('content-type', 'application/json; charset=UTF-8')
response.assertHeader(
'content-disposition',
'attachment; filename="ticket_[' + ticket1.id + ',' + ticket2.id + '].json"'
)

const response_data = JSON.parse(response.text)
assert.equal(response_data.length, 2)
Expand All @@ -162,7 +154,6 @@ test('Assert a non-admin user can include non-existent or non-premissible ticket

response.assertStatus(200)
response.assertHeader('content-type', 'application/json; charset=UTF-8')
response.assertHeader('content-disposition', 'attachment; filename="ticket_[' + ticket1.id + '].json"')

const response_data = JSON.parse(response.text)
assert.equal(response_data.length, 1)
Expand Down
4 changes: 0 additions & 4 deletions test/functional/controllers/ticket-controller.spec.js
Expand Up @@ -191,7 +191,6 @@ test('Make sure user downloads ticket with no redirect and returns YAML file', a
const currTicket = await Ticket.find(ticket.id)

response.assertHeader('content-type', 'text/yaml; charset=UTF-8')
response.assertHeader('content-disposition', 'attachment; filename="ticket_[' + currTicket.id + '].yml"')
assert.include(response.text, 'id: ' + currTicket.id)
assert.include(response.text, 'user_id: ' + currTicket.user_id)
assert.include(response.text, 'title: ' + currTicket.title)
Expand All @@ -208,7 +207,6 @@ test('Make sure user downloads ticket with no redirect and returns JSON file', a
const currTicket = await Ticket.find(ticket.id)

response.assertHeader('content-type', 'application/json; charset=UTF-8')
response.assertHeader('content-disposition', 'attachment; filename="ticket_[' + currTicket.id + '].json"')
assert.include(response.text, '"id": ' + currTicket.id)
assert.include(response.text, '"user_id": ' + currTicket.user_id)
assert.include(response.text, '"title": "' + currTicket.title + '"')
Expand All @@ -222,7 +220,6 @@ test('Make sure user downloads ticket with no redirect and returns CSV file', as
.end()

response.assertHeader('content-type', 'text/csv; charset=UTF-8')
response.assertHeader('content-disposition', 'attachment; filename="ticket_[' + ticket.id + '].csv"')
})

test('Make sure user downloads ticket with no redirect and returns PDF file', async ({ client }) => {
Expand All @@ -232,5 +229,4 @@ test('Make sure user downloads ticket with no redirect and returns PDF file', as
.end()

response.assertHeader('content-type', 'application/pdf')
response.assertHeader('content-disposition', 'attachment; filename="ticket_[' + ticket.id + '].pdf"')
})