diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f0df34..5099403 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ Given a version number MAJOR.MINOR.PATCH, increment: ## [Unreleased] +### Added +- IndividualAccountRequest resource +- IndividualAccountAttachment resource ## [0.21.0] - 2026-05-28 ### Added diff --git a/README.md b/README.md index ed3da3e..a8fc6ad 100755 --- a/README.md +++ b/README.md @@ -66,6 +66,8 @@ This SDK version is compatible with the Stark Infra API v2. - [Identity](#identity) - [IndividualIdentity](#create-individualidentities): Create individual identities - [IndividualDocument](#create-individualdocuments): Create individual documents + - [IndividualAccountRequest](#create-individualaccountrequests): Request to open an individual account + - [IndividualAccountAttachment](#create-individualaccountattachments): Attach supporting documents to an individual account request - [Webhook](#webhook): - [Webhook](#create-a-webhook-subscription): Configure your webhook endpoints and subscriptions - [WebhookEvents](#process-webhook-events): Manage Webhook events @@ -3636,6 +3638,238 @@ IndividualDocument.Log log = IndividualDocument.Log.get("5155165527080960"); System.out.println(log); ``` +### Create IndividualAccountRequests + +You can create IndividualAccountRequests to open a Stark Infra account for an individual. The address is a structured nested object. + +```java +import com.starkinfra.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +HashMap address = new HashMap<>(); +address.put("street", "Rua do Estilo Barroco"); +address.put("number", "648"); +address.put("neighborhood", "Santo Amaro"); +address.put("city", "Sao Paulo"); +address.put("state", "SP"); +address.put("zipCode", "05724005"); + +HashMap data = new HashMap<>(); +data.put("name", "Tony Stark"); +data.put("taxId", "012.345.678-90"); +data.put("address", address); +data.put("income", 1000000L); +data.put("tags", new String[]{"employees", "monthly"}); + +List requests = new ArrayList<>(); +requests.add(new IndividualAccountRequest(data)); + +requests = IndividualAccountRequest.create(requests); + +for (IndividualAccountRequest request : requests) { + System.out.println(request); +} +``` + +**Note**: Instead of using IndividualAccountRequest objects, you can also pass each element in dictionary format + +### Query IndividualAccountRequests + +You can query multiple IndividualAccountRequests according to filters. + +```java +import com.starkinfra.*; +import com.starkinfra.utils.Generator; +import java.util.HashMap; + +HashMap params = new HashMap<>(); +params.put("limit", 3); +params.put("status", "created"); +params.put("after", "2019-04-01"); +params.put("before", "2030-04-30"); + +Generator requests = IndividualAccountRequest.query(params); + +for (IndividualAccountRequest request : requests) { + System.out.println(request); +} +``` + +### Get an IndividualAccountRequest + +After its creation, information on an IndividualAccountRequest may be retrieved by its id. + +```java +import com.starkinfra.*; + +IndividualAccountRequest request = IndividualAccountRequest.get("5189530608992256"); + +System.out.println(request); +``` + +### Update an IndividualAccountRequest + +You can update an IndividualAccountRequest by its id. The address is replaced as a whole object. + +```java +import com.starkinfra.*; +import java.util.HashMap; + +HashMap patchData = new HashMap<>(); +patchData.put("status", "processing"); + +IndividualAccountRequest request = IndividualAccountRequest.update("5189530608992256", patchData); + +System.out.println(request); +``` + +### Query IndividualAccountRequest logs + +You can query IndividualAccountRequest logs to better understand IndividualAccountRequest life cycles. + +```java +import com.starkinfra.*; +import com.starkinfra.utils.Generator; +import java.util.HashMap; + +HashMap params = new HashMap<>(); +params.put("limit", 3); +params.put("after", "2019-04-01"); +params.put("before", "2030-04-30"); + +Generator logs = IndividualAccountRequest.Log.query(params); + +for (IndividualAccountRequest.Log log : logs) { + System.out.println(log); +} +``` + +### Get an IndividualAccountRequest log + +You can also get a specific log by its id. + +```java +import com.starkinfra.*; + +IndividualAccountRequest.Log log = IndividualAccountRequest.Log.get("5189530608992256"); + +System.out.println(log); +``` + +### Create IndividualAccountAttachments + +You can attach supporting documents to an IndividualAccountRequest. Pass the raw image bytes and a MIME content type; the SDK encodes them as a data: URL before sending. + +```java +import com.starkinfra.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.nio.file.Files; +import java.nio.file.Paths; + +byte[] content = Files.readAllBytes(Paths.get("identity-front.png")); + +HashMap data = new HashMap<>(); +data.put("type", "identity-front"); +data.put("content", content); +data.put("contentType", "image/png"); +data.put("accountRequestId", "5189530608992256"); +data.put("tags", new String[]{"employees"}); + +List attachments = new ArrayList<>(); +attachments.add(new IndividualAccountAttachment(data)); + +attachments = IndividualAccountAttachment.create(attachments); + +for (IndividualAccountAttachment attachment : attachments) { + System.out.println(attachment); +} +``` + +**Note**: Instead of using IndividualAccountAttachment objects, you can also pass each element in dictionary format + +### Query IndividualAccountAttachments + +You can query multiple IndividualAccountAttachments according to filters. + +```java +import com.starkinfra.*; +import com.starkinfra.utils.Generator; +import java.util.HashMap; + +HashMap params = new HashMap<>(); +params.put("limit", 3); +params.put("status", "created"); +params.put("after", "2019-04-01"); +params.put("before", "2030-04-30"); + +Generator attachments = IndividualAccountAttachment.query(params); + +for (IndividualAccountAttachment attachment : attachments) { + System.out.println(attachment); +} +``` + +### Get an IndividualAccountAttachment + +After its creation, information on an IndividualAccountAttachment may be retrieved by its id. + +```java +import com.starkinfra.*; + +IndividualAccountAttachment attachment = IndividualAccountAttachment.get("5656565656565656"); + +System.out.println(attachment); +``` + +### Cancel an IndividualAccountAttachment + +You can cancel an IndividualAccountAttachment by its id. + +```java +import com.starkinfra.*; + +IndividualAccountAttachment attachment = IndividualAccountAttachment.cancel("5656565656565656"); + +System.out.println(attachment); +``` + +### Query IndividualAccountAttachment logs + +You can query IndividualAccountAttachment logs to better understand IndividualAccountAttachment life cycles. + +```java +import com.starkinfra.*; +import com.starkinfra.utils.Generator; +import java.util.HashMap; + +HashMap params = new HashMap<>(); +params.put("limit", 3); +params.put("after", "2019-04-01"); +params.put("before", "2030-04-30"); + +Generator logs = IndividualAccountAttachment.Log.query(params); + +for (IndividualAccountAttachment.Log log : logs) { + System.out.println(log); +} +``` + +### Get an IndividualAccountAttachment log + +You can also get a specific log by its id. + +```java +import com.starkinfra.*; + +IndividualAccountAttachment.Log log = IndividualAccountAttachment.Log.get("5656565656565656"); + +System.out.println(log); +``` + ## Webhook ### Create a webhook subscription diff --git a/src/main/java/com/starkinfra/IndividualAccountAttachment.java b/src/main/java/com/starkinfra/IndividualAccountAttachment.java new file mode 100644 index 0000000..c8f1b73 --- /dev/null +++ b/src/main/java/com/starkinfra/IndividualAccountAttachment.java @@ -0,0 +1,661 @@ +package com.starkinfra; + +import com.starkinfra.utils.Rest; +import com.starkinfra.utils.Resource; +import com.starkinfra.utils.Generator; +import com.starkcore.utils.SubResource; +import com.starkinfra.error.ErrorElement; + +import java.util.Map; +import java.util.List; +import java.util.Base64; +import java.util.HashMap; +import java.util.ArrayList; + + +public final class IndividualAccountAttachment extends Resource { + /** + * IndividualAccountAttachment object + *

