-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use application object for awards backend (#30)
* set up Awards app class, port put to use that * fix put * move delete into Awards class * create shared "get award by uid" func * move get to class * move create to class * add copyright headers * fix tsc * run prettier
- Loading branch information
Showing
3 changed files
with
89 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright SeatGeek | ||
* Licensed under the terms of the Apache-2.0 license. See LICENSE file in project root for terms. | ||
*/ | ||
import { NotFoundError } from '@backstage/errors'; | ||
import { Award, AwardInput } from '@seatgeek/backstage-plugin-awards-common'; | ||
import { Logger } from 'winston'; | ||
import { AwardsStore } from './database/awards'; | ||
|
||
export class Awards { | ||
private readonly db: AwardsStore; | ||
private readonly logger: Logger; | ||
|
||
constructor(db: AwardsStore, logger: Logger) { | ||
this.db = db; | ||
this.logger = logger.child({ class: 'Awards' }); | ||
this.logger.debug('Constructed'); | ||
} | ||
|
||
async get(uid: string): Promise<Award> { | ||
return await this.getAwardByUid(uid); | ||
} | ||
|
||
async create(input: AwardInput): Promise<Award> { | ||
return await this.db.add( | ||
input.name, | ||
input.description, | ||
input.image, | ||
input.owners, | ||
input.recipients, | ||
); | ||
} | ||
|
||
async update( | ||
identityRef: string, | ||
uid: string, | ||
input: AwardInput, | ||
): Promise<Award> { | ||
const award = await this.getAwardByUid(uid); | ||
|
||
if (!award.owners.includes(identityRef)) { | ||
throw new Error('Unauthorized to update award'); | ||
} | ||
|
||
const updated = await this.db.update( | ||
uid, | ||
input.name, | ||
input.description, | ||
input.image, | ||
input.owners, | ||
input.recipients, | ||
); | ||
|
||
return updated; | ||
} | ||
|
||
async delete(identityRef: string, uid: string): Promise<boolean> { | ||
const award = await this.getAwardByUid(uid); | ||
|
||
if (!award.owners.includes(identityRef)) { | ||
throw new Error('Unauthorized to delete award'); | ||
} | ||
|
||
return await this.db.delete(uid); | ||
} | ||
|
||
private async getAwardByUid(uid: string): Promise<Award> { | ||
const res = await this.db.search(uid, '', [], []); | ||
|
||
if (!res || res.length === 0) { | ||
throw new NotFoundError(uid); | ||
} | ||
|
||
return res[0]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,5 @@ export interface Award { | |
owners: string[]; | ||
recipients: string[]; | ||
} | ||
|
||
export type AwardInput = Omit<Award, 'uid'>; |