Skip to content

Instantly share code, notes, and snippets.

View tyteen4a03's full-sized avatar

Timothy Choi tyteen4a03

View GitHub Profile
@tyteen4a03
tyteen4a03 / combFields.ts
Last active January 24, 2025 00:31
Coerce Postgres columns schema types by @ShilohFox (License: MIT)
import { bigint, integer, serial, smallint } from "drizzle-orm/pg-core";
import type { Field } from "payload";
import toSnakeCase from "to-snake-case";
/*
* Iterates through the list of fields and prepares changes using Payload's Drizzle exposure.
* The fields marked with `numberType: "smallint" | "integer" | "bigint"` in their `custom`
* field will use their respective column type in the schema, so postgres can coerce them.
*
* fields: The fields to comb through recursively.
@tyteen4a03
tyteen4a03 / cron-server.ts
Created November 1, 2024 17:11
Payload 3.0 Cron Runner
import { createServer } from "node:http";
import { parse } from "node:url";
import { initGraphClient } from "@/modules/msgraph/client";
import { rosterfyImportWorkflowId } from "@/modules/volunteer/workflows/rosterfyImportWorkflow";
import config from "@payload-config";
import next from "next";
import cron from "node-cron";
import { getPayload } from "payload";
const port = Number.parseInt(process.env.PORT || "3010");
@tyteen4a03
tyteen4a03 / JobStopsListenerField.tsx
Last active January 3, 2025 16:31
PayloadCMS Listener Field Pattern (v3)
import JobStopsListenerFieldEffect from "@/modules/transport/components/JobStopsListenerField/JobStopsListenerFieldEffect";
import type { UIFieldServerComponent } from "payload";
const JobStopsListenerField: UIFieldServerComponent = async ({ payload }) => {
const warehouse =
(
await payload.find({
collection: "addresses",
where: {
type: {
@tyteen4a03
tyteen4a03 / generate-password.js
Created December 5, 2023 12:20
Generate Microsoft-friendly random passwords
const VOWELS = 'aeiou';
const CONSONANTS = 'bcdfghjklmnpqrstvwxyz';
const generateInitialPassword = () => {
const vowel = VOWELS[Math.floor(Math.random() * VOWELS.length)]
const consonant = CONSONANTS[Math.floor(Math.random() * CONSONANTS.length)]
const randomNumber = Math.floor(Math.random() * (999999 - 100000 + 1)) + 100000;
return `${consonant.toLocaleUpperCase()}${vowel}${consonant}${vowel}${randomNumber}`;
}
@tyteen4a03
tyteen4a03 / PayloadFormExample.tsx
Created October 30, 2023 15:47
Payload Form Example
import { Gutter } from "payload/components/elements";
import { Form, FormSubmit, RenderFields, fieldTypes } from "payload/components/forms";
import { DefaultTemplate } from "payload/components/templates";
import { AdminViewComponent } from "payload/config";
import { useStepNav } from "payload/dist/admin/components/elements/StepNav";
import { Field } from "payload/types";
import React, { useEffect } from "react";
import { useTranslation } from "react-i18next";
const PayloadFormExample: AdminViewComponent = () => {
@tyteen4a03
tyteen4a03 / field-example.ts
Created October 30, 2023 12:37
PayloadCMS ColoredSelectCell
{
name: "jobStatus",
type: "select",
required: true,
defaultValue: "toBeAssigned",
options: [
{
label: "To Be Assigned",
value: "toBeAssigned",
},
job "mongodb" {
datacenters = ["prod-azure"]
type = "service"
constraint {
distinct_hosts = true
}
update {
max_parallel = 1
@tyteen4a03
tyteen4a03 / gist:195e09bc2c0e4169ff5d7a783f17ba14
Created April 4, 2020 13:04
code2pdf output HTML instead
require 'cgi'
require 'shellwords'
class ConvertToPDF
PDF_OPTIONS = {
page_size: 'A4'
}.freeze
def initialize(params = {})
if !params.key?(:from) || params[:from].nil?
@tyteen4a03
tyteen4a03 / delete_all_contact.py
Last active November 30, 2019 22:25
Freshdesk delete all contacts
import requests
api_key = ('API-KEY-HERE', 'X')
domain = 'DOMAIN-HERE'
contact_ids = []
page = 1
while True:
print("Downloading page {} of deleted contacts".format(page))
results = requests.get("https://{}.freshdesk.com/api/v2/contacts?page={}&per_page=100&state=deleted".format(domain, page), auth=api_key).json()
@tyteen4a03
tyteen4a03 / convertDates.py
Created July 21, 2017 11:33
a quick and dirty way to convert US dates to British dates generated by Facebook Page Insights
import csv
import re
with open("test.csv", "r+") as f:
with open("testoutput.csv", "w+") as g:
reader = csv.DictReader(f)
writer = csv.DictWriter(g, reader.fieldnames)
for row in reader:
date = row["Posted"]
m = re.match(r"^(\d\d)/(\d\d)(.*)$", date)