+ * IndividualAccountAttachments are supporting documents (identity, driver's license, selfie) + * attached to an IndividualAccountRequest for the account-approval flow. The caller uploads the + * raw image bytes and a MIME content type; the SDK encodes them as a data: URL before sending. + *

+ * When you initialize an IndividualAccountAttachment, the entity will not be automatically + * created in the Stark Infra API. The 'create' function sends the objects + * to the Stark Infra API and returns the list of created objects. + *

+ * Parameters: + * type [string]: kind of supporting document. Options: "drivers-license-front", "drivers-license-back", "identity-front", "identity-back" or "selfie" + * content [string]: Base64 data url of the picture, built from the raw bytes and the contentType. ex: "data:image/png;base64,/9j/4AAQSkZJRgABAQAASABIAAD..." + * accountRequestId [string]: ID of the parent IndividualAccountRequest. ex: "5189530608992256" + * tags [list of strings, default null]: list of strings for reference when searching for IndividualAccountAttachments. ex: ["employees", "monthly"] + * id [string]: unique id returned when the IndividualAccountAttachment is created. ex: "5656565656565656" + * status [string]: current IndividualAccountAttachment status. ex: "created", "success", "failed" or "deleted" + * created [string]: creation datetime for the IndividualAccountAttachment. ex: "2020-03-10 10:30:00.000000+00:00" + * + */ + static ClassData data = new ClassData(IndividualAccountAttachment.class, "IndividualAccountAttachment"); + + public String type; + public String content; + public String accountRequestId; + public String[] tags; + public String status; + public String created; + + /** + * IndividualAccountAttachment object + *

+ * IndividualAccountAttachments are supporting documents attached to an IndividualAccountRequest. + * This constructor takes the content already encoded as a Base64 data: URL. + *

+ * Parameters: + * @param type [string]: kind of supporting document. ex: "drivers-license-front", "drivers-license-back", "identity-front", "identity-back" or "selfie" + * @param content [string]: Base64 data url of the picture. ex: "data:image/png;base64,/9j/4AAQSkZJRgABAQAASABIAAD..." + * @param accountRequestId [string]: ID of the parent IndividualAccountRequest. ex: "5189530608992256" + * @param tags [list of strings, default null]: list of strings for reference when searching for IndividualAccountAttachments. ex: ["employees", "monthly"] + * @param id [string]: unique id returned when the IndividualAccountAttachment is created. ex: "5656565656565656" + * @param status [string]: current IndividualAccountAttachment status. ex: "created", "success", "failed" or "deleted" + * @param created [string]: creation datetime for the IndividualAccountAttachment. ex: "2020-03-10 10:30:00.000000+00:00" + */ + public IndividualAccountAttachment(String type, String content, String accountRequestId, + String[] tags, String id, String status, String created) { + super(id); + this.type = type; + this.content = content; + this.accountRequestId = accountRequestId; + this.tags = tags; + this.status = status; + this.created = created; + } + + /** + * IndividualAccountAttachment object + *

+ * IndividualAccountAttachments are supporting documents attached to an IndividualAccountRequest. + *

+ * When you initialize an IndividualAccountAttachment, the entity will not be automatically + * created in the Stark Infra API. The 'create' function sends the objects + * to the Stark Infra API and returns the list of created objects. + *

+ * Parameters (required): + * @param data map of properties for the creation of the IndividualAccountAttachment + * type [string]: kind of supporting document. ex: "drivers-license-front", "drivers-license-back", "identity-front", "identity-back" or "selfie" + * content [byte[]]: raw image bytes. ex: Files.readAllBytes(path) + * contentType [string]: content MIME type. This parameter is required as input only. ex: "image/png" or "image/jpeg" + * accountRequestId [string]: ID of the parent IndividualAccountRequest. ex: "5189530608992256" + *

+ * Parameters (optional): + * tags [list of strings, default null]: list of strings for reference when searching for IndividualAccountAttachments. ex: ["employees", "monthly"] + *

+ * Attributes (return-only): + * id [string]: unique id returned when the IndividualAccountAttachment is created. ex: "5656565656565656" + * status [string]: current IndividualAccountAttachment status. ex: "created", "success", "failed" or "deleted" + * created [string]: creation datetime for the IndividualAccountAttachment. ex: "2020-03-10 10:30:00.000000+00:00" + * @throws Exception error in the request + */ + @SuppressWarnings("unchecked") + public IndividualAccountAttachment(Map data) throws Exception { + super(null); + HashMap dataCopy = new HashMap<>(data); + + this.type = (String) dataCopy.remove("type"); + this.accountRequestId = (String) dataCopy.remove("accountRequestId"); + this.tags = (String[]) dataCopy.remove("tags"); + this.status = null; + this.created = null; + + if (dataCopy.containsKey("contentType") && dataCopy.get("content") instanceof byte[]) { + byte[] content = (byte[]) dataCopy.remove("content"); + String contentType = (String) dataCopy.remove("contentType"); + this.content = "data:" + contentType + ";base64," + Base64.getEncoder().encodeToString(content); + } + + if (dataCopy.containsKey("content") && dataCopy.get("content") instanceof byte[]) { + byte[] content = (byte[]) dataCopy.remove("content"); + this.content = "data:;base64," + Base64.getEncoder().encodeToString(content); + } + + if (dataCopy.containsKey("content") && dataCopy.get("content") instanceof String) { + this.content = (String) dataCopy.remove("content"); + } + + if (!dataCopy.isEmpty()) { + throw new Exception("Unknown parameters used in constructor: [" + String.join(", ", dataCopy.keySet()) + "]"); + } + } + + /** + * Retrieve a specific IndividualAccountAttachment + *

+ * Receive a single IndividualAccountAttachment object previously created in the Stark Infra API by passing its id + *

+ * Parameters: + * @param id [string]: object unique id. ex: "5656565656565656" + *

+ * Return: + * @return IndividualAccountAttachment object with updated attributes + * @throws Exception error in the request + */ + public static IndividualAccountAttachment get(String id) throws Exception { + return IndividualAccountAttachment.get(id, null); + } + + /** + * Retrieve a specific IndividualAccountAttachment + *

+ * Receive a single IndividualAccountAttachment object previously created in the Stark Infra API by passing its id + *

+ * Parameters: + * @param id [string]: object unique id. ex: "5656565656565656" + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return IndividualAccountAttachment object with updated attributes + * @throws Exception error in the request + */ + public static IndividualAccountAttachment get(String id, User user) throws Exception { + return Rest.getId(data, id, user); + } + + /** + * Retrieve IndividualAccountAttachments + *

+ * Receive a generator of IndividualAccountAttachment objects previously created in the Stark Infra API. + * Use this function instead of page if you want to stream the objects without worrying about cursors and pagination. + *

+ * Parameters: + * @param params map of parameters for the query + * limit [integer, default null]: maximum number of objects to be retrieved. Unlimited if null. ex: 35 + * after [string, default null]: date filter for objects created only after specified date. ex: "2020-03-10" + * before [string, default null]: date filter for objects created only before specified date. ex: "2020-03-10" + * status [string, default null]: filter for status of retrieved objects. ex: "created" + * tags [list of strings, default null]: tags to filter retrieved objects. ex: ["tony", "stark"] + * ids [list of strings, default null]: list of ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + *

+ * Return: + * @return generator of IndividualAccountAttachment objects with updated attributes + * @throws Exception error in the request + */ + public static Generator query(Map params) throws Exception { + return IndividualAccountAttachment.query(params, null); + } + + /** + * Retrieve IndividualAccountAttachments + *

+ * Receive a generator of IndividualAccountAttachment objects previously created in the Stark Infra API. + * Use this function instead of page if you want to stream the objects without worrying about cursors and pagination. + *

+ * Parameters: + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return generator of IndividualAccountAttachment objects with updated attributes + * @throws Exception error in the request + */ + public static Generator query(User user) throws Exception { + return IndividualAccountAttachment.query(new HashMap<>(), user); + } + + /** + * Retrieve IndividualAccountAttachments + *

+ * Receive a generator of IndividualAccountAttachment objects previously created in the Stark Infra API. + * Use this function instead of page if you want to stream the objects without worrying about cursors and pagination. + *

+ * Return: + * @return generator of IndividualAccountAttachment objects with updated attributes + * @throws Exception error in the request + */ + public static Generator query() throws Exception { + return IndividualAccountAttachment.query(new HashMap<>(), null); + } + + /** + * Retrieve IndividualAccountAttachments + *

+ * Receive a generator of IndividualAccountAttachment objects previously created in the Stark Infra API. + * Use this function instead of page if you want to stream the objects without worrying about cursors and pagination. + *

+ * Parameters: + * @param params map of parameters for the query + * limit [integer, default null]: maximum number of objects to be retrieved. Unlimited if null. ex: 35 + * after [string, default null]: date filter for objects created only after specified date. ex: "2020-03-10" + * before [string, default null]: date filter for objects created only before specified date. ex: "2020-03-10" + * status [string, default null]: filter for status of retrieved objects. ex: "created" + * tags [list of strings, default null]: tags to filter retrieved objects. ex: ["tony", "stark"] + * ids [list of strings, default null]: list of ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return generator of IndividualAccountAttachment objects with updated attributes + * @throws Exception error in the request + */ + public static Generator query(Map params, User user) throws Exception { + return Rest.getStream(data, params, user); + } + + public final static class Page { + public List attachments; + public String cursor; + + public Page(List attachments, String cursor) { + this.attachments = attachments; + this.cursor = cursor; + } + } + + /** + * Retrieve paged IndividualAccountAttachments + *

+ * Receive a list of up to 100 IndividualAccountAttachment objects previously created in the Stark Infra API and the cursor to the next page. + * Use this function instead of query if you want to manually page your requests. + *

+ * Parameters: + * @param params map of parameters for the query + * cursor [string, default null]: cursor returned on the previous page function call + * limit [integer, default 100]: maximum number of objects to be retrieved. It must be an integer between 1 and 100. ex: 50 + * after [string, default null]: date filter for objects created only after specified date. ex: "2020-03-10" + * before [string, default null]: date filter for objects created only before specified date. ex: "2020-03-10" + * status [string, default null]: filter for status of retrieved objects. ex: "created" + * tags [list of strings, default null]: tags to filter retrieved objects. ex: ["tony", "stark"] + * ids [list of strings, default null]: list of ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + *

+ * Return: + * @return IndividualAccountAttachment.Page object: + * IndividualAccountAttachment.Page.attachments: list of IndividualAccountAttachment objects with updated attributes + * IndividualAccountAttachment.Page.cursor: cursor to retrieve the next page of IndividualAccountAttachment objects + * @throws Exception error in the request + */ + public static Page page(Map params) throws Exception { + return page(params, null); + } + + /** + * Retrieve paged IndividualAccountAttachments + *

+ * Receive a list of up to 100 IndividualAccountAttachment objects previously created in the Stark Infra API and the cursor to the next page. + * Use this function instead of query if you want to manually page your requests. + *

+ * Parameters: + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return IndividualAccountAttachment.Page object: + * IndividualAccountAttachment.Page.attachments: list of IndividualAccountAttachment objects with updated attributes + * IndividualAccountAttachment.Page.cursor: cursor to retrieve the next page of IndividualAccountAttachment objects + * @throws Exception error in the request + */ + public static Page page(User user) throws Exception { + return page(new HashMap<>(), user); + } + + /** + * Retrieve paged IndividualAccountAttachments + *

+ * Receive a list of up to 100 IndividualAccountAttachment objects previously created in the Stark Infra API and the cursor to the next page. + * Use this function instead of query if you want to manually page your requests. + *

+ * Return: + * @return IndividualAccountAttachment.Page object: + * IndividualAccountAttachment.Page.attachments: list of IndividualAccountAttachment objects with updated attributes + * IndividualAccountAttachment.Page.cursor: cursor to retrieve the next page of IndividualAccountAttachment objects + * @throws Exception error in the request + */ + public static Page page() throws Exception { + return page(new HashMap<>(), null); + } + + /** + * Retrieve paged IndividualAccountAttachments + *

+ * Receive a list of up to 100 IndividualAccountAttachment objects previously created in the Stark Infra API and the cursor to the next page. + * Use this function instead of query if you want to manually page your requests. + *

+ * Parameters: + * @param params map of parameters for the query + * cursor [string, default null]: cursor returned on the previous page function call + * limit [integer, default 100]: maximum number of objects to be retrieved. It must be an integer between 1 and 100. ex: 50 + * after [string, default null]: date filter for objects created only after specified date. ex: "2020-03-10" + * before [string, default null]: date filter for objects created only before specified date. ex: "2020-03-10" + * status [string, default null]: filter for status of retrieved objects. ex: "created" + * tags [list of strings, default null]: tags to filter retrieved objects. ex: ["tony", "stark"] + * ids [list of strings, default null]: list of ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return IndividualAccountAttachment.Page object: + * IndividualAccountAttachment.Page.attachments: list of IndividualAccountAttachment objects with updated attributes + * IndividualAccountAttachment.Page.cursor: cursor to retrieve the next page of IndividualAccountAttachment objects + * @throws Exception error in the request + */ + public static Page page(Map params, User user) throws Exception { + com.starkcore.utils.Page page = Rest.getPage(data, params, user); + List attachments = new ArrayList<>(); + for (SubResource attachment: page.entities) { + attachments.add((IndividualAccountAttachment) attachment); + } + return new Page(attachments, page.cursor); + } + + /** + * Create IndividualAccountAttachments + *

+ * Send a list of IndividualAccountAttachment objects for creation in the Stark Infra API + *

+ * Parameters: + * @param attachments [list of IndividualAccountAttachment objects or HashMaps]: list of IndividualAccountAttachment objects to be created in the API + *

+ * Return: + * @return list of IndividualAccountAttachment objects with updated attributes + * @throws Exception error in the request + */ + public static List create(List attachments) throws Exception { + return IndividualAccountAttachment.create(attachments, null); + } + + /** + * Create IndividualAccountAttachments + *

+ * Send a list of IndividualAccountAttachment objects for creation in the Stark Infra API + *

+ * Parameters: + * @param attachments [list of IndividualAccountAttachment objects or HashMaps]: list of IndividualAccountAttachment objects to be created in the API + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return list of IndividualAccountAttachment objects with updated attributes + * @throws Exception error in the request + */ + @SuppressWarnings("unchecked") + public static List create(List attachments, User user) throws Exception { + List attachmentList = new ArrayList<>(); + for (Object attachment : attachments){ + if (attachment instanceof Map){ + attachmentList.add(new IndividualAccountAttachment((Map) attachment)); + continue; + } + if (attachment instanceof IndividualAccountAttachment){ + attachmentList.add((IndividualAccountAttachment) attachment); + continue; + } + throw new Exception("Unknown type \"" + attachment.getClass() + "\", use IndividualAccountAttachment or HashMap"); + } + return Rest.post(data, attachmentList, user); + } + + /** + * Cancel an IndividualAccountAttachment entity + *

+ * Cancel an IndividualAccountAttachment entity previously created in the Stark Infra API + *

+ * Parameters: + * @param id [string]: IndividualAccountAttachment unique id. ex: "5656565656565656" + *

+ * Return: + * @return deleted IndividualAccountAttachment object + * @throws Exception error in the request + */ + public static IndividualAccountAttachment cancel(String id) throws Exception { + return IndividualAccountAttachment.cancel(id, null); + } + + /** + * Cancel an IndividualAccountAttachment entity + *

+ * Cancel an IndividualAccountAttachment entity previously created in the Stark Infra API + *

+ * Parameters: + * @param id [string]: IndividualAccountAttachment unique id. ex: "5656565656565656" + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return deleted IndividualAccountAttachment object + * @throws Exception error in the request + */ + public static IndividualAccountAttachment cancel(String id, User user) throws Exception { + return Rest.delete(data, id, user); + } + + public final static class Log extends Resource { + static ClassData data = new ClassData(Log.class, "IndividualAccountAttachmentLog"); + + public String created; + public String type; + public List errors; + public IndividualAccountAttachment attachment; + + /** + * IndividualAccountAttachment Log object + *

+ * Every time an IndividualAccountAttachment entity is modified, a corresponding IndividualAccountAttachment Log + * is generated for the entity. This log is never generated by the user. + *

+ * Attributes: + * @param id [string]: unique id returned when the log is created. ex: "5656565656565656" + * @param attachment [IndividualAccountAttachment]: IndividualAccountAttachment entity to which the log refers to. + * @param errors [list of ErrorElement]: list of errors linked to the IndividualAccountAttachment event. + * @param type [string]: type of the IndividualAccountAttachment event which triggered the log creation. ex: "success" or "failed" + * @param created [string]: creation datetime for the log. ex: "2020-03-10 10:30:00.000000+00:00" + */ + public Log(String created, String type, List errors, IndividualAccountAttachment attachment, String id) { + super(id); + this.created = created; + this.type = type; + this.errors = errors; + this.attachment = attachment; + } + + /** + * Retrieve a specific IndividualAccountAttachment Log + *

+ * Receive a single IndividualAccountAttachment Log object previously created by the Stark Infra API by passing its id + *

+ * Parameters: + * @param id [string]: object unique id. ex: "5656565656565656" + *

+ * Return: + * @return IndividualAccountAttachment Log object with updated attributes + * @throws Exception error in the request + */ + public static Log get(String id) throws Exception { + return Log.get(id, null); + } + + /** + * Retrieve a specific IndividualAccountAttachment Log + *

+ * Receive a single IndividualAccountAttachment Log object previously created by the Stark Infra API by passing its id + *

+ * Parameters: + * @param id [string]: object unique id. ex: "5656565656565656" + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return IndividualAccountAttachment Log object with updated attributes + * @throws Exception error in the request + */ + public static Log get(String id, User user) throws Exception { + return Rest.getId(data, id, user); + } + + /** + * Retrieve IndividualAccountAttachment Logs + *

+ * Receive a generator of IndividualAccountAttachment.Log objects previously created in the Stark Infra API. + * Use this function instead of page if you want to stream the objects without worrying about cursors and pagination. + *

+ * Parameters: + * @param params map of parameters for the query + * limit [integer, default null]: maximum number of objects to be retrieved. Unlimited if null. ex: 35 + * after [string, default null]: date filter for objects created only after specified date. ex: "2020-03-09" + * before [string, default null]: date filter for objects created only before specified date. ex: "2020-03-10" + * types [list of strings, default null]: filter retrieved objects by types. ex: "success" or "failed" + * attachmentIds [list of strings, default null]: list of IndividualAccountAttachment ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + *

+ * Return: + * @return generator of IndividualAccountAttachment Log objects with updated attributes + * @throws Exception error in the request + */ + public static Generator query(Map params) throws Exception { + return Log.query(params, null); + } + + /** + * Retrieve IndividualAccountAttachment Logs + *

+ * Receive a generator of IndividualAccountAttachment.Log objects previously created in the Stark Infra API. + * Use this function instead of page if you want to stream the objects without worrying about cursors and pagination. + *

+ * Parameters: + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return generator of IndividualAccountAttachment Log objects with updated attributes + * @throws Exception error in the request + */ + public static Generator query(User user) throws Exception { + return Log.query(new HashMap<>(), user); + } + + /** + * Retrieve IndividualAccountAttachment Logs + *

+ * Receive a generator of IndividualAccountAttachment.Log objects previously created in the Stark Infra API. + * Use this function instead of page if you want to stream the objects without worrying about cursors and pagination. + *

+ * Return: + * @return generator of IndividualAccountAttachment Log objects with updated attributes + * @throws Exception error in the request + */ + public static Generator query() throws Exception { + return Log.query(new HashMap<>(), null); + } + + /** + * Retrieve IndividualAccountAttachment Logs + *

+ * Receive a generator of IndividualAccountAttachment.Log objects previously created in the Stark Infra API. + * Use this function instead of page if you want to stream the objects without worrying about cursors and pagination. + *

+ * Parameters: + * @param params map of parameters for the query + * limit [integer, default null]: maximum number of objects to be retrieved. Unlimited if null. ex: 35 + * after [string, default null]: date filter for objects created only after specified date. ex: "2020-03-09" + * before [string, default null]: date filter for objects created only before specified date. ex: "2020-03-10" + * types [list of strings, default null]: filter retrieved objects by types. ex: "success" or "failed" + * attachmentIds [list of strings, default null]: list of IndividualAccountAttachment ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return generator of IndividualAccountAttachment Log objects with updated attributes + * @throws Exception error in the request + */ + public static Generator query(Map params, User user) throws Exception { + return Rest.getStream(data, params, user); + } + + public final static class Page { + public List logs; + public String cursor; + + public Page(List logs, String cursor) { + this.logs = logs; + this.cursor = cursor; + } + } + + /** + * Retrieve paged IndividualAccountAttachment.Logs + *

+ * Receive a list of up to 100 IndividualAccountAttachment.Log objects previously created in the Stark Infra API and the cursor to the next page. + * Use this function instead of query if you want to manually page your requests. + *

+ * Parameters: + * @param params map of parameters for the query + * cursor [string, default null]: cursor returned on the previous page function call + * limit [integer, default 100]: maximum number of objects to be retrieved. It must be an integer between 1 and 100. ex: 50 + * after [string, default null]: date filter for objects created only after specified date. ex: "2020-03-09" + * before [string, default null]: date filter for objects created only before specified date. ex: "2020-03-10" + * types [list of strings, default null]: filter retrieved objects by types. ex: "success" or "failed" + * attachmentIds [list of strings, default null]: list of IndividualAccountAttachment ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + *

+ * Return: + * @return IndividualAccountAttachment.Log.Page object: + * IndividualAccountAttachment.Log.Page.logs: list of IndividualAccountAttachment.Log objects with updated attributes + * IndividualAccountAttachment.Log.Page.cursor: cursor to retrieve the next page of IndividualAccountAttachment.Log objects + * @throws Exception error in the request + */ + public static Log.Page page(Map params) throws Exception { + return Log.page(params, null); + } + + /** + * Retrieve paged IndividualAccountAttachment.Logs + *

+ * Receive a list of up to 100 IndividualAccountAttachment.Log objects previously created in the Stark Infra API and the cursor to the next page. + * Use this function instead of query if you want to manually page your requests. + *

+ * Parameters: + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return IndividualAccountAttachment.Log.Page object: + * IndividualAccountAttachment.Log.Page.logs: list of IndividualAccountAttachment.Log objects with updated attributes + * IndividualAccountAttachment.Log.Page.cursor: cursor to retrieve the next page of IndividualAccountAttachment.Log objects + * @throws Exception error in the request + */ + public static Log.Page page(User user) throws Exception { + return Log.page(new HashMap<>(), user); + } + + /** + * Retrieve paged IndividualAccountAttachment.Logs + *

+ * Receive a list of up to 100 IndividualAccountAttachment.Log objects previously created in the Stark Infra API and the cursor to the next page. + * Use this function instead of query if you want to manually page your requests. + *

+ * Return: + * @return IndividualAccountAttachment.Log.Page object: + * IndividualAccountAttachment.Log.Page.logs: list of IndividualAccountAttachment.Log objects with updated attributes + * IndividualAccountAttachment.Log.Page.cursor: cursor to retrieve the next page of IndividualAccountAttachment.Log objects + * @throws Exception error in the request + */ + public static Log.Page page() throws Exception { + return Log.page(new HashMap<>(), null); + } + + /** + * Retrieve paged IndividualAccountAttachment.Logs + *

+ * Receive a list of up to 100 IndividualAccountAttachment.Log objects previously created in the Stark Infra API and the cursor to the next page. + * Use this function instead of query if you want to manually page your requests. + *

+ * Parameters: + * @param params map of parameters for the query + * cursor [string, default null]: cursor returned on the previous page function call + * limit [integer, default 100]: maximum number of objects to be retrieved. It must be an integer between 1 and 100. ex: 50 + * after [string, default null]: date filter for objects created only after specified date. ex: "2020-03-09" + * before [string, default null]: date filter for objects created only before specified date. ex: "2020-03-10" + * types [list of strings, default null]: filter retrieved objects by types. ex: "success" or "failed" + * attachmentIds [list of strings, default null]: list of IndividualAccountAttachment ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return IndividualAccountAttachment.Log.Page object: + * IndividualAccountAttachment.Log.Page.logs: list of IndividualAccountAttachment.Log objects with updated attributes + * IndividualAccountAttachment.Log.Page.cursor: cursor to retrieve the next page of IndividualAccountAttachment.Log objects + * @throws Exception error in the request + */ + public static Log.Page page(Map params, User user) throws Exception { + com.starkcore.utils.Page page = Rest.getPage(data, params, user); + List logs = new ArrayList<>(); + for (SubResource log: page.entities) { + logs.add((Log) log); + } + return new Log.Page(logs, page.cursor); + } + } +} diff --git a/src/main/java/com/starkinfra/IndividualAccountRequest.java b/src/main/java/com/starkinfra/IndividualAccountRequest.java new file mode 100644 index 0000000..3cf24e9 --- /dev/null +++ b/src/main/java/com/starkinfra/IndividualAccountRequest.java @@ -0,0 +1,776 @@ +package com.starkinfra; + +import com.starkinfra.utils.Rest; +import com.starkinfra.utils.Resource; +import com.starkinfra.utils.Generator; +import com.starkcore.utils.SubResource; +import com.starkinfra.error.ErrorElement; + +import java.util.Map; +import java.util.List; +import java.util.HashMap; +import java.util.ArrayList; + + +public final class IndividualAccountRequest extends Resource { + /** + * IndividualAccountRequest object + *

+ * IndividualAccountRequests are used to open a Stark Infra account for an individual. The + * caller submits the individual's identifying data and income, and the API runs the approval + * flow asynchronously, moving the request through created, processing, success, failed or canceled. + *

+ * When you initialize an IndividualAccountRequest, the entity will not be automatically + * created in the Stark Infra API. The 'create' function sends the objects + * to the Stark Infra API and returns the list of created objects. + *

+ * Parameters: + * name [string]: full legal name of the individual. ex: "Tony Stark" + * taxId [string]: Brazilian CPF with or without formatting. ex: "012.345.678-90" or "01234567890" + * address [IndividualAccountRequest.Address]: structured residential address. ex: new IndividualAccountRequest.Address(data) + * income [Long]: monthly income in cents. Must be greater than 0. ex: 1000000 (= R$ 10,000.00) + * tags [list of strings, default null]: list of strings for reference when searching for IndividualAccountRequests. ex: ["employees", "monthly"] + * id [string]: unique id returned when the IndividualAccountRequest is created. ex: "5189530608992256" + * status [string]: current IndividualAccountRequest status. ex: "created", "processing", "success", "failed" or "canceled" + * accountType [string]: account-request kind. Always "individual" for this resource. ex: "individual" + * flags [list of strings]: server-side review flags. Empty unless the request triggered a manual-review condition. ex: ["manualReview"] + * created [string]: creation datetime for the IndividualAccountRequest. ex: "2020-03-10 10:30:00.000000+00:00" + * updated [string]: latest update datetime for the IndividualAccountRequest. ex: "2020-03-10 10:30:00.000000+00:00" + * + */ + static ClassData data = new ClassData(IndividualAccountRequest.class, "IndividualAccountRequest"); + + public String name; + public String taxId; + public Address address; + public Long income; + public String[] tags; + public String accountType; + public String[] flags; + public String status; + public String created; + public String updated; + + /** + * IndividualAccountRequest object + *

+ * IndividualAccountRequests are used to open a Stark Infra account for an individual. + *

+ * When you initialize an IndividualAccountRequest, the entity will not be automatically + * created in the Stark Infra API. The 'create' function sends the objects + * to the Stark Infra API and returns the list of created objects. + *

+ * Parameters: + * @param name [string]: full legal name of the individual. ex: "Tony Stark" + * @param taxId [string]: Brazilian CPF with or without formatting. ex: "012.345.678-90" or "01234567890" + * @param address [IndividualAccountRequest.Address]: structured residential address. + * @param income [Long]: monthly income in cents. Must be greater than 0. ex: 1000000 (= R$ 10,000.00) + * @param tags [list of strings, default null]: list of strings for reference when searching for IndividualAccountRequests. ex: ["employees", "monthly"] + * @param id [string]: unique id returned when the IndividualAccountRequest is created. ex: "5189530608992256" + * @param status [string]: current IndividualAccountRequest status. ex: "created", "processing", "success", "failed" or "canceled" + * @param accountType [string]: account-request kind. Always "individual" for this resource. ex: "individual" + * @param flags [list of strings]: server-side review flags. ex: ["manualReview"] + * @param created [string]: creation datetime for the IndividualAccountRequest. ex: "2020-03-10 10:30:00.000000+00:00" + * @param updated [string]: latest update datetime for the IndividualAccountRequest. ex: "2020-03-10 10:30:00.000000+00:00" + */ + public IndividualAccountRequest(String name, String taxId, Address address, Long income, String[] tags, + String id, String status, String accountType, String[] flags, + String created, String updated) { + super(id); + this.name = name; + this.taxId = taxId; + this.address = address; + this.income = income; + this.tags = tags; + this.status = status; + this.accountType = accountType; + this.flags = flags; + this.created = created; + this.updated = updated; + } + + /** + * IndividualAccountRequest object + *

+ * IndividualAccountRequests are used to open a Stark Infra account for an individual. + *

+ * When you initialize an IndividualAccountRequest, the entity will not be automatically + * created in the Stark Infra API. The 'create' function sends the objects + * to the Stark Infra API and returns the list of created objects. + *

+ * Parameters (required): + * @param data map of properties for the creation of the IndividualAccountRequest + * name [string]: full legal name of the individual. ex: "Tony Stark" + * taxId [string]: Brazilian CPF with or without formatting. ex: "012.345.678-90" or "01234567890" + * address [IndividualAccountRequest.Address or map]: structured residential address. + * income [Long]: monthly income in cents. Must be greater than 0. ex: 1000000 (= R$ 10,000.00) + *

+ * Parameters (optional): + * tags [list of strings, default null]: list of strings for reference when searching for IndividualAccountRequests. ex: ["employees", "monthly"] + *

+ * Attributes (return-only): + * id [string]: unique id returned when the IndividualAccountRequest is created. ex: "5189530608992256" + * status [string]: current IndividualAccountRequest status. ex: "created", "processing", "success", "failed" or "canceled" + * accountType [string]: account-request kind. Always "individual" for this resource. ex: "individual" + * flags [list of strings]: server-side review flags. ex: ["manualReview"] + * created [string]: creation datetime for the IndividualAccountRequest. ex: "2020-03-10 10:30:00.000000+00:00" + * updated [string]: latest update datetime for the IndividualAccountRequest. ex: "2020-03-10 10:30:00.000000+00:00" + * @throws Exception error in the request + */ + @SuppressWarnings("unchecked") + public IndividualAccountRequest(Map data) throws Exception { + super(null); + HashMap dataCopy = new HashMap<>(data); + + this.name = (String) dataCopy.remove("name"); + this.taxId = (String) dataCopy.remove("taxId"); + this.address = parseAddress(dataCopy.remove("address")); + this.income = ((Number) dataCopy.remove("income")).longValue(); + this.tags = (String[]) dataCopy.remove("tags"); + this.status = null; + this.accountType = null; + this.flags = null; + this.created = null; + this.updated = null; + + if (!dataCopy.isEmpty()) { + throw new Exception("Unknown parameters used in constructor: [" + String.join(", ", dataCopy.keySet()) + "]"); + } + } + + @SuppressWarnings("unchecked") + private Address parseAddress(Object address) throws Exception { + if (address == null) + return null; + + if (address instanceof Address) { + return (Address) address; + } + + return new Address((Map) address); + } + + /** + * Retrieve a specific IndividualAccountRequest + *

+ * Receive a single IndividualAccountRequest object previously created in the Stark Infra API by passing its id + *

+ * Parameters: + * @param id [string]: object unique id. ex: "5189530608992256" + *

+ * Return: + * @return IndividualAccountRequest object with updated attributes + * @throws Exception error in the request + */ + public static IndividualAccountRequest get(String id) throws Exception { + return IndividualAccountRequest.get(id, null); + } + + /** + * Retrieve a specific IndividualAccountRequest + *

+ * Receive a single IndividualAccountRequest object previously created in the Stark Infra API by passing its id + *

+ * Parameters: + * @param id [string]: object unique id. ex: "5189530608992256" + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return IndividualAccountRequest object with updated attributes + * @throws Exception error in the request + */ + public static IndividualAccountRequest get(String id, User user) throws Exception { + return Rest.getId(data, id, user); + } + + /** + * Retrieve IndividualAccountRequests + *

+ * Receive a generator of IndividualAccountRequest objects previously created in the Stark Infra API. + * Use this function instead of page if you want to stream the objects without worrying about cursors and pagination. + *

+ * Parameters: + * @param params map of parameters for the query + * limit [integer, default null]: maximum number of objects to be retrieved. Unlimited if null. ex: 35 + * after [string, default null]: date filter for objects created only after specified date. ex: "2020-03-10" + * before [string, default null]: date filter for objects created only before specified date. ex: "2020-03-10" + * status [string, default null]: filter for status of retrieved objects. ex: "created" + * tags [list of strings, default null]: tags to filter retrieved objects. ex: ["tony", "stark"] + * ids [list of strings, default null]: list of ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + *

+ * Return: + * @return generator of IndividualAccountRequest objects with updated attributes + * @throws Exception error in the request + */ + public static Generator query(Map params) throws Exception { + return IndividualAccountRequest.query(params, null); + } + + /** + * Retrieve IndividualAccountRequests + *

+ * Receive a generator of IndividualAccountRequest objects previously created in the Stark Infra API. + * Use this function instead of page if you want to stream the objects without worrying about cursors and pagination. + *

+ * Parameters: + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return generator of IndividualAccountRequest objects with updated attributes + * @throws Exception error in the request + */ + public static Generator query(User user) throws Exception { + return IndividualAccountRequest.query(new HashMap<>(), user); + } + + /** + * Retrieve IndividualAccountRequests + *

+ * Receive a generator of IndividualAccountRequest objects previously created in the Stark Infra API. + * Use this function instead of page if you want to stream the objects without worrying about cursors and pagination. + *

+ * Return: + * @return generator of IndividualAccountRequest objects with updated attributes + * @throws Exception error in the request + */ + public static Generator query() throws Exception { + return IndividualAccountRequest.query(new HashMap<>(), null); + } + + /** + * Retrieve IndividualAccountRequests + *

+ * Receive a generator of IndividualAccountRequest objects previously created in the Stark Infra API. + * Use this function instead of page if you want to stream the objects without worrying about cursors and pagination. + *

+ * Parameters: + * @param params map of parameters for the query + * limit [integer, default null]: maximum number of objects to be retrieved. Unlimited if null. ex: 35 + * after [string, default null]: date filter for objects created only after specified date. ex: "2020-03-10" + * before [string, default null]: date filter for objects created only before specified date. ex: "2020-03-10" + * status [string, default null]: filter for status of retrieved objects. ex: "created" + * tags [list of strings, default null]: tags to filter retrieved objects. ex: ["tony", "stark"] + * ids [list of strings, default null]: list of ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return generator of IndividualAccountRequest objects with updated attributes + * @throws Exception error in the request + */ + public static Generator query(Map params, User user) throws Exception { + return Rest.getStream(data, params, user); + } + + public final static class Page { + public List requests; + public String cursor; + + public Page(List requests, String cursor) { + this.requests = requests; + this.cursor = cursor; + } + } + + /** + * Retrieve paged IndividualAccountRequests + *

+ * Receive a list of up to 100 IndividualAccountRequest objects previously created in the Stark Infra API and the cursor to the next page. + * Use this function instead of query if you want to manually page your requests. + *

+ * Parameters: + * @param params map of parameters for the query + * cursor [string, default null]: cursor returned on the previous page function call + * limit [integer, default 100]: maximum number of objects to be retrieved. It must be an integer between 1 and 100. ex: 50 + * after [string, default null]: date filter for objects created only after specified date. ex: "2020-03-10" + * before [string, default null]: date filter for objects created only before specified date. ex: "2020-03-10" + * status [string, default null]: filter for status of retrieved objects. ex: "created" + * tags [list of strings, default null]: tags to filter retrieved objects. ex: ["tony", "stark"] + * ids [list of strings, default null]: list of ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + *

+ * Return: + * @return IndividualAccountRequest.Page object: + * IndividualAccountRequest.Page.requests: list of IndividualAccountRequest objects with updated attributes + * IndividualAccountRequest.Page.cursor: cursor to retrieve the next page of IndividualAccountRequest objects + * @throws Exception error in the request + */ + public static Page page(Map params) throws Exception { + return page(params, null); + } + + /** + * Retrieve paged IndividualAccountRequests + *

+ * Receive a list of up to 100 IndividualAccountRequest objects previously created in the Stark Infra API and the cursor to the next page. + * Use this function instead of query if you want to manually page your requests. + *

+ * Parameters: + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return IndividualAccountRequest.Page object: + * IndividualAccountRequest.Page.requests: list of IndividualAccountRequest objects with updated attributes + * IndividualAccountRequest.Page.cursor: cursor to retrieve the next page of IndividualAccountRequest objects + * @throws Exception error in the request + */ + public static Page page(User user) throws Exception { + return page(new HashMap<>(), user); + } + + /** + * Retrieve paged IndividualAccountRequests + *

+ * Receive a list of up to 100 IndividualAccountRequest objects previously created in the Stark Infra API and the cursor to the next page. + * Use this function instead of query if you want to manually page your requests. + *

+ * Return: + * @return IndividualAccountRequest.Page object: + * IndividualAccountRequest.Page.requests: list of IndividualAccountRequest objects with updated attributes + * IndividualAccountRequest.Page.cursor: cursor to retrieve the next page of IndividualAccountRequest objects + * @throws Exception error in the request + */ + public static Page page() throws Exception { + return page(new HashMap<>(), null); + } + + /** + * Retrieve paged IndividualAccountRequests + *

+ * Receive a list of up to 100 IndividualAccountRequest objects previously created in the Stark Infra API and the cursor to the next page. + * Use this function instead of query if you want to manually page your requests. + *

+ * Parameters: + * @param params map of parameters for the query + * cursor [string, default null]: cursor returned on the previous page function call + * limit [integer, default 100]: maximum number of objects to be retrieved. It must be an integer between 1 and 100. ex: 50 + * after [string, default null]: date filter for objects created only after specified date. ex: "2020-03-10" + * before [string, default null]: date filter for objects created only before specified date. ex: "2020-03-10" + * status [string, default null]: filter for status of retrieved objects. ex: "created" + * tags [list of strings, default null]: tags to filter retrieved objects. ex: ["tony", "stark"] + * ids [list of strings, default null]: list of ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return IndividualAccountRequest.Page object: + * IndividualAccountRequest.Page.requests: list of IndividualAccountRequest objects with updated attributes + * IndividualAccountRequest.Page.cursor: cursor to retrieve the next page of IndividualAccountRequest objects + * @throws Exception error in the request + */ + public static Page page(Map params, User user) throws Exception { + com.starkcore.utils.Page page = Rest.getPage(data, params, user); + List requests = new ArrayList<>(); + for (SubResource request: page.entities) { + requests.add((IndividualAccountRequest) request); + } + return new Page(requests, page.cursor); + } + + /** + * Create IndividualAccountRequests + *

+ * Send a list of IndividualAccountRequest objects for creation in the Stark Infra API + *

+ * Parameters: + * @param requests [list of IndividualAccountRequest objects or HashMaps]: list of IndividualAccountRequest objects to be created in the API + *

+ * Return: + * @return list of IndividualAccountRequest objects with updated attributes + * @throws Exception error in the request + */ + public static List create(List requests) throws Exception { + return IndividualAccountRequest.create(requests, null); + } + + /** + * Create IndividualAccountRequests + *

+ * Send a list of IndividualAccountRequest objects for creation in the Stark Infra API + *

+ * Parameters: + * @param requests [list of IndividualAccountRequest objects or HashMaps]: list of IndividualAccountRequest objects to be created in the API + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return list of IndividualAccountRequest objects with updated attributes + * @throws Exception error in the request + */ + @SuppressWarnings("unchecked") + public static List create(List requests, User user) throws Exception { + List requestList = new ArrayList<>(); + for (Object request : requests){ + if (request instanceof Map){ + requestList.add(new IndividualAccountRequest((Map) request)); + continue; + } + if (request instanceof IndividualAccountRequest){ + requestList.add((IndividualAccountRequest) request); + continue; + } + throw new Exception("Unknown type \"" + request.getClass() + "\", use IndividualAccountRequest or HashMap"); + } + return Rest.post(data, requestList, user); + } + + /** + * Update IndividualAccountRequest entity + *

+ * Update an IndividualAccountRequest by passing id. + *

+ * Parameters: + * @param id [string]: IndividualAccountRequest id. ex: "5189530608992256" + * @param patchData map of parameters + * name [string, default null]: replace the legal name. ex: "Tony Stark" + * taxId [string, default null]: replace the CPF. ex: "012.345.678-90" + * address [map, default null]: replace the address as a whole object (no partial address PATCH). + * income [Long, default null]: replace monthly income in cents. ex: 1000000 + * status [string, default null]: manual state transition. ex: "processing" + * tags [list of strings, default null]: replace tag list. ex: ["employees", "monthly"] + *

+ * Return: + * @return IndividualAccountRequest object with updated attributes + * @throws Exception error in the request + */ + public static IndividualAccountRequest update(String id, Map patchData) throws Exception { + return IndividualAccountRequest.update(id, patchData, null); + } + + /** + * Update IndividualAccountRequest entity + *

+ * Update an IndividualAccountRequest by passing id. + *

+ * Parameters: + * @param id [string]: IndividualAccountRequest id. ex: "5189530608992256" + * @param patchData map of parameters + * name [string, default null]: replace the legal name. ex: "Tony Stark" + * taxId [string, default null]: replace the CPF. ex: "012.345.678-90" + * address [map, default null]: replace the address as a whole object (no partial address PATCH). + * income [Long, default null]: replace monthly income in cents. ex: 1000000 + * status [string, default null]: manual state transition. ex: "processing" + * tags [list of strings, default null]: replace tag list. ex: ["employees", "monthly"] + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return IndividualAccountRequest object with updated attributes + * @throws Exception error in the request + */ + public static IndividualAccountRequest update(String id, Map patchData, User user) throws Exception { + return Rest.patch(data, id, patchData, user); + } + + /** + * IndividualAccountRequest.Address object + *

+ * Embedded value object describing the individual's residential address. It has no endpoints + * of its own; it is exposed only as the address field on the parent and is serialized as a + * nested JSON object on the wire. + *

+ * Parameters: + * street [string]: street name. ex: "Rua do Estilo Barroco" + * number [string]: street number. ex: "648" + * neighborhood [string]: neighborhood / district. ex: "Santo Amaro" + * city [string]: city. ex: "Sao Paulo" + * state [string]: state (BR 2-letter code). ex: "SP" + * zipCode [string]: ZIP code (BR CEP) with or without formatting. ex: "05724005" + * + */ + public final static class Address extends SubResource { + public String street; + public String number; + public String neighborhood; + public String city; + public String state; + public String zipCode; + + /** + * IndividualAccountRequest.Address object + *

+ * Embedded value object describing the individual's residential address. + *

+ * Parameters: + * @param street [string]: street name. ex: "Rua do Estilo Barroco" + * @param number [string]: street number. ex: "648" + * @param neighborhood [string]: neighborhood / district. ex: "Santo Amaro" + * @param city [string]: city. ex: "Sao Paulo" + * @param state [string]: state (BR 2-letter code). ex: "SP" + * @param zipCode [string]: ZIP code (BR CEP) with or without formatting. ex: "05724005" + */ + public Address(String street, String number, String neighborhood, String city, String state, String zipCode) { + this.street = street; + this.number = number; + this.neighborhood = neighborhood; + this.city = city; + this.state = state; + this.zipCode = zipCode; + } + + /** + * IndividualAccountRequest.Address object + *

+ * Embedded value object describing the individual's residential address. + *

+ * Parameters: + * @param data map of properties for the creation of the IndividualAccountRequest.Address + * street [string]: street name. ex: "Rua do Estilo Barroco" + * number [string]: street number. ex: "648" + * neighborhood [string]: neighborhood / district. ex: "Santo Amaro" + * city [string]: city. ex: "Sao Paulo" + * state [string]: state (BR 2-letter code). ex: "SP" + * zipCode [string]: ZIP code (BR CEP) with or without formatting. ex: "05724005" + * @throws Exception error in the request + */ + public Address(Map data) throws Exception { + HashMap dataCopy = new HashMap<>(data); + + this.street = (String) dataCopy.remove("street"); + this.number = (String) dataCopy.remove("number"); + this.neighborhood = (String) dataCopy.remove("neighborhood"); + this.city = (String) dataCopy.remove("city"); + this.state = (String) dataCopy.remove("state"); + this.zipCode = (String) dataCopy.remove("zipCode"); + + if (!dataCopy.isEmpty()) { + throw new Exception("Unknown parameters used in constructor: [" + String.join(", ", dataCopy.keySet()) + "]"); + } + } + } + + public final static class Log extends Resource { + static ClassData data = new ClassData(Log.class, "IndividualAccountRequestLog"); + + public String created; + public String type; + public List errors; + public IndividualAccountRequest request; + + /** + * IndividualAccountRequest Log object + *

+ * Every time an IndividualAccountRequest entity is modified, a corresponding IndividualAccountRequest Log + * is generated for the entity. This log is never generated by the user. + *

+ * Attributes: + * @param id [string]: unique id returned when the log is created. ex: "5656565656565656" + * @param request [IndividualAccountRequest]: IndividualAccountRequest entity to which the log refers to. + * @param errors [list of ErrorElement]: list of errors linked to the IndividualAccountRequest event. + * @param type [string]: type of the IndividualAccountRequest event which triggered the log creation. ex: "processing" or "success" + * @param created [string]: creation datetime for the log. ex: "2020-03-10 10:30:00.000000+00:00" + */ + public Log(String created, String type, List errors, IndividualAccountRequest request, String id) { + super(id); + this.created = created; + this.type = type; + this.errors = errors; + this.request = request; + } + + /** + * Retrieve a specific IndividualAccountRequest Log + *

+ * Receive a single IndividualAccountRequest Log object previously created by the Stark Infra API by passing its id + *

+ * Parameters: + * @param id [string]: object unique id. ex: "5656565656565656" + *

+ * Return: + * @return IndividualAccountRequest Log object with updated attributes + * @throws Exception error in the request + */ + public static Log get(String id) throws Exception { + return Log.get(id, null); + } + + /** + * Retrieve a specific IndividualAccountRequest Log + *

+ * Receive a single IndividualAccountRequest Log object previously created by the Stark Infra API by passing its id + *

+ * Parameters: + * @param id [string]: object unique id. ex: "5656565656565656" + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return IndividualAccountRequest Log object with updated attributes + * @throws Exception error in the request + */ + public static Log get(String id, User user) throws Exception { + return Rest.getId(data, id, user); + } + + /** + * Retrieve IndividualAccountRequest Logs + *

+ * Receive a generator of IndividualAccountRequest.Log objects previously created in the Stark Infra API. + * Use this function instead of page if you want to stream the objects without worrying about cursors and pagination. + *

+ * Parameters: + * @param params map of parameters for the query + * limit [integer, default null]: maximum number of objects to be retrieved. Unlimited if null. ex: 35 + * after [string, default null]: date filter for objects created only after specified date. ex: "2020-03-09" + * before [string, default null]: date filter for objects created only before specified date. ex: "2020-03-10" + * types [list of strings, default null]: filter retrieved objects by types. ex: "success" or "failed" + * accountRequestIds [list of strings, default null]: list of IndividualAccountRequest ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + *

+ * Return: + * @return generator of IndividualAccountRequest Log objects with updated attributes + * @throws Exception error in the request + */ + public static Generator query(Map params) throws Exception { + return Log.query(params, null); + } + + /** + * Retrieve IndividualAccountRequest Logs + *

+ * Receive a generator of IndividualAccountRequest.Log objects previously created in the Stark Infra API. + * Use this function instead of page if you want to stream the objects without worrying about cursors and pagination. + *

+ * Parameters: + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return generator of IndividualAccountRequest Log objects with updated attributes + * @throws Exception error in the request + */ + public static Generator query(User user) throws Exception { + return Log.query(new HashMap<>(), user); + } + + /** + * Retrieve IndividualAccountRequest Logs + *

+ * Receive a generator of IndividualAccountRequest.Log objects previously created in the Stark Infra API. + * Use this function instead of page if you want to stream the objects without worrying about cursors and pagination. + *

+ * Return: + * @return generator of IndividualAccountRequest Log objects with updated attributes + * @throws Exception error in the request + */ + public static Generator query() throws Exception { + return Log.query(new HashMap<>(), null); + } + + /** + * Retrieve IndividualAccountRequest Logs + *

+ * Receive a generator of IndividualAccountRequest.Log objects previously created in the Stark Infra API. + * Use this function instead of page if you want to stream the objects without worrying about cursors and pagination. + *

+ * Parameters: + * @param params map of parameters for the query + * limit [integer, default null]: maximum number of objects to be retrieved. Unlimited if null. ex: 35 + * after [string, default null]: date filter for objects created only after specified date. ex: "2020-03-09" + * before [string, default null]: date filter for objects created only before specified date. ex: "2020-03-10" + * types [list of strings, default null]: filter retrieved objects by types. ex: "success" or "failed" + * accountRequestIds [list of strings, default null]: list of IndividualAccountRequest ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return generator of IndividualAccountRequest Log objects with updated attributes + * @throws Exception error in the request + */ + public static Generator query(Map params, User user) throws Exception { + return Rest.getStream(data, params, user); + } + + public final static class Page { + public List logs; + public String cursor; + + public Page(List logs, String cursor) { + this.logs = logs; + this.cursor = cursor; + } + } + + /** + * Retrieve paged IndividualAccountRequest.Logs + *

+ * Receive a list of up to 100 IndividualAccountRequest.Log objects previously created in the Stark Infra API and the cursor to the next page. + * Use this function instead of query if you want to manually page your requests. + *

+ * Parameters: + * @param params map of parameters for the query + * cursor [string, default null]: cursor returned on the previous page function call + * limit [integer, default 100]: maximum number of objects to be retrieved. It must be an integer between 1 and 100. ex: 50 + * after [string, default null]: date filter for objects created only after specified date. ex: "2020-03-09" + * before [string, default null]: date filter for objects created only before specified date. ex: "2020-03-10" + * types [list of strings, default null]: filter retrieved objects by types. ex: "success" or "failed" + * accountRequestIds [list of strings, default null]: list of IndividualAccountRequest ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + *

+ * Return: + * @return IndividualAccountRequest.Log.Page object: + * IndividualAccountRequest.Log.Page.logs: list of IndividualAccountRequest.Log objects with updated attributes + * IndividualAccountRequest.Log.Page.cursor: cursor to retrieve the next page of IndividualAccountRequest.Log objects + * @throws Exception error in the request + */ + public static Log.Page page(Map params) throws Exception { + return Log.page(params, null); + } + + /** + * Retrieve paged IndividualAccountRequest.Logs + *

+ * Receive a list of up to 100 IndividualAccountRequest.Log objects previously created in the Stark Infra API and the cursor to the next page. + * Use this function instead of query if you want to manually page your requests. + *

+ * Parameters: + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return IndividualAccountRequest.Log.Page object: + * IndividualAccountRequest.Log.Page.logs: list of IndividualAccountRequest.Log objects with updated attributes + * IndividualAccountRequest.Log.Page.cursor: cursor to retrieve the next page of IndividualAccountRequest.Log objects + * @throws Exception error in the request + */ + public static Log.Page page(User user) throws Exception { + return Log.page(new HashMap<>(), user); + } + + /** + * Retrieve paged IndividualAccountRequest.Logs + *

+ * Receive a list of up to 100 IndividualAccountRequest.Log objects previously created in the Stark Infra API and the cursor to the next page. + * Use this function instead of query if you want to manually page your requests. + *

+ * Return: + * @return IndividualAccountRequest.Log.Page object: + * IndividualAccountRequest.Log.Page.logs: list of IndividualAccountRequest.Log objects with updated attributes + * IndividualAccountRequest.Log.Page.cursor: cursor to retrieve the next page of IndividualAccountRequest.Log objects + * @throws Exception error in the request + */ + public static Log.Page page() throws Exception { + return Log.page(new HashMap<>(), null); + } + + /** + * Retrieve paged IndividualAccountRequest.Logs + *

+ * Receive a list of up to 100 IndividualAccountRequest.Log objects previously created in the Stark Infra API and the cursor to the next page. + * Use this function instead of query if you want to manually page your requests. + *

+ * Parameters: + * @param params map of parameters for the query + * cursor [string, default null]: cursor returned on the previous page function call + * limit [integer, default 100]: maximum number of objects to be retrieved. It must be an integer between 1 and 100. ex: 50 + * after [string, default null]: date filter for objects created only after specified date. ex: "2020-03-09" + * before [string, default null]: date filter for objects created only before specified date. ex: "2020-03-10" + * types [list of strings, default null]: filter retrieved objects by types. ex: "success" or "failed" + * accountRequestIds [list of strings, default null]: list of IndividualAccountRequest ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkinfra.Settings.user was set before function call + *

+ * Return: + * @return IndividualAccountRequest.Log.Page object: + * IndividualAccountRequest.Log.Page.logs: list of IndividualAccountRequest.Log objects with updated attributes + * IndividualAccountRequest.Log.Page.cursor: cursor to retrieve the next page of IndividualAccountRequest.Log objects + * @throws Exception error in the request + */ + public static Log.Page page(Map params, User user) throws Exception { + com.starkcore.utils.Page page = Rest.getPage(data, params, user); + List logs = new ArrayList<>(); + for (SubResource log: page.entities) { + logs.add((Log) log); + } + return new Log.Page(logs, page.cursor); + } + } +} diff --git a/src/test/java/TestIndividualAccountAttachment.java b/src/test/java/TestIndividualAccountAttachment.java new file mode 100644 index 0000000..8f65d98 --- /dev/null +++ b/src/test/java/TestIndividualAccountAttachment.java @@ -0,0 +1,412 @@ +import org.junit.Test; + +import com.starkinfra.Settings; +import com.starkinfra.IndividualAccountAttachment; +import com.starkinfra.IndividualAccountRequest; +import com.starkinfra.utils.Generator; +import com.starkcore.error.InputErrors; + +import java.util.Arrays; +import java.util.List; +import java.util.HashMap; +import java.util.ArrayList; +import java.util.Collections; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + + +public class TestIndividualAccountAttachment { + + @Test + public void testCreate() throws Exception { + Settings.user = utils.User.defaultProject(); + + List attachments = new ArrayList<>(); + attachments.add(example()); + + attachments = IndividualAccountAttachment.create(attachments); + + for (IndividualAccountAttachment attachment : attachments) { + assertNotNull(attachment.id); + assertNotNull(attachment.status); + assertNotNull(attachment.created); + String id = IndividualAccountAttachment.get(attachment.id).id; + assertEquals(id, attachment.id); + } + } + + @Test + public void testConstructorEncodesDataUrl() throws Exception { + Settings.user = utils.User.defaultProject(); + + IndividualAccountAttachment attachment = example(); + + assertNotNull(attachment.content); + assertTrue( + "content must be encoded as a data:;base64,... URL", + attachment.content.startsWith("data:image/png;base64,") + ); + } + + @Test + public void testContentTypeIsInputOnly() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap responseData = new HashMap<>(); + responseData.put("id", "5189530608992256"); + responseData.put("type", "identity-front"); + responseData.put("content", "data:image/png;base64,aGVsbG8="); + responseData.put("accountRequestId", "5189530608992256"); + responseData.put("status", "created"); + responseData.put("contentType", "image/png"); + + try { + new IndividualAccountAttachment(responseData); + throw new Exception("expected the constructor to reject the input-only contentType field"); + } catch (Exception e) { + assertTrue( + "expected unknown-parameter rejection mentioning contentType, got: " + e.getMessage(), + e.getMessage() != null && e.getMessage().contains("contentType") + ); + } + } + + @Test + public void testQueryGet() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap params = new HashMap<>(); + params.put("limit", 3); + params.put("status", "created"); + params.put("after", "2019-04-01"); + params.put("before", "2030-04-30"); + Generator attachments = IndividualAccountAttachment.query(params); + + int i = 0; + for (IndividualAccountAttachment attachment : attachments) { + i += 1; + attachment = IndividualAccountAttachment.get(attachment.id); + assertNotNull(attachment.id); + } + assertTrue(i > 0); + } + + @Test + public void testQueryIds() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap params = new HashMap<>(); + params.put("limit", 10); + params.put("tags", new String[]{"employees"}); + Generator attachments = IndividualAccountAttachment.query(params); + + int i = 0; + ArrayList idsExpected = new ArrayList<>(); + for (IndividualAccountAttachment attachment : attachments) { + i += 1; + assertNotNull(attachment.id); + idsExpected.add(attachment.id); + } + + params.put("ids", idsExpected.toArray(new String[0])); + Generator result = IndividualAccountAttachment.query(params); + + int n = 0; + ArrayList idsResult = new ArrayList<>(); + for (IndividualAccountAttachment attachment : result) { + n += 1; + assertNotNull(attachment.id); + idsResult.add(attachment.id); + } + + Collections.sort(idsExpected); + Collections.sort(idsResult); + assertTrue(i > 0); + assertTrue(n > 0); + assertEquals(idsExpected, idsResult); + } + + @Test + public void testPage() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap params = new HashMap<>(); + params.put("limit", 2); + params.put("after", "2019-04-01"); + params.put("before", "2030-04-30"); + params.put("cursor", null); + + List ids = new ArrayList<>(); + for (int i = 0; i < 2; i++) { + IndividualAccountAttachment.Page page = IndividualAccountAttachment.page(params); + for (IndividualAccountAttachment attachment : page.attachments) { + if (ids.contains(attachment.id)) { + throw new Exception("repeated id"); + } + ids.add(attachment.id); + } + if (page.cursor == null) { + break; + } + params.put("cursor", page.cursor); + } + + if (ids.size() != 4) { + throw new Exception("ids.size() != 4"); + } + } + + @Test + public void testCancel() throws Exception { + Settings.user = utils.User.defaultProject(); + + IndividualAccountAttachment created = + IndividualAccountAttachment.create(Collections.singletonList(example())).get(0); + + IndividualAccountAttachment canceled = IndividualAccountAttachment.cancel(created.id); + assertEquals("deleted", canceled.status); + } + + @Test + public void testLogQueryAndGet() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap params = new HashMap<>(); + params.put("limit", 3); + params.put("after", "2019-04-01"); + params.put("before", "2030-04-30"); + params.put("types", new String[]{"created"}); + Generator logs = IndividualAccountAttachment.Log.query(params); + + int i = 0; + for (IndividualAccountAttachment.Log log : logs) { + i += 1; + log = IndividualAccountAttachment.Log.get(log.id); + assertNotNull(log.id); + assertNotNull(log.attachment.id); + } + assertTrue(i > 0); + } + + @Test + public void testLogQueryAttachmentIds() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap params = new HashMap<>(); + params.put("limit", 5); + Generator logs = IndividualAccountAttachment.Log.query(params); + + ArrayList attachmentIds = new ArrayList<>(); + for (IndividualAccountAttachment.Log log : logs) { + attachmentIds.add(log.attachment.id); + } + + HashMap filterParams = new HashMap<>(); + filterParams.put("limit", 5); + filterParams.put("attachmentIds", attachmentIds.toArray(new String[0])); + Generator filtered = IndividualAccountAttachment.Log.query(filterParams); + + int i = 0; + for (IndividualAccountAttachment.Log log : filtered) { + i += 1; + assertNotNull(log.id); + assertTrue(attachmentIds.contains(log.attachment.id)); + } + assertTrue(i > 0); + } + + @Test + public void testLogPage() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap params = new HashMap<>(); + params.put("limit", 2); + params.put("after", "2019-04-01"); + params.put("before", "2030-04-30"); + params.put("cursor", null); + + List ids = new ArrayList<>(); + for (int i = 0; i < 2; i++) { + IndividualAccountAttachment.Log.Page page = IndividualAccountAttachment.Log.page(params); + for (IndividualAccountAttachment.Log log : page.logs) { + if (ids.contains(log.id)) { + throw new Exception("repeated id"); + } + ids.add(log.id); + } + if (page.cursor == null) { + break; + } + params.put("cursor", page.cursor); + } + + if (ids.size() != 4) { + throw new Exception("ids.size() != 4"); + } + } + + @Test + public void testTypeEnum() throws Exception { + Settings.user = utils.User.defaultProject(); + + List allowed = Arrays.asList( + "drivers-license-front", "drivers-license-back", + "identity-front", "identity-back" + ); + + HashMap params = new HashMap<>(); + params.put("limit", 5); + Generator attachments = IndividualAccountAttachment.query(params); + + int i = 0; + for (IndividualAccountAttachment attachment : attachments) { + i += 1; + assertTrue("unexpected type: " + attachment.type, allowed.contains(attachment.type)); + } + assertTrue(i > 0); + } + + @Test + public void testDateTimeParsing() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap params = new HashMap<>(); + params.put("limit", 1); + Generator attachments = IndividualAccountAttachment.query(params); + + int i = 0; + for (IndividualAccountAttachment attachment : attachments) { + i += 1; + assertNotNull(attachment.created); + } + assertTrue(i > 0); + } + + @Test + public void testCreateInvalidType() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap data = exampleData(); + data.put("type", "not-a-real-type"); + + List attachments = new ArrayList<>(); + attachments.add(new IndividualAccountAttachment(data)); + + boolean raised = false; + try { + IndividualAccountAttachment.create(attachments); + } catch (InputErrors e) { + raised = true; + } + assertTrue("expected InputErrors to be raised", raised); + } + + @Test + public void testCreateInvalidContent() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap data = exampleData(); + data.put("content", new byte[]{}); + + List attachments = new ArrayList<>(); + attachments.add(new IndividualAccountAttachment(data)); + + boolean raised = false; + try { + IndividualAccountAttachment.create(attachments); + } catch (InputErrors e) { + raised = true; + } + assertTrue("expected InputErrors to be raised", raised); + } + + @Test + public void testCreateInvalidContentType() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap data = exampleData(); + data.remove("contentType"); + + List attachments = new ArrayList<>(); + attachments.add(new IndividualAccountAttachment(data)); + + boolean raised = false; + try { + IndividualAccountAttachment.create(attachments); + } catch (InputErrors e) { + raised = true; + } + assertTrue("expected InputErrors to be raised", raised); + } + + @Test + public void testCreateAccountRequestNotFound() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap data = exampleData(); + data.put("accountRequestId", "0"); + + List attachments = new ArrayList<>(); + attachments.add(new IndividualAccountAttachment(data)); + + boolean raised = false; + try { + IndividualAccountAttachment.create(attachments); + } catch (InputErrors e) { + raised = true; + } + assertTrue("expected InputErrors to be raised", raised); + } + + @Test + public void testCancelIdempotent() throws Exception { + Settings.user = utils.User.defaultProject(); + + IndividualAccountAttachment attachment = example(); + List created = + IndividualAccountAttachment.create(Collections.singletonList(attachment)); + String id = created.get(0).id; + + IndividualAccountAttachment firstCancel = IndividualAccountAttachment.cancel(id); + assertEquals("deleted", firstCancel.status); + + IndividualAccountAttachment secondCancel = IndividualAccountAttachment.cancel(id); + assertEquals("deleted", secondCancel.status); + } + + @Test + public void testGetNotFound() throws Exception { + Settings.user = utils.User.defaultProject(); + + boolean raised = false; + try { + IndividualAccountAttachment.get("0"); + } catch (InputErrors e) { + raised = true; + } + assertTrue("expected InputErrors to be raised", raised); + } + + static IndividualAccountAttachment example() throws Exception { + return new IndividualAccountAttachment(exampleData()); + } + + static HashMap exampleData() throws Exception { + IndividualAccountRequest parent = TestIndividualAccountRequest.example(); + List created = + IndividualAccountRequest.create(Collections.singletonList(parent)); + String accountRequestId = created.get(0).id; + + byte[] content = new byte[]{(byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}; + + HashMap data = new HashMap<>(); + data.put("type", "identity-front"); + data.put("content", content); + data.put("contentType", "image/png"); + data.put("accountRequestId", accountRequestId); + data.put("tags", new String[]{"employees"}); + return data; + } +} diff --git a/src/test/java/TestIndividualAccountRequest.java b/src/test/java/TestIndividualAccountRequest.java new file mode 100644 index 0000000..38e6de7 --- /dev/null +++ b/src/test/java/TestIndividualAccountRequest.java @@ -0,0 +1,450 @@ +import org.junit.Test; + +import com.starkinfra.Settings; +import com.starkinfra.IndividualAccountRequest; +import com.starkinfra.utils.Generator; +import com.starkcore.error.InputErrors; + +import java.util.Arrays; +import java.util.List; +import java.util.UUID; +import java.util.HashMap; +import java.util.ArrayList; +import java.util.Collections; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + + +public class TestIndividualAccountRequest { + + @Test + public void testCreate() throws Exception { + Settings.user = utils.User.defaultProject(); + + List requests = new ArrayList<>(); + requests.add(example()); + + requests = IndividualAccountRequest.create(requests); + + for (IndividualAccountRequest request : requests) { + assertNotNull(request.id); + assertNotNull(request.status); + assertEquals("individual", request.accountType); + assertNotNull(request.created); + assertNotNull(request.updated); + String id = IndividualAccountRequest.get(request.id).id; + assertEquals(id, request.id); + } + } + + @Test + public void testCreateWithObjectAddress() throws Exception { + Settings.user = utils.User.defaultProject(); + + IndividualAccountRequest request = example(); + + assertNotNull(request.address); + assertEquals("Rua do Estilo Barroco", request.address.street); + assertEquals("648", request.address.number); + assertEquals("Santo Amaro", request.address.neighborhood); + assertEquals("Sao Paulo", request.address.city); + assertEquals("SP", request.address.state); + assertEquals("05724005", request.address.zipCode); + } + + @Test + public void testQueryGet() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap params = new HashMap<>(); + params.put("limit", 3); + Generator requests = IndividualAccountRequest.query(params); + + int i = 0; + for (IndividualAccountRequest request : requests) { + i += 1; + request = IndividualAccountRequest.get(request.id); + assertNotNull(request.id); + } + assertTrue(i > 0); + } + + @Test + public void testQueryIds() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap params = new HashMap<>(); + params.put("limit", 10); + params.put("tags", new String[]{"employees", "monthly"}); + Generator requests = IndividualAccountRequest.query(params); + + int i = 0; + ArrayList requestsIdsExpected = new ArrayList<>(); + for (IndividualAccountRequest request : requests) { + i += 1; + assertNotNull(request.id); + requestsIdsExpected.add(request.id); + } + + params.put("ids", requestsIdsExpected.toArray(new String[0])); + Generator requestsResult = IndividualAccountRequest.query(params); + + int n = 0; + ArrayList requestsIdsResult = new ArrayList<>(); + for (IndividualAccountRequest request : requestsResult) { + n += 1; + assertNotNull(request.id); + requestsIdsResult.add(request.id); + } + + assertTrue(i > 0); + assertTrue(n > 0); + for (String id : requestsIdsResult) { + assertTrue( + "ids filter returned an id outside the requested set: " + id, + requestsIdsExpected.contains(id) + ); + } + } + + @Test + public void testPage() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap params = new HashMap<>(); + params.put("limit", 2); + params.put("after", "2019-04-01"); + params.put("before", "2030-04-30"); + params.put("cursor", null); + + List ids = new ArrayList<>(); + for (int i = 0; i < 2; i++) { + IndividualAccountRequest.Page page = IndividualAccountRequest.page(params); + for (IndividualAccountRequest request : page.requests) { + if (ids.contains(request.id)) { + throw new Exception("repeated id"); + } + ids.add(request.id); + } + if (page.cursor == null) { + break; + } + params.put("cursor", page.cursor); + } + + if (ids.size() != 4) { + throw new Exception("ids.size() != 4"); + } + } + + @Test + public void testUpdate() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap patchData = new HashMap<>(); + patchData.put("name", "Tony Stark"); + + HashMap params = new HashMap<>(); + params.put("limit", 1); + Generator requests = IndividualAccountRequest.query(params); + for (IndividualAccountRequest request : requests) { + IndividualAccountRequest updated = IndividualAccountRequest.update(request.id, patchData); + assertEquals("Tony Stark", updated.name); + assertEquals(request.id, updated.id); + } + } + + @Test + public void testUpdateAddress() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap newAddress = new HashMap<>(); + newAddress.put("street", "Avenida Paulista"); + newAddress.put("number", "1000"); + newAddress.put("neighborhood", "Bela Vista"); + newAddress.put("city", "Sao Paulo"); + newAddress.put("state", "SP"); + newAddress.put("zipCode", "01310100"); + + HashMap patchData = new HashMap<>(); + patchData.put("address", newAddress); + + HashMap params = new HashMap<>(); + params.put("limit", 1); + Generator requests = IndividualAccountRequest.query(params); + for (IndividualAccountRequest request : requests) { + IndividualAccountRequest updated = IndividualAccountRequest.update(request.id, patchData); + assertEquals("Avenida Paulista", updated.address.street); + assertEquals("01310100", updated.address.zipCode); + } + } + + @Test + public void testStatusEnum() throws Exception { + Settings.user = utils.User.defaultProject(); + + List allowed = Arrays.asList("approved", "created", "denied", "processing", "updated"); + + HashMap params = new HashMap<>(); + params.put("limit", 5); + Generator requests = IndividualAccountRequest.query(params); + + int i = 0; + for (IndividualAccountRequest request : requests) { + i += 1; + assertNotNull(request.status); + assertTrue("unexpected status: " + request.status, allowed.contains(request.status)); + } + assertTrue(i > 0); + } + + @Test + public void testLogQueryAndGet() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap params = new HashMap<>(); + params.put("limit", 3); + params.put("after", "2019-04-01"); + params.put("before", "2030-04-30"); + params.put("types", new String[]{"created"}); + Generator logs = IndividualAccountRequest.Log.query(params); + + int i = 0; + for (IndividualAccountRequest.Log log : logs) { + i += 1; + log = IndividualAccountRequest.Log.get(log.id); + assertNotNull(log.id); + assertNotNull(log.request.id); + } + assertTrue(i > 0); + } + + @Test + public void testLogQueryAccountRequestIds() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap params = new HashMap<>(); + params.put("limit", 5); + Generator logs = IndividualAccountRequest.Log.query(params); + + ArrayList accountRequestIds = new ArrayList<>(); + for (IndividualAccountRequest.Log log : logs) { + accountRequestIds.add(log.request.id); + } + + HashMap filterParams = new HashMap<>(); + filterParams.put("limit", 5); + filterParams.put("accountRequestIds", accountRequestIds.toArray(new String[0])); + Generator filtered = IndividualAccountRequest.Log.query(filterParams); + + int i = 0; + for (IndividualAccountRequest.Log log : filtered) { + i += 1; + assertNotNull(log.id); + assertTrue(accountRequestIds.contains(log.request.id)); + } + assertTrue(i > 0); + } + + @Test + public void testLogPage() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap params = new HashMap<>(); + params.put("limit", 2); + params.put("after", "2019-04-01"); + params.put("before", "2030-04-30"); + params.put("cursor", null); + + List ids = new ArrayList<>(); + for (int i = 0; i < 2; i++) { + IndividualAccountRequest.Log.Page page = IndividualAccountRequest.Log.page(params); + for (IndividualAccountRequest.Log log : page.logs) { + if (ids.contains(log.id)) { + throw new Exception("repeated id"); + } + ids.add(log.id); + } + if (page.cursor == null) { + break; + } + params.put("cursor", page.cursor); + } + + if (ids.size() != 4) { + throw new Exception("ids.size() != 4"); + } + } + + @Test + public void testDateTimeParsing() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap params = new HashMap<>(); + params.put("limit", 1); + Generator requests = IndividualAccountRequest.query(params); + + int i = 0; + for (IndividualAccountRequest request : requests) { + i += 1; + assertNotNull(request.created); + assertNotNull(request.updated); + } + assertTrue(i > 0); + } + + @Test + public void testOutputOnlyFieldsRejectedByConstructor() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap data = exampleData(); + data.put("status", "created"); + data.put("accountType", "individual"); + + try { + new IndividualAccountRequest(data); + throw new Exception("expected the constructor to reject the return-only fields"); + } catch (Exception e) { + assertTrue( + "expected unknown-parameter rejection, got: " + e.getMessage(), + e.getMessage() != null + && e.getMessage().startsWith("Unknown parameters used in constructor:") + ); + } + } + + @Test + public void testCreateInvalidName() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap data = exampleData(); + data.put("name", ""); + + List requests = new ArrayList<>(); + requests.add(new IndividualAccountRequest(data)); + + boolean raised = false; + try { + IndividualAccountRequest.create(requests); + } catch (InputErrors e) { + raised = true; + } + assertTrue("expected InputErrors to be raised", raised); + } + + @Test + public void testCreateInvalidTaxId() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap data = exampleData(); + data.put("taxId", "000.000.000-00"); + + List requests = new ArrayList<>(); + requests.add(new IndividualAccountRequest(data)); + + boolean raised = false; + try { + IndividualAccountRequest.create(requests); + } catch (InputErrors e) { + raised = true; + } + assertTrue("expected InputErrors to be raised", raised); + } + + @Test + public void testCreateInvalidAddress() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap incompleteAddress = new HashMap<>(); + incompleteAddress.put("street", "Rua do Estilo Barroco"); + + HashMap data = exampleData(); + data.put("address", incompleteAddress); + + List requests = new ArrayList<>(); + requests.add(new IndividualAccountRequest(data)); + + boolean raised = false; + try { + IndividualAccountRequest.create(requests); + } catch (InputErrors e) { + raised = true; + } + assertTrue("expected InputErrors to be raised", raised); + } + + @Test + public void testCreateInvalidIncome() throws Exception { + Settings.user = utils.User.defaultProject(); + + HashMap data = exampleData(); + data.put("income", -1L); + + List requests = new ArrayList<>(); + requests.add(new IndividualAccountRequest(data)); + + boolean raised = false; + try { + IndividualAccountRequest.create(requests); + } catch (InputErrors e) { + raised = true; + } + assertTrue("expected InputErrors to be raised", raised); + } + + @Test + public void testUpdateInvalidStatus() throws Exception { + Settings.user = utils.User.defaultProject(); + + IndividualAccountRequest request = + IndividualAccountRequest.create(Collections.singletonList(example())).get(0); + + HashMap patchData = new HashMap<>(); + patchData.put("status", "not-a-real-status"); + + boolean raised = false; + try { + IndividualAccountRequest.update(request.id, patchData); + } catch (InputErrors e) { + raised = true; + } + assertTrue("expected InputErrors to be raised", raised); + } + + @Test + public void testGetNotFound() throws Exception { + Settings.user = utils.User.defaultProject(); + + boolean raised = false; + try { + IndividualAccountRequest.get("0"); + } catch (InputErrors e) { + raised = true; + } + assertTrue("expected InputErrors to be raised", raised); + } + + static IndividualAccountRequest example() throws Exception { + return new IndividualAccountRequest(exampleData()); + } + + static HashMap exampleData() throws Exception { + HashMap address = new HashMap<>(); + address.put("street", "Rua do Estilo Barroco"); + address.put("number", "648"); + address.put("neighborhood", "Santo Amaro"); + address.put("city", "Sao Paulo"); + address.put("state", "SP"); + address.put("zipCode", "05724005"); + + HashMap data = new HashMap<>(); + data.put("name", "Tony Stark " + UUID.randomUUID().toString()); + data.put("taxId", "012.345.678-90"); + data.put("address", address); + data.put("income", 1000000L); + data.put("tags", new String[]{"employees", "monthly"}); + return data; + } +}