This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HYP-2: promoting example controller to a real token, also pushing as …
…a current state, tests need adjusting.
- Loading branch information
Showing
19 changed files
with
239 additions
and
192 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
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,102 @@ | ||
import { | ||
ValidateError, | ||
Controller, | ||
Post, | ||
Get, | ||
Route, | ||
Response, | ||
Body, | ||
SuccessResponse, | ||
Tags, | ||
Security, | ||
Path, | ||
Query, | ||
} from 'tsoa' | ||
import type { Logger } from 'pino' | ||
import { injectable } from 'tsyringe' | ||
|
||
import { logger } from '../../../lib/logger' | ||
import Database, { CertificateRow } from '../../../lib/db' | ||
import { BadRequest, NotFound } from '../../../lib/error-handler/index' | ||
import Identity from '../../../lib/services/identity' | ||
import * as Certificate from '../../../models/certificate' | ||
import { DATE, UUID } from '../../../models/strings' | ||
// import { TransactionResponse, TransactionType } from '../../../models/transaction' | ||
import ChainNode from '../../../lib/chainNode' | ||
import env from '../../../env' | ||
import { camelToSnake } from 'src/lib/utils/shared' | ||
|
||
@Route('v1/certificate') | ||
@injectable() | ||
@Tags('certificate') | ||
@Security('BearerAuth') | ||
export class CertificateController extends Controller { | ||
log: Logger | ||
db: Database | ||
node: ChainNode | ||
|
||
constructor(private identity: Identity) { | ||
super() | ||
this.log = logger.child({ controller: '/certificate' }) | ||
this.db = new Database() | ||
this.node = new ChainNode({ | ||
host: env.NODE_HOST, | ||
port: env.NODE_PORT, | ||
logger, | ||
userUri: env.USER_URI, | ||
}) | ||
this.identity = identity | ||
} | ||
|
||
/** | ||
* @summary insert a certificate for initialisation | ||
*/ | ||
@Post() | ||
@Response<BadRequest>(400, 'Request was invalid') | ||
@Response<ValidateError>(422, 'Validation Failed') | ||
@SuccessResponse('201') | ||
public async post(@Body() body: Certificate.Request): Promise<Certificate.Response> { | ||
this.log.info({ identity: this.identity, body }) | ||
|
||
const formatted = Object.keys(body).reduce((out, key) => ({ | ||
[camelToSnake(key)]: body[key], | ||
...out, | ||
}), {}) | ||
|
||
const result: CertificateRow[] | string = await this.db.insert('certificate', formatted) | ||
|
||
return { message: 'ok', result } | ||
} | ||
|
||
/** | ||
* description | ||
* @summary Lists all local certificates | ||
* @param createdAt - is optional and will return certificates based on options provided | ||
* TODO - combine where so with all possible options e.g. | ||
* columns so it's not a default and will be rendered in swagger | ||
*/ | ||
@Get('/') | ||
public async getAll(@Query() createdAt?: DATE): Promise<Certificate.Response> { | ||
const where: Record<string, number | string | DATE> = {} | ||
if (createdAt) where.created_at = createdAt | ||
const certificates = await this.db.get('certificate', where) | ||
|
||
return { message: 'ok', certificates } | ||
} | ||
|
||
/** | ||
* @summary | ||
* @param id The certificate's identifier | ||
*/ | ||
@Response<ValidateError>(422, 'Validation Failed') | ||
@Response<NotFound>(404, 'Item not found') | ||
@Get('{id}') | ||
public async getById(@Path() id: UUID): Promise<Certificate.Response> { | ||
if (!id) throw new BadRequest() | ||
|
||
return { | ||
message: 'ok', | ||
certificate: await this.db.get('certificate', { id }), | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.