From 453327982da05826648a33c2fc21f939607192cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Rafael=20Siqueira?= Date: Thu, 18 Dec 2025 17:12:23 -0300 Subject: [PATCH] add search filter --- .../_.$orgid.billing._index/range-period.tsx | 21 ++- .../routes/_.$orgid.billing._index/route.tsx | 57 +++--- .../async-combobox.tsx | 171 ------------------ .../routes/_.$orgid.enrollments.add/route.tsx | 117 +++++++++++- .../routes/_app.enrollments._index/route.tsx | 129 ++++++++++++- layercake/pyproject.toml | 3 +- layercake/uv.lock | 87 +++++++++ packages/ui/src/components/datetime.tsx | 22 +-- .../src/components/range-calendar-filter.tsx | 8 +- packages/ui/src/components/search-filter.tsx | 137 ++++++++++++++ users-events/app/config.py | 3 + users-events/app/events/set_custom_domain.py | 49 +++++ users-events/template.yaml | 21 +++ .../tests/events/test_set_custom_domain.py | 22 +++ users-events/uv.lock | 88 ++++++++- 15 files changed, 688 insertions(+), 247 deletions(-) delete mode 100644 apps/admin.saladeaula.digital/app/routes/_.$orgid.enrollments.add/async-combobox.tsx create mode 100644 packages/ui/src/components/search-filter.tsx create mode 100644 users-events/app/events/set_custom_domain.py create mode 100644 users-events/tests/events/test_set_custom_domain.py diff --git a/apps/admin.saladeaula.digital/app/routes/_.$orgid.billing._index/range-period.tsx b/apps/admin.saladeaula.digital/app/routes/_.$orgid.billing._index/range-period.tsx index 37ab071..442ff0a 100644 --- a/apps/admin.saladeaula.digital/app/routes/_.$orgid.billing._index/range-period.tsx +++ b/apps/admin.saladeaula.digital/app/routes/_.$orgid.billing._index/range-period.tsx @@ -1,11 +1,12 @@ import { useSearchParams } from 'react-router' import { ChevronRightIcon, ChevronLeftIcon } from 'lucide-react' import { subMonths, addMonths } from 'date-fns' -import { DateTime } from 'luxon' +import { DateTime as LuxonDateTime } from 'luxon' import { Button } from '@repo/ui/components/ui/button' import { ButtonGroup } from '@repo/ui/components/ui/button-group' import { Badge } from '@repo/ui/components/ui/badge' +import { DateTime } from '@repo/ui/components/datetime' import { formatDate, billingPeriod } from './util' import { tz } from './data' @@ -16,12 +17,18 @@ type RangePeriodProps = { billingDay: number } +const dtOptions: Intl.DateTimeFormatOptions = { + year: '2-digit', + hour: undefined, + minute: undefined +} + export function RangePeriod({ startDate, endDate, billingDay }: RangePeriodProps) { - const today = DateTime.now().setZone(tz).toJSDate() + const today = LuxonDateTime.now().setZone(tz).toJSDate() const [, setSearchParams] = useSearchParams() const prevPeriod = billingPeriod(billingDay, subMonths(startDate, 1)) const nextPeriod = billingPeriod(billingDay, addMonths(startDate, 1)) @@ -51,10 +58,10 @@ export function RangePeriod({ >
- {datetime.format(startDate)} + {startDate} - {datetime.format(endDate)} + {endDate}
@@ -78,9 +85,3 @@ export function RangePeriod({ ) } - -const datetime = new Intl.DateTimeFormat('pt-BR', { - day: '2-digit', - month: '2-digit', - year: '2-digit' -}) diff --git a/apps/admin.saladeaula.digital/app/routes/_.$orgid.billing._index/route.tsx b/apps/admin.saladeaula.digital/app/routes/_.$orgid.billing._index/route.tsx index a74bea1..e28d52c 100644 --- a/apps/admin.saladeaula.digital/app/routes/_.$orgid.billing._index/route.tsx +++ b/apps/admin.saladeaula.digital/app/routes/_.$orgid.billing._index/route.tsx @@ -1,7 +1,7 @@ import type { Route } from './+types/route' import Fuse from 'fuse.js' -import { DateTime } from 'luxon' +import { DateTime as LuxonDateTime } from 'luxon' import { Suspense, useMemo } from 'react' import { BanIcon } from 'lucide-react' import { Await, useSearchParams } from 'react-router' @@ -30,6 +30,8 @@ import { EmptyTitle } from '@repo/ui/components/ui/empty' import { Kbd } from '@repo/ui/components/ui/kbd' +import { Currency } from '@repo/ui/components/currency' +import { DateTime } from '@repo/ui/components/datetime' import { billingPeriod, formatDate } from './util' import { RangePeriod } from './range-period' @@ -45,11 +47,15 @@ export async function loader({ context, request, params }: Route.LoaderArgs) { url: `/orgs/${params.orgid}/subscription`, context, request - }).then((r) => r.json()) + }) + + const { billing_day = 1 } = (await subscription.json()) as { + billing_day: number + } const [startDate, endDate] = billingPeriod( - subscription?.billing_day || 1, - DateTime.now().setZone(tz).toJSDate() + billing_day, + LuxonDateTime.now().setZone(tz).toJSDate() ) const start = searchParams.get('start') || formatDate(startDate) const end = searchParams.get('end') || formatDate(endDate) @@ -61,15 +67,15 @@ export async function loader({ context, request, params }: Route.LoaderArgs) { }).then((r) => r.json()) return { - subscription, + billing_day, billing, - startDate: DateTime.fromISO(start, { zone: tz }).toJSDate(), - endDate: DateTime.fromISO(end, { zone: tz }).toJSDate() + startDate: LuxonDateTime.fromISO(start, { zone: tz }).toJSDate(), + endDate: LuxonDateTime.fromISO(end, { zone: tz }).toJSDate() } } export default function Route({ - loaderData: { subscription, billing, startDate, endDate } + loaderData: { billing_day, billing, startDate, endDate } }: Route.ComponentProps) { const [searchParams, setSearchParams] = useSearchParams() const search = searchParams.get('s') as string @@ -120,7 +126,7 @@ export default function Route({ - - )} - - {loading && ( - - - - )} - - - - - - - - {searched ? ( - - Nenhum resultado encontrado. - - - {data.map(({ id, name, email, cpf }) => ( - { - onChange?.({ id, name, email, cpf }) - set(false) - }} - > -
- - - {initials(name)} - - - -
    -
  • - {name} -
  • -
  • - {email} -
  • - - {cpf ? ( -
  • - {formatCPF(cpf)} -
  • - ) : ( -
  • - - Inelegível -
  • - )} -
-
- - -
- ))} -
-
- ) : null} -
-
- - ) -} diff --git a/apps/admin.saladeaula.digital/app/routes/_.$orgid.enrollments.add/route.tsx b/apps/admin.saladeaula.digital/app/routes/_.$orgid.enrollments.add/route.tsx index eb28d62..0f0977a 100644 --- a/apps/admin.saladeaula.digital/app/routes/_.$orgid.enrollments.add/route.tsx +++ b/apps/admin.saladeaula.digital/app/routes/_.$orgid.enrollments.add/route.tsx @@ -13,7 +13,9 @@ import { CheckIcon, BookIcon, ArrowDownAZIcon, - ArrowDownZAIcon + ArrowDownZAIcon, + AlertTriangleIcon, + UserIcon } from 'lucide-react' import { Link, useParams, useFetcher } from 'react-router' import { Controller, useFieldArray, useForm } from 'react-hook-form' @@ -22,7 +24,10 @@ import { format } from 'date-fns' import { ptBR } from 'react-day-picker/locale' import { zodResolver } from '@hookform/resolvers/zod' import Fuse from 'fuse.js' +import { formatCPF } from '@brazilian-utils/brazilian-utils' +import { Avatar, AvatarFallback } from '@repo/ui/components/ui/avatar' +import { Abbr } from '@repo/ui/components/abbr' import { Command, CommandEmpty, @@ -63,13 +68,13 @@ import { import { Label } from '@repo/ui/components/ui/label' import { Calendar } from '@repo/ui/components/ui/calendar' import { createSearch } from '@repo/util/meili' -import { cn } from '@repo/ui/lib/utils' +import { initials, cn } from '@repo/ui/lib/utils' import { HttpMethod, request as req } from '@repo/util/request' import { useIsMobile } from '@repo/ui/hooks/use-mobile' import { cloudflareContext } from '@repo/auth/context' +import { SearchFilter } from '@repo/ui/components/search-filter' import { formSchema, type Schema, MAX_ITEMS } from './data' -import { AsyncCombobox } from './async-combobox' import { redirect } from 'react-router' export function meta({}: Route.MetaArgs) { @@ -213,13 +218,109 @@ export default function Route({ fieldState }) => (
- + render={({ + id, + name, + email, + cpf, + onSelect, + onClose + }) => ( + { + onSelect() + onClose() + }} + > +
+ + + {initials(name)} + + + +
    +
  • + {name} +
  • +
  • + {email} +
  • + + {cpf ? ( +
  • + {formatCPF(cpf)} +
  • + ) : ( +
  • + + Inelegível +
  • + )} +
+
+ + +
+ )} + > + {({ loading }) => ( + + + + + + + {value && ( + + + + )} + + {loading && ( + + + + )} + + )} + + ([]) const status = searchParams.get('status') - const rangeParams = useRangeParams() + const dateRange = useRangeParams() + const org = searchParams.has('org') + ? JSON.parse(searchParams.get('org') || '') + : null + + const onSearch = async (search: string) => { + const params = new URLSearchParams({ q: search }) + const r = await fetch(`/orgs.json?${params.toString()}`) + const { hits } = (await r.json()) as { hits: any[] } + return hits + } return ( }> @@ -78,7 +111,7 @@ export default function Route({ loaderData: { data } }: Route.ComponentProps) {

Matrículas

- + {({ hits, page = 1, hitsPerPage, totalHits }) => ( ({ @@ -190,6 +223,88 @@ export default function Route({ loaderData: { data } }: Route.ComponentProps) { }) }} /> + + { + setSearchParams((searchParams) => { + if (value) { + searchParams.set( + 'org', + JSON.stringify(pick(['id', 'name', 'q'], value)) + ) + } else { + searchParams.delete('org') + } + + return searchParams + }) + }} + render={({ id, name, cnpj, onSelect }) => ( + +
+ + + {initials(name)} + + + +
    +
  • + {name} +
  • +
  • + {formatCNPJ(cnpj)} +
  • +
+
+ + +
+ )} + > + {({ loading }) => ( + + )} +
diff --git a/layercake/pyproject.toml b/layercake/pyproject.toml index 343ef09..555e605 100644 --- a/layercake/pyproject.toml +++ b/layercake/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "layercake" -version = "0.11.2" +version = "0.11.3" description = "Packages shared dependencies to optimize deployment and ensure consistency across functions." readme = "README.md" authors = [ @@ -29,6 +29,7 @@ dependencies = [ "python-multipart>=0.0.20", "authlib>=1.6.5", "python-calamine>=0.5.4", + "cloudflare>=4.3.1", ] [dependency-groups] diff --git a/layercake/uv.lock b/layercake/uv.lock index e6bd1e7..c4371af 100644 --- a/layercake/uv.lock +++ b/layercake/uv.lock @@ -20,6 +20,19 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/89/03/a851e84fcbb85214dc637b6378121ef9a0dd61b4c65264675d8a5c9b1ae7/antlr4_python3_runtime-4.13.2-py3-none-any.whl", hash = "sha256:fe3835eb8d33daece0e799090eda89719dbccee7aa39ef94eed3818cafa5a7e8", size = 144462, upload-time = "2024-08-03T19:00:11.134Z" }, ] +[[package]] +name = "anyio" +version = "4.12.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "idna" }, + { name = "typing-extensions", marker = "python_full_version < '3.13'" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/16/ce/8a777047513153587e5434fd752e89334ac33e379aa3497db860eeb60377/anyio-4.12.0.tar.gz", hash = "sha256:73c693b567b0c55130c104d0b43a9baf3aa6a31fc6110116509f27bf75e21ec0", size = 228266, upload-time = "2025-11-28T23:37:38.911Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/7f/9c/36c5c37947ebfb8c7f22e0eb6e4d188ee2d53aa3880f3f2744fb894f0cb1/anyio-4.12.0-py3-none-any.whl", hash = "sha256:dad2376a628f98eeca4881fc56cd06affd18f659b17a747d3ff0307ced94b1bb", size = 113362, upload-time = "2025-11-28T23:36:57.897Z" }, +] + [[package]] name = "arnparse" version = "0.0.2" @@ -368,6 +381,23 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/2c/1a/aff8bb287a4b1400f69e09a53bd65de96aa5cee5691925b38731c67fc695/click_default_group-1.2.4-py2.py3-none-any.whl", hash = "sha256:9b60486923720e7fc61731bdb32b617039aba820e22e1c88766b1125592eaa5f", size = 4123, upload-time = "2023-08-04T07:54:56.875Z" }, ] +[[package]] +name = "cloudflare" +version = "4.3.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "anyio" }, + { name = "distro" }, + { name = "httpx" }, + { name = "pydantic" }, + { name = "sniffio" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/5d/48/e481c0a9b9010a5c41b5ca78ff9fbe00dc8a9a4d39da5af610a4ec49c7f7/cloudflare-4.3.1.tar.gz", hash = "sha256:b1e1c6beeb8d98f63bfe0a1cba874fc4e22e000bcc490544f956c689b3b5b258", size = 1933187, upload-time = "2025-06-16T21:43:18.716Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/3a/8f/c6c543565efd3144da4304efa5917aac06b6416a8663a6defe0e9b2b7569/cloudflare-4.3.1-py3-none-any.whl", hash = "sha256:6927135a5ee5633d6e2e1952ca0484745e933727aeeb189996d2ad9d292071c6", size = 4406465, upload-time = "2025-06-16T21:43:17.3Z" }, +] + [[package]] name = "colorama" version = "0.4.6" @@ -516,6 +546,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/47/ef/4cb333825d10317a36a1154341ba37e6e9c087bac99c1990ef07ffdb376f/dictdiffer-0.9.0-py2.py3-none-any.whl", hash = "sha256:442bfc693cfcadaf46674575d2eba1c53b42f5e404218ca2c2ff549f2df56595", size = 16754, upload-time = "2021-07-22T13:24:26.783Z" }, ] +[[package]] +name = "distro" +version = "1.9.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/fc/f8/98eea607f65de6527f8a2e8885fc8015d3e6f5775df186e443e0964a11c3/distro-1.9.0.tar.gz", hash = "sha256:2fa77c6fd8940f116ee1d6b94a2f90b13b5ea8d019b98bc8bafdcabcdd9bdbed", size = 60722, upload-time = "2023-12-24T09:54:32.31Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/12/b3/231ffd4ab1fc9d679809f356cebee130ac7daa00d6d6f3206dd4fd137e9e/distro-1.9.0-py3-none-any.whl", hash = "sha256:7bffd925d65168f85027d8da9af6bddab658135b840670a223589bc0c8ef02b2", size = 20277, upload-time = "2023-12-24T09:54:30.421Z" }, +] + [[package]] name = "dnspython" version = "2.8.0" @@ -608,6 +647,43 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/0a/14/933037032608787fb92e365883ad6a741c235e0ff992865ec5d904a38f1e/graphql_core-3.2.7-py3-none-any.whl", hash = "sha256:17fc8f3ca4a42913d8e24d9ac9f08deddf0a0b2483076575757f6c412ead2ec0", size = 207262, upload-time = "2025-11-01T22:30:38.912Z" }, ] +[[package]] +name = "h11" +version = "0.16.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/01/ee/02a2c011bdab74c6fb3c75474d40b3052059d95df7e73351460c8588d963/h11-0.16.0.tar.gz", hash = "sha256:4e35b956cf45792e4caa5885e69fba00bdbc6ffafbfa020300e549b208ee5ff1", size = 101250, upload-time = "2025-04-24T03:35:25.427Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/04/4b/29cac41a4d98d144bf5f6d33995617b185d14b22401f75ca86f384e87ff1/h11-0.16.0-py3-none-any.whl", hash = "sha256:63cf8bbe7522de3bf65932fda1d9c2772064ffb3dae62d55932da54b31cb6c86", size = 37515, upload-time = "2025-04-24T03:35:24.344Z" }, +] + +[[package]] +name = "httpcore" +version = "1.0.9" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "certifi" }, + { name = "h11" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/06/94/82699a10bca87a5556c9c59b5963f2d039dbd239f25bc2a63907a05a14cb/httpcore-1.0.9.tar.gz", hash = "sha256:6e34463af53fd2ab5d807f399a9b45ea31c3dfa2276f15a2c3f00afff6e176e8", size = 85484, upload-time = "2025-04-24T22:06:22.219Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/7e/f5/f66802a942d491edb555dd61e3a9961140fd64c90bce1eafd741609d334d/httpcore-1.0.9-py3-none-any.whl", hash = "sha256:2d400746a40668fc9dec9810239072b40b4484b640a8c38fd654a024c7a1bf55", size = 78784, upload-time = "2025-04-24T22:06:20.566Z" }, +] + +[[package]] +name = "httpx" +version = "0.28.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "anyio" }, + { name = "certifi" }, + { name = "httpcore" }, + { name = "idna" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/b1/df/48c586a5fe32a0f01324ee087459e112ebb7224f646c0b5023f5e79e9956/httpx-0.28.1.tar.gz", hash = "sha256:75e98c5f16b0f35b567856f597f06ff2270a374470a5c2392242528e3e3e42fc", size = 141406, upload-time = "2024-12-06T15:37:23.222Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/2a/39/e50c7c3a983047577ee07d2a9e53faf5a69493943ec3f6a384bdc792deb2/httpx-0.28.1-py3-none-any.whl", hash = "sha256:d909fcccc110f8c7faf814ca82a9a4d816bc5a6dbfea25d6591d6985b8ba59ad", size = 73517, upload-time = "2024-12-06T15:37:21.509Z" }, +] + [[package]] name = "idna" version = "3.11" @@ -754,6 +830,7 @@ dependencies = [ { name = "arnparse" }, { name = "authlib" }, { name = "aws-lambda-powertools", extra = ["all"] }, + { name = "cloudflare" }, { name = "dictdiffer" }, { name = "ftfy" }, { name = "glom" }, @@ -790,6 +867,7 @@ requires-dist = [ { name = "arnparse", specifier = ">=0.0.2" }, { name = "authlib", specifier = ">=1.6.5" }, { name = "aws-lambda-powertools", extras = ["all"], specifier = ">=3.23.0" }, + { name = "cloudflare", specifier = ">=4.3.1" }, { name = "dictdiffer", specifier = ">=0.9.0" }, { name = "ftfy", specifier = ">=6.3.1" }, { name = "glom", specifier = ">=24.11.0" }, @@ -1902,6 +1980,15 @@ s3 = [ { name = "boto3" }, ] +[[package]] +name = "sniffio" +version = "1.3.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/a2/87/a6771e1546d97e7e041b6ae58d80074f81b7d5121207425c964ddf5cfdbd/sniffio-1.3.1.tar.gz", hash = "sha256:f4324edc670a0f49750a81b895f35c3adb843cca46f0530f79fc1babb23789dc", size = 20372, upload-time = "2024-02-25T23:20:04.057Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/e9/44/75a9c9421471a6c4805dbf2356f7c181a29c1879239abab1ea2cc8f38b40/sniffio-1.3.1-py3-none-any.whl", hash = "sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2", size = 10235, upload-time = "2024-02-25T23:20:01.196Z" }, +] + [[package]] name = "sqlite-fts4" version = "1.0.3" diff --git a/packages/ui/src/components/datetime.tsx b/packages/ui/src/components/datetime.tsx index df00b11..d4b7929 100644 --- a/packages/ui/src/components/datetime.tsx +++ b/packages/ui/src/components/datetime.tsx @@ -1,33 +1,25 @@ -import type { ComponentPropsWithoutRef } from 'react' - type DateTimeProps = { - children: string + children: string | Date options?: Intl.DateTimeFormatOptions locale?: string -} & ComponentPropsWithoutRef<'time'> +} export function DateTime({ children, options, - locale = 'pt-BR', - ...props -}: DateTimeProps) { + locale = 'pt-BR' +}: DateTimeProps): string { const optionsInit: Intl.DateTimeFormatOptions = { day: '2-digit', month: '2-digit', - year: 'numeric', - hour: '2-digit', - minute: '2-digit' + year: 'numeric' } + const date = children instanceof Date ? children : new Date(children) const datetime = new Intl.DateTimeFormat(locale, { ...optionsInit, ...options }) - return ( - - ) + return datetime.format(date) } diff --git a/packages/ui/src/components/range-calendar-filter.tsx b/packages/ui/src/components/range-calendar-filter.tsx index 5c67686..fb8f46b 100644 --- a/packages/ui/src/components/range-calendar-filter.tsx +++ b/packages/ui/src/components/range-calendar-filter.tsx @@ -75,7 +75,7 @@ export function RangeCalendarFilter({ - {dateRange && ( + {dateRange ? ( <> @@ -128,7 +128,7 @@ export function RangeCalendarFilter({ - )} + ) : null}
diff --git a/packages/ui/src/components/search-filter.tsx b/packages/ui/src/components/search-filter.tsx new file mode 100644 index 0000000..4367d3c --- /dev/null +++ b/packages/ui/src/components/search-filter.tsx @@ -0,0 +1,137 @@ +import { + ReactElement, + ComponentProps, + ReactNode, + useState, + useRef +} from 'react' +import { useBoolean, useRequest, useToggle } from 'ahooks' + +import { cn } from '../lib/utils' +import { Popover, PopoverContent, PopoverTrigger } from './ui/popover' +import { + Command, + CommandEmpty, + CommandGroup, + CommandInput, + CommandList, + CommandItem +} from './ui/command' +import { Button } from './ui/button' +import { Separator } from './ui/separator' + +interface SearchFilterProps { + defaultValue?: string + onValueChange?: (value: string) => void + requestOptions?: object + placeholder?: string + onChange: (value: T | undefined) => void + onSearch: (search: string) => Promise + align: 'start' | 'center' | 'end' + children: ({ loading }: { loading: boolean }) => ReactNode + render: ( + item: T & { + onSelect: () => void + onClose: () => void + } + ) => ReactElement> +} + +export function SearchFilter({ + defaultValue, + onValueChange, + placeholder, + align, + onSearch, + onChange, + children, + render, + requestOptions = {} +}: SearchFilterProps) { + const inputRef = useRef(null) + const [open, { toggle, set }] = useToggle() + const [value, setValue] = useState(defaultValue) + const [searched, { setTrue, setFalse }] = useBoolean() + const { + data = [], + loading, + runAsync, + mutate + } = useRequest(onSearch, { + manual: true, + debounceWait: 300, + defaultParams: [''], + onSuccess: setTrue, + ...requestOptions + }) + + return ( + + {children({ loading })} + + + + { + onValueChange?.(value) + setValue(value) + await runAsync(value) + }} + /> + + {searched ? ( + + Nenhum resultado encontrado. + + + {data.map((item) => { + return render({ + ...item, + onClose: () => set(false), + onSelect: () => { + onChange?.({ ...item, q: value }) + } + }) + })} + + + ) : null} + + + {data.length ? ( + <> + +
+ +
+ + ) : null} +
+
+ ) +} diff --git a/users-events/app/config.py b/users-events/app/config.py index ea2696c..00f586c 100644 --- a/users-events/app/config.py +++ b/users-events/app/config.py @@ -4,3 +4,6 @@ USER_TABLE: str = os.getenv('USER_TABLE') # type: ignore CHUNK_SIZE = 50 EMAIL_SENDER = ('EDUSEG®', 'noreply@eduseg.com.br') + +CLOUDFLARE_ACCOUNT_ID = '5436b62470020c04b434ad31c3e4cf4e' +CLOUDFLARE_ZONE_ID = '95c5be1f8ac60e7fd243e6dda987d758' diff --git a/users-events/app/events/set_custom_domain.py b/users-events/app/events/set_custom_domain.py new file mode 100644 index 0000000..f8e852d --- /dev/null +++ b/users-events/app/events/set_custom_domain.py @@ -0,0 +1,49 @@ +from aws_lambda_powertools.utilities.data_classes import ( + EventBridgeEvent, + event_source, +) +from aws_lambda_powertools.utilities.typing import LambdaContext +from cloudflare import Cloudflare +from layercake.dateutils import now +from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair + +from boto3clients import dynamodb_client +from config import CLOUDFLARE_ZONE_ID, USER_TABLE + +dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client) +cf_client = Cloudflare() + + +@event_source(data_class=EventBridgeEvent) +def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool: + new_image = event.detail['new_image'] + + try: + custom_hostname = cf_client.custom_hostnames.create( + zone_id=CLOUDFLARE_ZONE_ID, + hostname=new_image['hostname'], + ssl={ + 'type': 'dv', + 'method': 'http', + 'settings': { + 'tls_1_3': 'on', + 'min_tls_version': '1.2', + }, + }, + ) + + dyn.update_item( + key=KeyPair( + pk=new_image['id'], + sk=new_image['sk'], + ), + update_expr='SET hostname_id = :hostname_id, updated_at = :now', + expr_attr_values={ + ':hostname_id': custom_hostname.id, # type: ignore + ':now': now(), + }, + ) + except Exception: + pass + + return True diff --git a/users-events/template.yaml b/users-events/template.yaml index 9581058..38a1c9e 100644 --- a/users-events/template.yaml +++ b/users-events/template.yaml @@ -26,6 +26,7 @@ Globals: POWERTOOLS_LOGGER_SAMPLE_RATE: 0.1 POWERTOOLS_LOGGER_LOG_EVENT: true USER_TABLE: !Ref UserTable + CLOUDFLARE_API_TOKEN: '{{resolve:ssm:/saladeaula/cloudflare_api_token}}' Resources: EventLog: @@ -265,3 +266,23 @@ Resources: new_image: sk: ['0'] changes: [name, email, cpf] + + EventSetCustomDomainFunction: + Type: AWS::Serverless::Function + Properties: + Handler: events.set_custom_domain.lambda_handler + LoggingConfig: + LogGroup: !Ref EventLog + Policies: + - DynamoDBCrudPolicy: + TableName: !Ref UserTable + Events: + DynamoDBEvent: + Type: EventBridgeRule + Properties: + Pattern: + resources: [!Ref UserTable] + detail-type: [INSERT] + detail: + new_image: + sk: ['CUSTOM_DOMAIN'] diff --git a/users-events/tests/events/test_set_custom_domain.py b/users-events/tests/events/test_set_custom_domain.py new file mode 100644 index 0000000..a9887b7 --- /dev/null +++ b/users-events/tests/events/test_set_custom_domain.py @@ -0,0 +1,22 @@ +from aws_lambda_powertools.utilities.typing.lambda_context import LambdaContext +from layercake.dynamodb import DynamoDBPersistenceLayer + +import events.set_custom_domain as app + + +def test_set_custom_domain( + seeds, + dynamodb_persistence_layer: DynamoDBPersistenceLayer, + lambda_context: LambdaContext, +): + event = { + 'detail': { + 'new_image': { + 'id': '5OxmMjL-ujoR5IMGegQz', + 'sk': 'CUSTOM_DOMAIN', + 'hostname': 'app.eduseg.com.br', + }, + }, + } + + assert app.lambda_handler(event, lambda_context) # type: ignore diff --git a/users-events/uv.lock b/users-events/uv.lock index 80341eb..45039e1 100644 --- a/users-events/uv.lock +++ b/users-events/uv.lock @@ -11,6 +11,18 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/78/b6/6307fbef88d9b5ee7421e68d78a9f162e0da4900bc5f5793f6d3d0e34fb8/annotated_types-0.7.0-py3-none-any.whl", hash = "sha256:1f02e8b43a8fbbc3f3e0d4f0f4bfc8131bcb4eebe8849b8e5c773f3a1c582a53", size = 13643, upload-time = "2024-05-20T21:33:24.1Z" }, ] +[[package]] +name = "anyio" +version = "4.12.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "idna" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/16/ce/8a777047513153587e5434fd752e89334ac33e379aa3497db860eeb60377/anyio-4.12.0.tar.gz", hash = "sha256:73c693b567b0c55130c104d0b43a9baf3aa6a31fc6110116509f27bf75e21ec0", size = 228266, upload-time = "2025-11-28T23:37:38.911Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/7f/9c/36c5c37947ebfb8c7f22e0eb6e4d188ee2d53aa3880f3f2744fb894f0cb1/anyio-4.12.0-py3-none-any.whl", hash = "sha256:dad2376a628f98eeca4881fc56cd06affd18f659b17a747d3ff0307ced94b1bb", size = 113362, upload-time = "2025-11-28T23:36:57.897Z" }, +] + [[package]] name = "arnparse" version = "0.0.2" @@ -259,6 +271,23 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/2c/1a/aff8bb287a4b1400f69e09a53bd65de96aa5cee5691925b38731c67fc695/click_default_group-1.2.4-py2.py3-none-any.whl", hash = "sha256:9b60486923720e7fc61731bdb32b617039aba820e22e1c88766b1125592eaa5f", size = 4123, upload-time = "2023-08-04T07:54:56.875Z" }, ] +[[package]] +name = "cloudflare" +version = "4.3.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "anyio" }, + { name = "distro" }, + { name = "httpx" }, + { name = "pydantic" }, + { name = "sniffio" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/5d/48/e481c0a9b9010a5c41b5ca78ff9fbe00dc8a9a4d39da5af610a4ec49c7f7/cloudflare-4.3.1.tar.gz", hash = "sha256:b1e1c6beeb8d98f63bfe0a1cba874fc4e22e000bcc490544f956c689b3b5b258", size = 1933187, upload-time = "2025-06-16T21:43:18.716Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/3a/8f/c6c543565efd3144da4304efa5917aac06b6416a8663a6defe0e9b2b7569/cloudflare-4.3.1-py3-none-any.whl", hash = "sha256:6927135a5ee5633d6e2e1952ca0484745e933727aeeb189996d2ad9d292071c6", size = 4406465, upload-time = "2025-06-16T21:43:17.3Z" }, +] + [[package]] name = "colorama" version = "0.4.6" @@ -341,6 +370,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/47/ef/4cb333825d10317a36a1154341ba37e6e9c087bac99c1990ef07ffdb376f/dictdiffer-0.9.0-py2.py3-none-any.whl", hash = "sha256:442bfc693cfcadaf46674575d2eba1c53b42f5e404218ca2c2ff549f2df56595", size = 16754, upload-time = "2021-07-22T13:24:26.783Z" }, ] +[[package]] +name = "distro" +version = "1.9.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/fc/f8/98eea607f65de6527f8a2e8885fc8015d3e6f5775df186e443e0964a11c3/distro-1.9.0.tar.gz", hash = "sha256:2fa77c6fd8940f116ee1d6b94a2f90b13b5ea8d019b98bc8bafdcabcdd9bdbed", size = 60722, upload-time = "2023-12-24T09:54:32.31Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/12/b3/231ffd4ab1fc9d679809f356cebee130ac7daa00d6d6f3206dd4fd137e9e/distro-1.9.0-py3-none-any.whl", hash = "sha256:7bffd925d65168f85027d8da9af6bddab658135b840670a223589bc0c8ef02b2", size = 20277, upload-time = "2023-12-24T09:54:30.421Z" }, +] + [[package]] name = "dnspython" version = "2.7.0" @@ -410,6 +448,43 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/9c/a2/75fd80784ec33da8d39cf885e8811a4fbc045a90db5e336b8e345e66dbb2/glom-24.11.0-py3-none-any.whl", hash = "sha256:991db7fcb4bfa9687010aa519b7b541bbe21111e70e58fdd2d7e34bbaa2c1fbd", size = 102690, upload-time = "2024-11-02T23:17:46.468Z" }, ] +[[package]] +name = "h11" +version = "0.16.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/01/ee/02a2c011bdab74c6fb3c75474d40b3052059d95df7e73351460c8588d963/h11-0.16.0.tar.gz", hash = "sha256:4e35b956cf45792e4caa5885e69fba00bdbc6ffafbfa020300e549b208ee5ff1", size = 101250, upload-time = "2025-04-24T03:35:25.427Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/04/4b/29cac41a4d98d144bf5f6d33995617b185d14b22401f75ca86f384e87ff1/h11-0.16.0-py3-none-any.whl", hash = "sha256:63cf8bbe7522de3bf65932fda1d9c2772064ffb3dae62d55932da54b31cb6c86", size = 37515, upload-time = "2025-04-24T03:35:24.344Z" }, +] + +[[package]] +name = "httpcore" +version = "1.0.9" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "certifi" }, + { name = "h11" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/06/94/82699a10bca87a5556c9c59b5963f2d039dbd239f25bc2a63907a05a14cb/httpcore-1.0.9.tar.gz", hash = "sha256:6e34463af53fd2ab5d807f399a9b45ea31c3dfa2276f15a2c3f00afff6e176e8", size = 85484, upload-time = "2025-04-24T22:06:22.219Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/7e/f5/f66802a942d491edb555dd61e3a9961140fd64c90bce1eafd741609d334d/httpcore-1.0.9-py3-none-any.whl", hash = "sha256:2d400746a40668fc9dec9810239072b40b4484b640a8c38fd654a024c7a1bf55", size = 78784, upload-time = "2025-04-24T22:06:20.566Z" }, +] + +[[package]] +name = "httpx" +version = "0.28.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "anyio" }, + { name = "certifi" }, + { name = "httpcore" }, + { name = "idna" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/b1/df/48c586a5fe32a0f01324ee087459e112ebb7224f646c0b5023f5e79e9956/httpx-0.28.1.tar.gz", hash = "sha256:75e98c5f16b0f35b567856f597f06ff2270a374470a5c2392242528e3e3e42fc", size = 141406, upload-time = "2024-12-06T15:37:23.222Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/2a/39/e50c7c3a983047577ee07d2a9e53faf5a69493943ec3f6a384bdc792deb2/httpx-0.28.1-py3-none-any.whl", hash = "sha256:d909fcccc110f8c7faf814ca82a9a4d816bc5a6dbfea25d6591d6985b8ba59ad", size = 73517, upload-time = "2024-12-06T15:37:21.509Z" }, +] + [[package]] name = "idna" version = "3.10" @@ -475,12 +550,13 @@ wheels = [ [[package]] name = "layercake" -version = "0.11.2" +version = "0.11.3" source = { directory = "../layercake" } dependencies = [ { name = "arnparse" }, { name = "authlib" }, { name = "aws-lambda-powertools", extra = ["all"] }, + { name = "cloudflare" }, { name = "dictdiffer" }, { name = "ftfy" }, { name = "glom" }, @@ -506,6 +582,7 @@ requires-dist = [ { name = "arnparse", specifier = ">=0.0.2" }, { name = "authlib", specifier = ">=1.6.5" }, { name = "aws-lambda-powertools", extras = ["all"], specifier = ">=3.23.0" }, + { name = "cloudflare", specifier = ">=4.3.1" }, { name = "dictdiffer", specifier = ">=0.9.0" }, { name = "ftfy", specifier = ">=6.3.1" }, { name = "glom", specifier = ">=24.11.0" }, @@ -1022,6 +1099,15 @@ s3 = [ { name = "boto3" }, ] +[[package]] +name = "sniffio" +version = "1.3.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/a2/87/a6771e1546d97e7e041b6ae58d80074f81b7d5121207425c964ddf5cfdbd/sniffio-1.3.1.tar.gz", hash = "sha256:f4324edc670a0f49750a81b895f35c3adb843cca46f0530f79fc1babb23789dc", size = 20372, upload-time = "2024-02-25T23:20:04.057Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/e9/44/75a9c9421471a6c4805dbf2356f7c181a29c1879239abab1ea2cc8f38b40/sniffio-1.3.1-py3-none-any.whl", hash = "sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2", size = 10235, upload-time = "2024-02-25T23:20:01.196Z" }, +] + [[package]] name = "sqlite-fts4" version = "1.0.3"