Skip to content

Commit

Permalink
fix: use work when there's no versions
Browse files Browse the repository at this point in the history
Fixes #42
  • Loading branch information
dvirtz committed Jan 6, 2025
1 parent 247f8cf commit 08ec232
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
32 changes: 16 additions & 16 deletions src/acum-work-import/acum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,26 @@ type AlbumInfoResponse = Response<{
}>;

export type WorkVersion = WorkBean & {
versionNumber: string;
creators: Creators;
authors?: ReadonlyArray<Creator>;
composers?: ReadonlyArray<Creator>;
arrangers?: ReadonlyArray<Creator>;
translators?: ReadonlyArray<Creator>;
composersAndAuthors?: ReadonlyArray<Creator>;
versionIswcNumber: string;
versionEssenceType: string;
albumTrackNumber: string;
};

type WorkBean = Bean<'org.acum.site.searchdb.dto.bean.WorkBean'> & {
export type WorkBean = Bean<'org.acum.site.searchdb.dto.bean.WorkBean'> & {
work_id: string;
fullWorkId: string;
workNumber: string;
workLanguage: string;
workHebName: string;
workEngName: string;
workId: string;
versionNumber: string;
creators?: Creators;
authors?: ReadonlyArray<Creator>;
composers?: ReadonlyArray<Creator>;
arrangers?: ReadonlyArray<Creator>;
translators?: ReadonlyArray<Creator>;
composersAndAuthors?: ReadonlyArray<Creator>;
versionIswcNumber: string;
versionEssenceType: string;
};

type WorkInfoResponse = Response<{
Expand All @@ -82,13 +82,13 @@ type WorkInfoResponse = Response<{
workAlbums: ReadonlyArray<AlbumBean>;
}>;

export async function workVersions(workId: string): Promise<ReadonlyArray<WorkVersion> | undefined> {
export async function fetchWork(workId: string): Promise<ReadonlyArray<WorkBean> | undefined> {
const result = await tryFetchJSON<WorkInfoResponse>(
`https://nocs.acum.org.il/acumsitesearchdb/getworkinfo?workId=${workId}`
);
if (result) {
if (result.errorCode == 0) {
return result.data.workVersions;
return result.data.workVersions ? result.data.workVersions : [result.data.work];
}

console.error('failed to fetch work %s: %s', workId, result.errorDescription);
Expand Down Expand Up @@ -121,13 +121,13 @@ export async function getAlbumInfo(albumId: string): Promise<AlbumBean | undefin
export async function workISWCs(workID: string) {
const formatISWC = (iswc: string) => iswc.replace(/T(\d{3})(\d{3})(\d{3})(\d)/, 'T-$1.$2.$3-$4');

return (await workVersions(workID))
return (await fetchWork(workID))
?.map(albumVersion => albumVersion.versionIswcNumber)
.filter(iswc => iswc.length > 0)
.map(formatISWC);
}

export function trackName(track: WorkVersion) {
export function trackName(track: WorkBean): string {
return workLanguage(track) == WorkLanguage.Hebrew ? track.workHebName : track.workEngName;
}

Expand All @@ -147,7 +147,7 @@ function stringToEnum<T>(value: string, enumType: {[s: string]: T}): T {
return enumType.Unknown;
}

export function essenceType(track: WorkVersion): EssenceType {
export function essenceType(track: WorkBean): EssenceType {
return stringToEnum(track.versionEssenceType, EssenceType);
}

Expand All @@ -158,7 +158,7 @@ export enum WorkLanguage {
Unknown = '-1',
}

export function workLanguage(track: WorkVersion): WorkLanguage {
export function workLanguage(track: WorkBean): WorkLanguage {
return stringToEnum(track.workLanguage, WorkLanguage);
}

Expand Down
10 changes: 7 additions & 3 deletions src/acum-work-import/artists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ function nameMatch(creator: CreatorFull, artistName: string): boolean {

async function findArtist(
ipBaseNumber: IPBaseNumber,
creators: Creators,
creators: Creators | undefined,
addWarning: AddWarning
): Promise<ArtistT | null> {
const artistMBID = await (async () => {
const creator = creators.find(creator => creator.creatorIpBaseNumber === ipBaseNumber)!;
const creator = creators?.find(creator => creator.creatorIpBaseNumber === ipBaseNumber);
if (!creator) {
addWarning(`failed to find creator with IPI ${ipBaseNumber}`);
return null;
}
const role = (() => {
switch (creator.roleCode) {
case RoleCode.Composer:
Expand Down Expand Up @@ -67,7 +71,7 @@ async function findArtist(
export async function linkArtists(
artistCache: Map<string, Promise<ArtistT | null>>,
writers: readonly Creator[] | undefined,
creators: Creators,
creators: Creators | undefined,
doLink: (artist: ArtistT) => void,
addWarning: (message: string) => Set<string>
) {
Expand Down
2 changes: 1 addition & 1 deletion src/acum-work-import/import-album.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function importAlbum(
const linkArrangers = async (
recording: RecordingT,
arrangers: ReadonlyArray<Creator> | undefined,
creators: Creators,
creators: Creators | undefined,
addWarning: AddWarning
) => {
await linkArtists(
Expand Down
4 changes: 2 additions & 2 deletions src/acum-work-import/import-work.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {filter, from, lastValueFrom, map, switchMap, take, tap, toArray, zip} fr
import {asyncTap} from 'src/common/lib/asyncTap';
import {compareInsensitive} from 'src/common/lib/compare';
import {addEditNote} from 'src/common/musicbrainz/edit-note';
import {IPBaseNumber, workUrl, workVersions} from './acum';
import {fetchWork, IPBaseNumber, workUrl} from './acum';
import {addWriterRelationship} from './relationships';
import {AddWarning} from './ui/warnings';
import {workEditData} from './ui/work-edit-data';
Expand All @@ -18,7 +18,7 @@ export async function importWork(workId: string, form: HTMLFormElement, addWarni
name: form.querySelector('[name="edit-work.name"]')?.getAttribute('value') || '',
});

const versions = await workVersions(workId);
const versions = await fetchWork(workId);
if (!versions) {
alert(`failed to find work ID ${workId}`);
throw new Error(`failed to find work ID ${workId}`);
Expand Down
4 changes: 2 additions & 2 deletions src/acum-work-import/ui/work-edit-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {mergeArrays} from 'src/common/lib/merge-arrays';
import {LANGUAGE_ZXX_ID} from 'src/common/musicbrainz/constants';
import {fetchEditParams, urlFromMbid} from 'src/common/musicbrainz/edits';
import {workAttributeTypes, workLanguages, workTypes} from 'src/common/musicbrainz/type-info';
import {essenceType, EssenceType, trackName, workISWCs, workLanguage, WorkLanguage, WorkVersion} from '../acum';
import {essenceType, EssenceType, trackName, WorkBean, workISWCs, workLanguage, WorkLanguage} from '../acum';
import {WorkStateWithEditDataT} from '../work-state';
import {AddWarning} from './warnings';

Expand Down Expand Up @@ -62,7 +62,7 @@ export function workEditDataEqual(lhs: WorkEditData, rhs: WorkEditData) {

export async function workEditData(
work: WorkT,
track: WorkVersion,
track: WorkBean,
addWarning: AddWarning
): Promise<{originalEditData: WorkEditData; editData: WorkEditData}> {
const originalEditData =
Expand Down
4 changes: 2 additions & 2 deletions src/acum-work-import/works.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
TRANSLATOR_LINK_TYPE_ID,
} from 'src/common/musicbrainz/constants';
import {iterateRelationshipsInTargetTypeGroup} from 'src/common/musicbrainz/type-group';
import {trackName, WorkVersion} from './acum';
import {trackName, WorkBean, WorkVersion} from './acum';
import {linkArtists} from './artists';
import {createRelationshipState} from './relationships';
import {AddWarning} from './ui/warnings';
Expand Down Expand Up @@ -131,7 +131,7 @@ export function createWork(attributes: Partial<WorkT>): WorkT {

export async function linkWriters(
artistCache: Map<string, Promise<ArtistT | null>>,
track: WorkVersion,
track: WorkBean,
doLink: (artist: ArtistT, linkTypeID: number) => void,
addWarning: (message: string) => Set<string>
) {
Expand Down

0 comments on commit 08ec232

Please sign in to comment.