async updateImage()

in src/commands/bulk/rights.ts [54:97]


  async updateImage(id: string, updateMessage: string, output: WriteStream, failures: WriteStream) {
    const image = await this.fetchImage(id)
    if (!image) {
      this.log(`Error retreiving ${id}`)
      failures.write(`${id}\n`)
      output.write(`${id}\t ERROR\n`)
      return false
    }

    if ('errorMessage' in image) {
      this.log(`Could not find ${id} ${image.errorMessage}`)
      failures.write(`${id}\n`)
      output.write(`${id}\t ${image.errorKey}\n`)
      return false
    }

    const imageRightsJSON = JSON.stringify(image.data.usageRights)

    const imageRights = this.getRights(imageRightsJSON)

    const edit = image.links.find((_: { rel: string }) => _.rel === 'edits').href

    const endpoint = new URL(`${edit}/usage-rights`)
    try {
      const response = await this.http!.put(endpoint, updateMessage)

      if (response.status !== 200) {
        this.log(`${id} update failed with status code ${response.status}`)
        failures.write(`${id}\n`)
        output.write(`${id}\t FAILED\n`)
        this.log(await response.text())
        return false
      }

      const responseData = await response.json()
      const newRights = this.getRights(JSON.stringify(responseData.data))
      this.log(`Succesfully updated ${id} from ${imageRights} to ${newRights}`)
      output.write(`${id}\t SUCCESS\t ${newRights}\n`)
    } catch {
      this.log(`Update failed on ${id}`)
      failures.write(`${id}\n`)
      output.write(`${id}\t FAILED\n`)
    }
  }