import cryptoRandomString from 'crypto-random-string' import { Resource, PaginationArgs, ResourceCollection, ResourceCreateArgs, ResourceAcl } from '../types' interface GetResourcesArgs extends PaginationArgs { userId: string } export const getResources = ( countResources: (userId: string) => Promise, queryResources: (params: GetResourcesArgs) => Promise ) => async (params: GetResourcesArgs): Promise => { const totalCount = await countResources(params.userId) const items = await queryResources(params) let cursor = null if (items.length > 0) { cursor = items.slice(-1)[0].createdAt.toISOString() } return { totalCount, items, cursor } } export const createResource = ( resourceSaver: (resource: Resource) => Promise, resourceAclSaver: (resourceAcl: ResourceAcl) => Promise ) => async ({ userId, name }: ResourceCreateArgs): Promise => { // 1. if no org, create project in main region, validate that, regionId is null // 2. if org, validate if user has access to the org // 3. if org and region, validate if org has access to region // 4. create resource const id = cryptoRandomString({ length: 10 }) const resource = { id, name, createdAt: new Date() } await resourceSaver(resource) await resourceAclSaver({ resourceId: id, userId }) return id }