Search in sources :

Example 1 with DEFAULT_SITE_ID

use of com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID in project formkiq-core by formkiq.

the class DocumentsRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    ApiPagination pagination = getPagination(awsservice.documentCacheService(), event);
    final int limit = pagination != null ? pagination.getLimit() : getLimit(logger, event);
    final PaginationMapToken ptoken = pagination != null ? pagination.getStartkey() : null;
    String tz = getParameter(event, "tz");
    String dateString = getParameter(event, "date");
    if (StringUtils.isBlank(dateString)) {
        if (StringUtils.isNotBlank(tz)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            ZoneOffset offset = DateUtil.getZoneOffset(tz);
            sdf.setTimeZone(TimeZone.getTimeZone(offset));
            dateString = sdf.format(new Date());
        } else {
            dateString = this.df.format(new Date());
        }
    }
    ZonedDateTime date = transformToDate(logger, awsservice, dateString, tz);
    String siteId = authorizer.getSiteId();
    final PaginationResults<DocumentItem> results = awsservice.documentService().findDocumentsByDate(siteId, date, ptoken, limit);
    ApiPagination current = createPagination(awsservice.documentCacheService(), event, pagination, results.getToken(), limit);
    List<DocumentItem> documents = subList(results.getResults(), limit);
    List<DynamicDocumentItem> items = documents.stream().map(m -> new DocumentItemToDynamicDocumentItem().apply(m)).collect(Collectors.toList());
    items.forEach(i -> i.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID));
    Map<String, Object> map = new HashMap<>();
    map.put("documents", items);
    map.put("previous", current.getPrevious());
    map.put("next", current.hasNext() ? current.getNext() : null);
    return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(map));
}
Also used : StringUtils(software.amazon.awssdk.utils.StringUtils) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) DEFAULT_SITE_ID(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) ApiGatewayRequestEventUtil(com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) Map(java.util.Map) ZoneOffset(java.time.ZoneOffset) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) TimeZone(java.util.TimeZone) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) ZoneRulesException(java.time.zone.ZoneRulesException) Collectors(java.util.stream.Collectors) BadException(com.formkiq.lambda.apigateway.exception.BadException) List(java.util.List) DateUtil(com.formkiq.stacks.dynamodb.DateUtil) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) PaginationResults(com.formkiq.stacks.dynamodb.PaginationResults) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) HashMap(java.util.HashMap) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) Date(java.util.Date) ZoneOffset(java.time.ZoneOffset) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) ZonedDateTime(java.time.ZonedDateTime) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with DEFAULT_SITE_ID

use of com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID in project formkiq-core by formkiq.

the class SitesRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    List<DynamicObject> sites = authorizer.getSiteIds().stream().map(siteId -> {
        DynamicObject config = awsservice.config(siteId);
        config.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID);
        return config;
    }).collect(Collectors.toList());
    sites.forEach(ob -> {
        ob.remove("PK");
        ob.remove("SK");
    });
    updateUploadEmail(logger, awsservice, authorizer, sites);
    return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(Map.of("sites", sites)));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) DEFAULT_SITE_ID(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) Random(java.util.Random) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) Collectors(java.util.stream.Collectors) List(java.util.List) ApiGatewayRequestEventUtil(com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) Map(java.util.Map) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse)

Example 3 with DEFAULT_SITE_ID

use of com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID in project formkiq-core by formkiq.

the class WebhooksRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsServices) throws Exception {
    String siteId = authorizer.getSiteId();
    String url = awsServices.ssmService().getParameterValue("/formkiq/" + awsServices.appEnvironment() + "/api/DocumentsPublicHttpUrl");
    List<DynamicObject> list = awsServices.webhookService().findWebhooks(siteId);
    List<Map<String, Object>> webhooks = list.stream().map(m -> {
        String path = "private".equals(m.getString("enabled")) ? "/private" : "/public";
        Map<String, Object> map = new HashMap<>();
        String u = url + path + "/webhooks/" + m.getString("documentId");
        if (siteId != null && !DEFAULT_SITE_ID.equals(siteId)) {
            u += "?siteId=" + siteId;
        }
        map.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID);
        map.put("id", m.getString("documentId"));
        map.put("name", m.getString("path"));
        map.put("url", u);
        map.put("insertedDate", m.getString("inserteddate"));
        map.put("userId", m.getString("userId"));
        map.put("enabled", m.getString("enabled"));
        map.put("ttl", m.getString("ttl"));
        return map;
    }).collect(Collectors.toList());
    return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(Map.of("webhooks", webhooks)));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) WEBHOOK_TIME_TO_LIVE(com.formkiq.stacks.dynamodb.ConfigService.WEBHOOK_TIME_TO_LIVE) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) Collection(java.util.Collection) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) DEFAULT_SITE_ID(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) HashMap(java.util.HashMap) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) Collectors(java.util.stream.Collectors) BadException(com.formkiq.lambda.apigateway.exception.BadException) TooManyRequestsException(com.formkiq.lambda.apigateway.exception.TooManyRequestsException) MAX_WEBHOOKS(com.formkiq.stacks.dynamodb.ConfigService.MAX_WEBHOOKS) List(java.util.List) ApiGatewayRequestEventUtil(com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) Map(java.util.Map) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ZoneOffset(java.time.ZoneOffset) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)3 ApiAuthorizer (com.formkiq.lambda.apigateway.ApiAuthorizer)3 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)3 ApiGatewayRequestEventUtil (com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil)3 ApiGatewayRequestHandler (com.formkiq.lambda.apigateway.ApiGatewayRequestHandler)3 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)3 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)3 SC_OK (com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK)3 AwsServiceCache (com.formkiq.lambda.apigateway.AwsServiceCache)3 DEFAULT_SITE_ID (com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID)3 List (java.util.List)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 BadException (com.formkiq.lambda.apigateway.exception.BadException)2 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)2 ZoneOffset (java.time.ZoneOffset)2 ZonedDateTime (java.time.ZonedDateTime)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 ApiPagination (com.formkiq.lambda.apigateway.ApiPagination)1