diff --git a/src/plus/focus/focusService.ts b/src/plus/focus/focusService.ts index 454199a..32178f5 100644 --- a/src/plus/focus/focusService.ts +++ b/src/plus/focus/focusService.ts @@ -4,29 +4,62 @@ import type { Container } from '../../container'; import type { ServerConnection } from '../gk/serverConnection'; export interface FocusItem { - provider: string; - type: 'issue' | 'pr'; + provider: EnrichedItemResponse['provider']; + type: EnrichedItemResponse['entityType']; id: string; - repositoryId: string; repositoryName: string; repositoryOwner: string; } -export interface EnrichedItem { +export type EnrichedItem = { id: string; userId: string; - type: 'pinned' | 'snoozed'; + type: EnrichedItemResponse['type']; - provider: string; + provider: EnrichedItemResponse['provider']; + entityType: EnrichedItemResponse['entityType']; + entityId: string; + + createdAt: number; + updatedAt: number; +} & ( + | { repositoryId: string } + | { + repositoryName: string; + repositoryOwner: string; + } +); + +type EnrichedItemRequest = { + provider: EnrichedItemResponse['provider']; + type: EnrichedItemResponse['entityType']; + id: string; +} & ( + | { repositoryId: string } + | { + repositoryName: string; + repositoryOwner: string; + } +); + +type EnrichedItemResponse = { + id: string; + userId: string; + type: 'pin' | 'snooze'; + + provider: 'azure' | 'bitbucket' | 'github' | 'gitlab' | 'gitkraken'; entityType: 'issue' | 'pr'; entityId: string; - repositoryId: string; - repositoryName: string; - repositoryOwner: string; createdAt: number; updatedAt: number; -} +} & ( + | { repositoryId: string } + | { + repositoryName: string; + repositoryOwner: string; + } +); export class FocusService implements Disposable { constructor( @@ -36,55 +69,74 @@ export class FocusService implements Disposable { dispose(): void {} - async pinItem(item: FocusItem): Promise { - type Result = { data: EnrichedItem }; + private async delete(id: string): Promise { + const rsp = await this.connection.fetch( + Uri.joinPath(this.connection.baseGkApiUri, `v1/enrich-items/${id}`).toString(), + { + method: 'DELETE', + }, + ); + if (!rsp.ok) { + debugger; + throw new Error(`Unable to delete enrichment: ${rsp.statusText}`); + } + } + + async get(type?: EnrichedItemResponse['type']): Promise { + type Result = { data: EnrichedItemResponse[] }; const rsp = await this.connection.fetch( - Uri.joinPath(this.connection.baseGkApiUri, 'v1/enrich-items/pin').toString(), + Uri.joinPath(this.connection.baseGkApiUri, 'v1/enrich-items').toString(), { - method: 'POST', - body: JSON.stringify(item), + method: 'GET', }, ); + const result = (await rsp.json()) as Result; - return result.data; + return type == null ? result.data : result.data.filter(i => i.type === type); } - async snoozeItem(item: FocusItem): Promise { - type Result = { data: EnrichedItem }; + getPins(): Promise { + return this.get('pin'); + } + + getSnoozed(): Promise { + return this.get('snooze'); + } + + async pinItem(item: FocusItem): Promise { + type Result = { data: EnrichedItemResponse }; const rsp = await this.connection.fetch( - Uri.joinPath(this.connection.baseGkApiUri, 'v1/enrich-items/snooze').toString(), + Uri.joinPath(this.connection.baseGkApiUri, 'v1/enrich-items/pin').toString(), { method: 'POST', - body: JSON.stringify(item), + body: JSON.stringify(item satisfies EnrichedItemRequest), }, ); const result = (await rsp.json()) as Result; return result.data; } - async getPins(): Promise { - const data = await this.getAll(); - return data.filter(i => i.type === 'pinned'); + unpinItem(id: string): Promise { + return this.delete(id); } - async getSnoozed(): Promise { - const data = await this.getAll(); - return data.filter(i => i.type === 'snoozed'); - } - - async getAll(): Promise { - type Result = { data: EnrichedItem[] }; + async snoozeItem(item: FocusItem): Promise { + type Result = { data: EnrichedItemResponse }; const rsp = await this.connection.fetch( - Uri.joinPath(this.connection.baseGkApiUri, 'v1/enrich-items').toString(), + Uri.joinPath(this.connection.baseGkApiUri, 'v1/enrich-items/snooze').toString(), { - method: 'GET', + method: 'POST', + body: JSON.stringify(item satisfies EnrichedItemRequest), }, ); - const result = (await rsp.json()) as Result; - return result.data.map(i => i); + return result.data; + } + + unsnoozeItem(id: string): Promise { + return this.delete(id); } }