Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { z } from 'zod'
- import { pipe, flow } from 'fp-ts/function'
- import { RequestFp } from '@helpers/Request'
- import * as T from 'fp-ts/lib/Task'
- import * as TE from 'fp-ts/lib/TaskEither'
- import { map, reduce, sort, last } from 'fp-ts/lib/Array'
- import { Ord } from 'fp-ts/Date'
- import { Option } from 'fp-ts/Option'
- const USERS_INDEX_API = `${process.env.MIX_APP_ENDPOINT}/api/v1/users`
- export type UserType = {
- id: number
- email: string
- lastAccess: Option<Date>
- microservices: MicroserviceRoleMinistryType[]
- }
- type MicroserviceRoleMinistryType = {
- microserviceName: string
- roles: {
- roleName: string
- ministryName: string[]
- }[]
- }
- const UsersResponseSchema = z.object({
- users: z.array(
- z.object({
- id: z.number(),
- email: z.string(),
- user_access_logs: z.array(
- z.object({
- created_at: z.string(),
- })
- ),
- accounts: z.array(
- z.object({
- ministry: z.object({
- name: z.string(),
- }),
- microservice_role: z.object({
- role: z.object({
- name: z.string(),
- }),
- microservice: z.object({
- name: z.string(),
- }),
- }),
- })
- ),
- })
- ),
- })
- type UsersResponseType = {
- users: {
- id: number
- email: string
- user_access_logs: {
- created_at: string
- }[]
- accounts: {
- ministry: {
- name: string
- }
- microservice_role: {
- role: {
- name: string
- }
- microservice: {
- name: string
- }
- }
- }[]
- }[]
- }
- const squashUserAccounts = reduce(
- [],
- (acc: MicroserviceRoleMinistryType[], cur: MicroserviceRoleMinistryType) => {
- // first iteration, skip parse
- if (!acc.length) return [cur]
- const indexOfExistingMicroservice = acc.findIndex(
- (y) => y.microserviceName === cur.microserviceName
- )
- // different microservice
- if (indexOfExistingMicroservice === -1) return [...acc, cur]
- // different role, push to roles array
- if (
- !acc[indexOfExistingMicroservice].roles.some(
- (y) => y.roleName === cur.roles[0].roleName
- )
- )
- acc[indexOfExistingMicroservice].roles.push(cur.roles[0])
- // same role, push to ministries array
- else
- acc[indexOfExistingMicroservice].roles[
- acc[indexOfExistingMicroservice].roles.findIndex(
- (y) => y.roleName === cur.roles[0].roleName
- )
- ].ministryName.push(cur.roles[0].ministryName[0])
- return acc
- }
- )
- export const GetUsers: T.Task<UserType[]> = pipe(
- RequestFp<UsersResponseType>(USERS_INDEX_API, UsersResponseSchema)(),
- TE.map(
- flow(
- ({ data: { users } }) => users,
- map((user) => ({
- id: user.id,
- email: user.email,
- microservices: pipe(
- user.accounts,
- map((account) => ({
- microserviceName: account.microservice_role.microservice.name,
- roles: [
- {
- roleName: account.microservice_role.role.name,
- ministryName: [account.ministry.name],
- },
- ],
- })),
- squashUserAccounts
- ),
- lastAccess: pipe(
- user.user_access_logs,
- map(({ created_at }) => <Date>(<unknown>created_at)),
- sort(Ord),
- last
- ),
- }))
- )
- ),
- TE.getOrElse(
- () => T.of(<UserType[]>{}) // Validation already handled by RequestFp(), just cast to empty object
- )
- )
Advertisement
Advertisement
Advertisement
RAW Paste Data
Copied
Advertisement