Skip to content

Commit

Permalink
🚧 prepare fetchMondayData
Browse files Browse the repository at this point in the history
  • Loading branch information
bouroo committed Jun 22, 2023
1 parent 9e6a960 commit 7286a3b
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ jobs:
- name: Fetch data 🏃🏻‍♂️
run: |
touch .env
echo NOCODB_API_PATH=${{ secrets.NOCODB_API_PATH }} >> .env
echo NOCODB_API_TOKEN=${{ secrets.NOCODB_API_TOKEN }} >> .env
echo MONDAY_API_TOKEN=${{ secrets.MONDAY_API_TOKEN }} >> .env
echo MONDAY_BOARD_ID=${{ secrets.MONDAY_BOARD_ID }} >> .env
yarn fetch-data
- name: Build 🍳
Expand Down
112 changes: 111 additions & 1 deletion scripts/monday-fetcher/extracts/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
import fetch from 'node-fetch';

export const LIMIT = 1000;
/**
* monday's default limit results set
* @type {number} - page limit
* @see https://developer.monday.com/api-reference/docs/rate-limits#pagination
*/
export const LIMIT: number = 25;

/**
* monday's API base URL
* @type {string} - monday's api v2 base url
* @see https://developer.monday.com/api-reference/docs
*/
const MONDAY_API_URL: string = 'https://api.monday.com/v2';

interface ColumnValues {
title: string;
text: string;
}

interface TaskItem {
id: number;
name: string;
column_values: ColumnValues[];
subitems?: TaskItem[];
}

interface BoardInfo {
id: number;
name: string;
items_count?: number;
items?: TaskItem[];
}

interface MondayResponse {
data: {
boards?: BoardInfo[];
items?: TaskItem[];
};
account_id: number;
}

