Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 5x 5x 5x 5x 5x 5x 6x 6x 6x 6x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 12x 12x 12x 12x 5x 5x 5x 1x 1x 1x 1x 1x 5x 5x 5x 1x 1x 5x 5x 5x 5x 5x 5x 5x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x | import { Controller, Get, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { Client } from '@notionhq/client';
import {
DataSourceObjectResponse,
PartialDataSourceObjectResponse,
QueryDataSourceParameters,
RichTextItemResponse,
} from '@notionhq/client/build/src/api-endpoints';
import { get } from 'lodash-es';
import {
CHANGELOG_VISIBILITY_PER_ROLE,
ChangelogEntry,
ChangelogEntryResponse,
ChangelogRole,
} from '@amalia/amalia-meta/changelog/shared';
import { type User } from '@amalia/core/models';
import { BooleanPipe, StringPipe } from '@amalia/kernel/api';
import { AmaliaAuthGuard, CurrentUser, PoliciesGuard } from '@amalia/kernel/auth/core';
import { configuration } from '../../configuration';
@UseGuards(AmaliaAuthGuard, PoliciesGuard)
@ApiBearerAuth()
@ApiTags('changelog-entries')
@Controller('changelog-entries')
export class ChangelogController {
@Get()
public async findAll(
@CurrentUser() user: User,
@Query('cursor', new StringPipe({ optional: true })) cursor?: string,
@Query('new', new BooleanPipe({ optional: true })) requestNew?: boolean,
@Query('from') from?: string,
): Promise<ChangelogEntryResponse> {
// If asking for new changelog, but we have no from, it's because the user never connected.
// Don't return anything to not blast him with all changelog entries ever.
if ((requestNew && !from) || !configuration.notion.accessToken) {
return { entries: [], cursor: null, hasMore: false };
}
const notion = new Client({ auth: configuration.notion.accessToken });
const { results, next_cursor, has_more } = await notion.dataSources.query({
data_source_id: configuration.notion.changelogDataSourceId,
page_size: 10,
start_cursor: cursor || undefined,
filter: ChangelogController.getNotionFilters(user, from),
sorts: ChangelogController.getNotionSort(),
});
return {
cursor: next_cursor,
hasMore: has_more,
entries: ChangelogController.databaseRowsToChangelogEntries(
results as DataSourceObjectResponse[] | PartialDataSourceObjectResponse[],
),
};
}
private static getNotionFilters(user: User, from?: string): QueryDataSourceParameters['filter'] {
return {
and: [
{
property: 'Published',
checkbox: {
equals: true,
},
},
{
or: CHANGELOG_VISIBILITY_PER_ROLE[user.role].map((accessRight) => ({
property: 'Role',
multi_select: {
contains: accessRight as string,
},
})),
},
from && {
property: 'Date',
date: {
after: from,
},
},
].filter(Boolean),
};
}
private static getNotionSort() {
return [
{
property: 'Date',
direction: 'descending' as const,
},
];
}
private static databaseRowsToChangelogEntries(
rows: DataSourceObjectResponse[] | PartialDataSourceObjectResponse[],
): ChangelogEntry[] {
return rows.map(
(row: { id: string; properties: Record<string, unknown> }) =>
({
id: row.id,
name: get(row.properties, 'Name.title.0.plain_text') as string,
content: get(row.properties, 'Content.rich_text') as ChangelogEntry['content'],
topic: get(row.properties, 'Topic.select.name') as string,
date: get(row.properties, 'Date.date.start') as string,
imageUrl: get(row.properties, 'Image.files.0.file.url') as string,
importance: get(row.properties, 'Importance.select.name') as string,
roles: (get(row.properties, 'Role.multi_select', []) as { name: string }[]).map(
(role: { name: string }) => role.name,
) as ChangelogRole[],
releaseType: get(row.properties, 'Release Type.select.name') as string,
url: get(row.properties, 'URL.url') as string,
urlLabel:
(get(row.properties, 'URL Label.rich_text', []) as RichTextItemResponse[]).length === 0
? undefined
: get(row.properties, 'URL Label.rich_text'),
}) as ChangelogEntry,
);
}
}
|