Skip to content

Instantly share code, notes, and snippets.

View jsjoeio's full-sized avatar

Joe Previte jsjoeio

View GitHub Profile
@jsjoeio
jsjoeio / index.ts
Created July 28, 2021 14:51
Example interacting with Supabase on server
type UserInGroup = Pick<DipUser, "timezone" | "telegramUsername" | "groupIds">
/**
* Gets the users from the DB using their groupId
*/
export async function getUsersInGroup(
telegramGroupId: number
): Promise<UserInGroup[] | undefined> {
const groupId = UPCOMING_GROUPS.find(
(group) => group.telegramGroupId === telegramGroupId
)?.id
@jsjoeio
jsjoeio / supabase.ts
Created July 28, 2021 14:50
Supbase helper function
import { createClient } from "@supabase/supabase-js"
export const supabaseClient = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL || "",
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || ""
)
export const supabaseServer = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL || "",
process.env.SUPABASE_SERVER_KEY || ""
@jsjoeio
jsjoeio / index.ts
Created July 21, 2021 15:22
Counting streaks based on updates
async function getStreaks() {
// Initialize google spreadsheet
const doc = await initializeGoogleSpreadsheet("updates")
const sheetTitle = "Test Group"
const sheet = doc.sheetsByTitle[sheetTitle]
// NOTE@jsjoeio - this is hardcoded to the test group
const groupId = -514638766
const groupStartDate = getGroupStartDate(groupId)
@jsjoeio
jsjoeio / index.ts
Created July 20, 2021 15:24
Vercel serverless function protected by secret to be used in cron job
import { NextApiRequest, NextApiResponse } from "next"
// ----------------------------------------------------------------------------
// To test this from the command line, run:
// curl --request POST \
// --url 'http://localhost:3000/api/<endpoint>' \
// --header 'Authorization: Bearer API_SECRET_KEY' \
export default async function handler(
req: NextApiRequest,
@jsjoeio
jsjoeio / index.ts
Last active July 12, 2021 14:59
Timezone related functions
import {
fromUnixTime,
format,
} from "date-fns"
/**
* Checks if a timezone string is valid or not
*
* We do this to catch typos.
*
@jsjoeio
jsjoeio / index.ts
Created July 4, 2021 18:37
TypeScript - Pick utility-type real-world example
export type User = {
/** Unix timestamp - created at time of write */
timestamp: number
/** ID of the user - created at time of write */
id: string
telegramUsername: string
telegramUserId: string
/** Like a nickname */
preferredName: string
firstName: string
@jsjoeio
jsjoeio / macro.gs
Created July 2, 2021 23:54
Google Spreadsheet macro example
/** @OnlyCurrentDoc */
const startDate = new Date("2021-06-16")
// so the first one is D2:K2
function MarkEmptyCellsInYesterdayRowWithHyphen() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Group 2 - B");
// Activates the sheet
SpreadsheetApp.setActiveSheet(sheet);
const today = new Date()
@jsjoeio
jsjoeio / index.ts
Created July 2, 2021 14:25
opaque type example (DateInTimezone)
declare const _date: unique symbol
export type DateInTimezone = Date & { readonly [_date]: Date }
/**
* Checks if the date is between 12am and 4am
* of the day
*
* Returns true if it is, false otherwise
**/
export function isBetween12amAnd4am(date: DateInTimezone) {
@jsjoeio
jsjoeio / example.js
Created May 23, 2021 14:58
Google Spreadsheet Node.js Example
const { GoogleSpreadsheet } = require('google-spreadsheet')
;(async function () {
// Initialize the sheet - doc ID is the long id in the sheets URL
const doc = new GoogleSpreadsheet('<the sheet ID from the url>')
// Initialize Auth - see more available options at https://theoephraim.github.io/node-google-spreadsheet/#/getting-started/authentication
await doc.useServiceAccountAuth({
client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
private_key: process.env.GOOGLE_PRIVATE_KEY,
})
@jsjoeio
jsjoeio / script.sh
Last active May 19, 2021 18:31
Create Draft PR from CLI and fill out with flags
#!/bin/sh
gh pr create --title "title here" --draft --assignee "@me" --milestone "3.11.0" --label "security"