interface PageInfo {
totalRows: number;
Expand All @@ -17,6 +56,77 @@ interface PublicViewResponse {
pageInfo: PageInfo;
}

/**
* fetch board info from monday
* @param {number} boardId - monday board's ID
*/
export async function fetchBoard(boardId: number): Promise<MondayResponse> {
const query = `query { boards (ids: ${boardId}) { id name items_count } }`;
const response = await fetch(MONDAY_API_URL, {
method: 'post',
headers: {
'Content-Type': 'application/json',
Authorization: process.env.MONDAY_API_TOKEN || '',
},
body: JSON.stringify({
query,
}),
});
return (await response.json()) as MondayResponse;
}

/**
* fetch board's task info
* @param {number} boardId - task item ID
* @param {number} page - items on board's page
*/
export async function fetchBoardTask(
boardId: number,
page: number
): Promise<MondayResponse> {
const query = `query { boards (ids: ${boardId}) { items (limit: ${LIMIT},page:${page}) { id name } } }`;
const response = await fetch(MONDAY_API_URL, {
method: 'post',
headers: {
'Content-Type': 'application/json',
Authorization: process.env.MONDAY_API_TOKEN || '',
},
body: JSON.stringify({
query,
}),
});
return (await response.json()) as MondayResponse;
}

/**
* fectch board task details
* @param {number} taskId - task item ID
* @returns
*/
export async function fetchTask(taskId: number) {
// todo: can `subitems` and `column_values` be optimize fetch to reduce API query cost?
const query = `query { items (ids: ${taskId}) { id name column_values { title text } subitems { id name column_values { title text } } } }`;
const response = await fetch(MONDAY_API_URL, {
method: 'post',
headers: {
'Content-Type': 'application/json',
Authorization: process.env.MONDAY_API_TOKEN || '',
},
body: JSON.stringify({
query,
}),
});
return (await response.json()) as MondayResponse;
}

// TODO: fetch board data from monday
export function fetchMondayData() {
// TODO: get board info
// TODO: get board's task items
// TODO: get task info
// TODO: get subtask items
}

export async function fetchNocoDB(resourcePath: string): Promise<Row[]> {
const apiPath = process.env.NOCODB_API_PATH;
const apiToken = process.env.NOCODB_API_TOKEN || '';
Expand Down
5 changes: 2 additions & 3 deletions scripts/monday-fetcher/extracts/party.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { fetchNocoDB } from './helpers';

export interface RawParty {
name: string;
side: string;
}

export async function getRawParties(): Promise<RawParty[]> {
return (await fetchNocoDB('/parties')) as RawParty[];
// TODO: get parties from DB or datasources?
return (await [{ name: 'ก้าวไกล', side: 'government' }]) as RawParty[];
}
17 changes: 1 addition & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4278,11 +4278,6 @@ consola@^2.15.0, consola@^2.15.3, consola@^2.6.0:
resolved "https://registry.yarnpkg.com/consola/-/consola-2.15.3.tgz#2e11f98d6a4be71ff72e0bdf07bd23e12cb61550"
integrity sha512-9vAdYbHj6x2fLKC4+oPH0kFzY/orMZyG2Aj+kNylHxKGJ/Ed4dpNyAQYwJOdqO4zdM7XpVHmyejQDcQHrnuXbw==

consola@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/consola/-/consola-3.1.0.tgz#dfdfa62ceb68bc1f06e4a76ad688566bd8813baf"
integrity sha512-rrrJE6rP0qzl/Srg+C9x/AE5Kxfux7reVm1Wh0wCjuXvih6DqZgqDZe8auTD28fzJ9TF0mHlSDrPpWlujQRo1Q==

console-browserify@^1.1.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/console-browserify/-/console-browserify-1.2.0.tgz#67063cef57ceb6cf4993a2ab3a55840ae8c49336"
Expand Down Expand Up @@ -8309,11 +8304,6 @@ minipass@^3.0.0, minipass@^3.1.1:
dependencies:
yallist "^4.0.0"

minipass@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/minipass/-/minipass-5.0.0.tgz#3e9788ffb90b694a5d0ec94479a45b5d8738133d"
integrity sha512-3FnjYuehv9k6ovOEbyOswadCDPX1piCfhV8ncmYtHOjuPwylVWsghTLo7rabjC3Rx5xD4HDx8Wm1xnMF7S5qFQ==

minizlib@^2.1.1:
version "2.1.2"
resolved "https://registry.yarnpkg.com/minizlib/-/minizlib-2.1.2.tgz#e90d3466ba209b932451508a11ce3d3632145931"
Expand Down Expand Up @@ -9053,7 +9043,7 @@ picocolors@^1.0.0:
resolved "https://registry.yarnpkg.com/picocolors/-/picocolors-1.0.0.tgz#cb5bdc74ff3f51892236eaf79d68bc44564ab81c"
integrity sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==

picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.2.3, picomatch@^2.3.1:
picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.2.3:
version "2.3.1"
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42"
integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==
Expand Down Expand Up @@ -11762,11 +11752,6 @@ ufo@^0.7.5:
resolved "https://registry.yarnpkg.com/ufo/-/ufo-0.7.11.tgz#17defad497981290383c5d26357773431fdbadcb"
integrity sha512-IT3q0lPvtkqQ8toHQN/BkOi4VIqoqheqM1FnkNWT9y0G8B3xJhwnoKBu5OHx8zHDOvveQzfKuFowJ0VSARiIDg==

ufo@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/ufo/-/ufo-1.1.2.tgz#d0d9e0fa09dece0c31ffd57bd363f030a35cfe76"
integrity sha512-TrY6DsjTQQgyS3E3dBaOXf0TpPD8u9FVrVYmKVegJuFw51n/YB9XPt+U6ydzFG5ZIN7+DIjPbNmXoBj9esYhgQ==

uglify-js@^3.5.1:
version "3.17.4"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.17.4.tgz#61678cf5fa3f5b7eb789bb345df29afb8257c22c"
Expand Down

0 comments on commit 7286a3b

Please sign in to comment.