-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(notifier-api): add a new route to create a category (#247)
- Loading branch information
Showing
18 changed files
with
239 additions
and
216 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {HttpStatusCodes, type NanotronClientRequest} from 'alwatr/nanotron'; | ||
|
||
import {config, logger} from '../config.js'; | ||
import {getAuthBearer} from '../lib/get-auth-bearer.js'; | ||
|
||
export async function requireAccessToken(this: NanotronClientRequest): Promise<void> { | ||
const token = getAuthBearer(this.headers.authorization); | ||
logger.logMethodArgs?.('requireAccessToken', {token}); | ||
|
||
if (token === null) { | ||
this.serverResponse.statusCode = HttpStatusCodes.Error_Client_401_Unauthorized; | ||
this.serverResponse.replyErrorResponse({ | ||
ok: false, | ||
errorCode: 'authorization_required', | ||
errorMessage: 'Authorization token required', | ||
}); | ||
return; | ||
} | ||
|
||
if (token !== config.accessToken) { | ||
this.serverResponse.statusCode = HttpStatusCodes.Error_Client_403_Forbidden; | ||
this.serverResponse.replyErrorResponse({ | ||
ok: false, | ||
errorCode: 'access_denied', | ||
errorMessage: 'Access denied, token is invalid!', | ||
}); | ||
} | ||
} |
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,11 @@ | ||
/** | ||
* Get the token placed in the request header. | ||
*/ | ||
export function getAuthBearer(authorizationHeader?: string): string | null { | ||
const auth = authorizationHeader?.split(' '); | ||
if (!auth || auth[0].toLowerCase() !== 'bearer' || !auth[1]) { | ||
return null; | ||
} | ||
// else | ||
return auth[1]; | ||
} |
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
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,80 @@ | ||
import {HttpStatusCodes, type NanotronClientRequest} from 'alwatr/nanotron'; | ||
|
||
import {logger} from '../config.js'; | ||
import {parseBodyAsJson} from '../handler/parse-body-as-json.js'; | ||
import {requireAccessToken} from '../handler/require-access-token.js'; | ||
import {bot} from '../lib/bot.js'; | ||
import {openCategoryCollection} from '../lib/nitrobase.js'; | ||
import {nanotronApiServer} from '../lib/server.js'; | ||
|
||
export type NewCategoryOption = { | ||
id: string; | ||
title: string; | ||
}; | ||
|
||
nanotronApiServer.defineRoute<{body: NewCategoryOption}>({ | ||
method: 'POST', | ||
url: 'new-category', | ||
preHandlers: [requireAccessToken, parseBodyAsJson, newCategoryValidation], | ||
async handler() { | ||
logger.logMethod?.('newCategoryRoute'); | ||
|
||
const {id, title} = this.sharedMeta.body; | ||
|
||
const categoryCollection = await openCategoryCollection(); | ||
|
||
categoryCollection.addItem(id, {title, members: []}); | ||
|
||
const botInfo = bot.botInfo; | ||
|
||
this.serverResponse.replyJson({ | ||
ok: true, | ||
data: { | ||
subscribe: `https://t.me/${botInfo.username}?start=${id}`, | ||
}, | ||
}); | ||
}, | ||
}); | ||
|
||
export async function newCategoryValidation(this: NanotronClientRequest<{body: JsonObject}>): Promise<void> { | ||
const {id, title} = this.sharedMeta.body; | ||
logger.logMethodArgs?.('newCategoryValidation', {id, title}); | ||
|
||
if (title === undefined || typeof title !== 'string') { | ||
this.serverResponse.statusCode = HttpStatusCodes.Error_Client_400_Bad_Request; | ||
this.serverResponse.replyErrorResponse({ | ||
ok: false, | ||
errorCode: 'title_required', | ||
errorMessage: 'Title is required.', | ||
}); | ||
return; | ||
} | ||
|
||
if (id === undefined || typeof id !== 'string') { | ||
this.serverResponse.statusCode = HttpStatusCodes.Error_Client_400_Bad_Request; | ||
this.serverResponse.replyErrorResponse({ | ||
ok: false, | ||
errorCode: 'id_required', | ||
errorMessage: 'Id is required.', | ||
}); | ||
return; | ||
} | ||
|
||
const categoryCollection = await openCategoryCollection(); | ||
|
||
if (categoryCollection.hasItem(id)) { | ||
this.serverResponse.statusCode = HttpStatusCodes.Error_Client_400_Bad_Request; | ||
this.serverResponse.replyErrorResponse({ | ||
ok: false, | ||
errorCode: 'category_exist', | ||
errorMessage: `Category ${id} already exist.`, | ||
}); | ||
return; | ||
} | ||
|
||
// just for type validation and cleanup extra | ||
(this.sharedMeta.body as NewCategoryOption) = { | ||
id, | ||
title, | ||
}; | ||
} |
Oops, something went wrong.