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
12 changes: 11 additions & 1 deletion providers/Qobuz/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { assert } from 'std/assert/assert.ts';
import { afterAll, describe, it } from '@std/testing/bdd';
import { assertSnapshot } from '@std/testing/snapshot';

import QobuzProvider from './mod.ts';
import QobuzProvider, { normalizeQobuzGtin } from './mod.ts';
import { assertEquals } from 'std/assert/assert_equals.ts';

describe('Qobuz provider', () => {
Expand Down Expand Up @@ -129,6 +129,16 @@ describe('Qobuz provider', () => {
);
});

describe('GTIN normalization', () => {
it('appends a missing check digit to invalid 13-digit barcodes, turning them into valid GTIN-14', () => {
assertEquals(normalizeQobuzGtin('0001589171735'), '00015891717357');
});

it('preserves GTIN-13 values which already have a valid check digit', () => {
assertEquals(normalizeQobuzGtin('0198884774947'), '0198884774947');
});
});

afterAll(() => {
lookupStub.restore();
});
Expand Down
13 changes: 11 additions & 2 deletions providers/Qobuz/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DurationPrecision, FeatureQuality, FeatureQualityMap } from '@/provider
import { getFromEnv } from '@/utils/config.ts';
import { parseHyphenatedDate, PartialDate } from '@/utils/date.ts';
import { ResponseError } from '@/utils/errors.ts';
import { isEqualGTIN } from '@/utils/gtin.ts';
import { checkDigit, isEqualGTIN, isValidGTIN } from '@/utils/gtin.ts';
import type {
ArtistCreditName,
Artwork,
Expand All @@ -29,6 +29,15 @@ import { ResponseError as SnapResponseError } from 'snap-storage';

const qobuzAppId = getFromEnv('HARMONY_QOBUZ_APP_ID') || '';

/** Appends the missing check digit to invalid 13 digit barcodes from older Qobuz releases. */
export function normalizeQobuzGtin(upc: string): string {
Comment thread
kellnerd marked this conversation as resolved.
if (isValidGTIN(upc) || !/^\d{13}$/.test(upc)) {
return upc;
}

return `${upc}${checkDigit(`${upc}0`)}`;
}

export default class QobuzProvider extends MetadataApiProvider {
readonly name = 'Qobuz';

Expand Down Expand Up @@ -245,7 +254,7 @@ export class QobuzReleaseLookup extends ReleaseApiLookup<QobuzProvider, QobuzAlb
types: linkTypes,
}],
info: this.generateReleaseInfo(),
gtin: rawRelease.upc,
gtin: normalizeQobuzGtin(rawRelease.upc),
};
}

Expand Down
Loading