Skip to content

Commit

Permalink
Add countries data init process
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Nov 1, 2023
1 parent f40557e commit b8e9828
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 75 deletions.
69 changes: 0 additions & 69 deletions data-import.sh

This file was deleted.

10 changes: 9 additions & 1 deletion functions/internals/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ export class Label {
static RefreshButton = "Refresh";
static Language = "Language";
static Country = "Country";
static Japan = "Japan";
static Work = "Work";
static BreakTime = "Break time";
static TimeOff = "Time off";
Expand Down Expand Up @@ -145,6 +144,10 @@ export class Label {
static hour = "hour";
static minute = "minute";

// Countries
static Japan = "Japan";
static UnitedStates = "United States";

// Report UI messages
static HereIsTheReportYouRequested =
"Here is the monthly report you requested!";
Expand Down Expand Up @@ -179,3 +182,8 @@ export class EntryType {
static BreakTime = "break_time";
static TimeOff = "time_off";
}

export class CountryCode {
static UnitedStates = "us";
static Japan = "jp";
}
79 changes: 79 additions & 0 deletions functions/internals/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Projects from "../../datastores/projects.ts";
import OrganizationPolicies from "../../datastores/organization_policies.ts";

import { todayYYYYMMDD } from "./datetime.ts";
import { CountryCode, Label } from "./constants.ts";

export type LogLevel = "DEBUG" | "INFO";

Expand Down Expand Up @@ -250,6 +251,84 @@ export async function fetchAllCountries(
return (await c.findAll()).items;
}

const DefaultCountries: Attributes<C>[] = [
{ id: CountryCode.Japan, label: Label.Japan },
{ id: CountryCode.UnitedStates, label: Label.UnitedStates },
];
interface setupCountriesAndHolidaysIfNecessaryArgs {
countries: SavedAttributes<C>[];
c: DataMapper<C>;
ph: DataMapper<PH>;
}
export async function setupCountriesAndHolidaysIfNecessary(
{ countries, c, ph }: setupCountriesAndHolidaysIfNecessaryArgs,
): Promise<SavedAttributes<C>[]> {
if (countries.length !== DefaultCountries.length) {
for (const attributes of DefaultCountries) {
await c.save({ attributes });
}
for (const attributes of DefaultPublicHolidays) {
await ph.save({ attributes });
}
return (await c.findAll()).items;
}
return countries;
}

// -----------------------------------------
// PublicHolidays
// -----------------------------------------

const DefaultPublicHolidays: Attributes<PH>[] = [
{
country_id_and_year: CountryCode.Japan + "-2023",
holidays: [
"20230101",
"20230102",
"20230109",
"20230211",
"20230223",
"20230321",
"20230429",
"20230503",
"20230504",
"20230505",
"20230717",
"20230811",
"20230918",
"20230923",
"20231009",
"20231103",
"20231123",
],
},
{
country_id_and_year: CountryCode.Japan + "-2024",
holidays: [
"20240101",
"20240108",
"20240211",
"20240212",
"20240223",
"20240320",
"20240429",
"20240503",
"20240504",
"20240505",
"20240506",
"20240715",
"20240811",
"20240812",
"20240916",
"20240922",
"20241014",
"20241103",
"20241104",
"20241123",
],
},
];

// -----------------------------------------
// Projects
// -----------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion functions/internals/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ labels[Label.Save] = { "ja": "保存する" };
labels[Label.RefreshButton] = { "ja": "更新" };
labels[Label.Language] = { "ja": "言語" };
labels[Label.Country] = { "ja": "国" };
labels[Label.Japan] = { "ja": "日本" };
labels[Label.Work] = { "ja": "勤務" };
labels[Label.BreakTime] = { "ja": "休憩" };
labels[Label.TimeOff] = { "ja": "休暇" };
Expand Down Expand Up @@ -51,6 +50,9 @@ labels[Label.minutes] = { "ja": "分" };
labels[Label.day] = { "ja": "日" };
labels[Label.hour] = { "ja": "時間" };
labels[Label.minute] = { "ja": "分" };
// Countries
labels[Label.Japan] = { "ja": "日本" };
labels[Label.Japan] = { "us": "アメリカ合衆国" };
// Report UI messages
labels[Label.HereIsTheReportYouRequested] = {
"ja": "こちらがご希望の月次レポートです!",
Expand Down
11 changes: 7 additions & 4 deletions functions/run_timesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import {
saveTimeEntry,
saveUserSettings,
serializeTimeEntry,
setupCountriesAndHolidaysIfNecessary,
TE,
US,
} from "./internals/datastore.ts";
Expand Down Expand Up @@ -117,11 +118,12 @@ export default SlackFunction(
if (isDebugMode) {
console.log(`### First time user (settings: ${p(settings)})`);
}
view = toUserSettingsView({
view,
countries: await fetchAllCountries({ ...components }),
let countries = await fetchAllCountries({ ...components });
countries = await setupCountriesAndHolidaysIfNecessary({
...components,
countries,
});
view = toUserSettingsView({ view, countries, ...components });
} else {
const item = await fetchTimeEntry({ ...components });
const manualEntryPermitted = await isManualEntryPermitted({
Expand Down Expand Up @@ -701,8 +703,9 @@ export default SlackFunction(
view: await toMainView({
view: newView(language),
item: entryForTheDay,
manualEntryPermitted,
...components,
manualEntryPermitted,
language: saved.language,
}),
};
}
Expand Down

0 comments on commit b8e9828

Please sign in to comment.