Skip to content

Commit

Permalink
Merge pull request thoughtworks#318 from setchy/feature/doc-sheet-bac…
Browse files Browse the repository at this point in the history
…kwards-compatibility

feat: add backwards compatibility for sheetId
  • Loading branch information
devansh-sharma-tw committed Jul 17, 2023
2 parents 28bc969 + 2b19a19 commit 3e35804
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 17 deletions.
50 changes: 49 additions & 1 deletion spec/util/urlUtils-spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { constructSheetUrl } = require('../../src/util/urlUtils')
const { constructSheetUrl, getDocumentOrSheetId, getSheetName } = require('../../src/util/urlUtils')
const config = require('../../src/config')
const queryParams = require('../../src/util/queryParamProcessor')

Expand Down Expand Up @@ -32,4 +32,52 @@ describe('Url Utils', () => {
expect(config).toHaveBeenCalledTimes(1)
expect(queryParams).toHaveBeenCalledTimes(1)
})

it('should prioritize documentId before legacy sheetId', () => {
queryParams.mockReturnValue({ documentId: 'documentId', sheetId: 'sheetId' })
delete window.location
window.location = Object.create(window)
window.location.href = 'https://thoughtworks.com/radar?documentId=documentId&sheetId=sheetId'
window.location.search = '?'

const id = getDocumentOrSheetId()

expect(id).toEqual('documentId')
})

it('supports documentId', () => {
queryParams.mockReturnValue({ documentId: 'documentId' })
delete window.location
window.location = Object.create(window)
window.location.href = 'https://thoughtworks.com/radar?documentId=documentId'
window.location.search = '?'

const id = getDocumentOrSheetId()

expect(id).toEqual('documentId')
})

it('supports sheetId', () => {
queryParams.mockReturnValue({ sheetId: 'sheetId' })
delete window.location
window.location = Object.create(window)
window.location.href = 'https://thoughtworks.com/radar?sheetId=sheetId'
window.location.search = '?'

const id = getDocumentOrSheetId()

expect(id).toEqual('sheetId')
})

it('supports sheetName', () => {
queryParams.mockReturnValue({ sheetName: 'sheetName' })
delete window.location
window.location = Object.create(window)
window.location.href = 'https://thoughtworks.com/radar?sheetName=sheetName'
window.location.search = '?'

const sheetName = getSheetName()

expect(sheetName).toEqual('sheetName')
})
})
22 changes: 6 additions & 16 deletions src/util/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const Quadrant = require('../models/quadrant')
const Ring = require('../models/ring')
const Blip = require('../models/blip')
const GraphingRadar = require('../graphing/radar')
const QueryParams = require('./queryParamProcessor')
const MalformedDataError = require('../exceptions/malformedDataError')
const SheetNotFoundError = require('../exceptions/sheetNotFoundError')
const ContentValidator = require('./contentValidator')
Expand All @@ -22,6 +21,7 @@ const ExceptionMessages = require('./exceptionMessages')
const GoogleAuth = require('./googleAuth')
const config = require('../config')
const featureToggles = config().featureToggles
const { getDocumentOrSheetId, getSheetName } = require('./urlUtils')
const { getGraphSize, graphConfig, isValidConfig } = require('../graphing/config')
const InvalidConfigError = require('../exceptions/invalidConfigError')
const InvalidContentError = require('../exceptions/invalidContentError')
Expand Down Expand Up @@ -317,20 +317,17 @@ const Factory = function () {
})

const domainName = DomainName(window.location.search.substring(1))
const queryString = featureToggles.UIRefresh2022
? window.location.href.match(/documentId(.*)/)
: window.location.href.match(/sheetId(.*)/)
const queryParams = queryString ? QueryParams(queryString[0]) : {}

const paramId = featureToggles.UIRefresh2022 ? queryParams.documentId : queryParams.sheetId
const paramId = getDocumentOrSheetId()
if (paramId && paramId.endsWith('.csv')) {
sheet = CSVDocument(paramId)
sheet.init().build()
} else if (paramId && paramId.endsWith('.json')) {
sheet = JSONFile(paramId)
sheet.init().build()
} else if (domainName && domainName.endsWith('google.com') && paramId) {
sheet = GoogleSheet(paramId, queryParams.sheetName)
const sheetName = getSheetName()
sheet = GoogleSheet(paramId, sheetName)
sheet.init().build()
} else {
if (!featureToggles.UIRefresh2022) {
Expand Down Expand Up @@ -540,15 +537,8 @@ function plotUnauthorizedErrorMessage() {

button.on('click', () => {
let sheet
if (featureToggles.UIRefresh2022) {
const queryString = window.location.href.match(/documentId(.*)/)
const queryParams = queryString ? QueryParams(queryString[0]) : {}
sheet = GoogleSheet(queryParams.documentId, queryParams.sheetName)
} else {
const queryString = window.location.href.match(/sheetId(.*)/)
const queryParams = queryString ? QueryParams(queryString[0]) : {}
sheet = GoogleSheet(queryParams.sheetId, queryParams.sheetName)
}
sheet = GoogleSheet(getDocumentOrSheetId(), getSheetName())

sheet.authenticate(true, false, () => {
if (featureToggles.UIRefresh2022 && !sheet.error) {
helperDescription.style('display', 'block')
Expand Down
12 changes: 12 additions & 0 deletions src/util/urlUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ function constructSheetUrl(sheetName) {
return sheetUrl
}

function getDocumentOrSheetId() {
const queryParams = QueryParams(window.location.search.substring(1))
return queryParams.documentId ?? queryParams.sheetId
}

function getSheetName() {
const queryParams = QueryParams(window.location.search.substring(1))
return queryParams.sheetName
}

module.exports = {
constructSheetUrl,
getDocumentOrSheetId,
getSheetName,
}

0 comments on commit 3e35804

Please sign in to comment.