Search in sources :

Example 6 with TagApiException

use of org.killbill.billing.util.api.TagApiException in project killbill by killbill.

the class AccountResource method deleteTags.

@TimedResource
@DELETE
@Path("/{accountId:" + UUID_PATTERN + "}/" + TAGS)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Remove tags from account")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied or account does not have a default payment method (AUTO_PAY_OFF tag only)") })
public Response deleteTags(@PathParam(ID_PARAM_NAME) final String id, @QueryParam(QUERY_TAGS) final String tagList, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final HttpServletRequest request) throws TagApiException, AccountApiException {
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    // Look if there is an AUTO_PAY_OFF for that account and check if the account has a default paymentMethod
    // If not we can't remove the AUTO_PAY_OFF tag
    final Collection<UUID> tagDefinitionUUIDs = getTagDefinitionUUIDs(tagList);
    boolean isTagAutoPayOff = false;
    for (final UUID cur : tagDefinitionUUIDs) {
        if (cur.equals(ControlTagType.AUTO_PAY_OFF.getId())) {
            isTagAutoPayOff = true;
            break;
        }
    }
    final UUID accountId = UUID.fromString(id);
    if (isTagAutoPayOff) {
        final Account account = accountUserApi.getAccountById(accountId, callContext);
        if (account.getPaymentMethodId() == null) {
            throw new TagApiException(ErrorCode.TAG_CANNOT_BE_REMOVED, ControlTagType.AUTO_PAY_OFF, " the account does not have a default payment method");
        }
    }
    return super.deleteTags(UUID.fromString(id), tagList, callContext);
}
Also used : Account(org.killbill.billing.account.api.Account) TagApiException(org.killbill.billing.util.api.TagApiException) UUID(java.util.UUID) CallContext(org.killbill.billing.util.callcontext.CallContext) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) TimedResource(org.killbill.commons.metrics.TimedResource) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 7 with TagApiException

use of org.killbill.billing.util.api.TagApiException in project killbill by killbill.

the class AccountResource method deleteAccountTags.

@TimedResource
@DELETE
@Path("/{accountId:" + UUID_PATTERN + "}/" + TAGS)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Remove tags from account")
@ApiResponses(value = { @ApiResponse(code = 204, message = "Successful operation"), @ApiResponse(code = 400, message = "Invalid account id supplied or account does not have a default payment method (AUTO_PAY_OFF tag only)") })
public Response deleteAccountTags(@PathParam(ID_PARAM_NAME) final UUID accountId, @QueryParam(QUERY_TAG) final List<UUID> tagList, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final HttpServletRequest request) throws TagApiException, AccountApiException {
    final CallContext callContext = context.createCallContextWithAccountId(accountId, createdBy, reason, comment, request);
    // Look if there is an AUTO_PAY_OFF for that account and check if the account has a default paymentMethod
    // If not we can't remove the AUTO_PAY_OFF tag
    boolean isTagAutoPayOff = false;
    for (final UUID cur : tagList) {
        if (cur.equals(ControlTagType.AUTO_PAY_OFF.getId())) {
            isTagAutoPayOff = true;
            break;
        }
    }
    if (isTagAutoPayOff) {
        final Account account = accountUserApi.getAccountById(accountId, callContext);
        if (account.getPaymentMethodId() == null) {
            throw new TagApiException(ErrorCode.TAG_CANNOT_BE_REMOVED, ControlTagType.AUTO_PAY_OFF, " the account does not have a default payment method");
        }
    }
    return super.deleteTags(accountId, tagList, callContext);
}
Also used : Account(org.killbill.billing.account.api.Account) TagApiException(org.killbill.billing.util.api.TagApiException) UUID(java.util.UUID) CallContext(org.killbill.billing.util.callcontext.CallContext) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) TimedResource(org.killbill.commons.metrics.TimedResource) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 8 with TagApiException

use of org.killbill.billing.util.api.TagApiException in project killbill by killbill.

the class DefaultTagUserApi method addTag.

@Override
public void addTag(final UUID objectId, final ObjectType objectType, final UUID tagDefinitionId, final CallContext context) throws TagApiException {
    if (SystemTags.isSystemTag(tagDefinitionId)) {
        // TODO Create a proper ErrorCode instaed
        throw new IllegalStateException(String.format("Failed to add tag for tagDefinitionId='%s': System tags are reserved for the system.", tagDefinitionId));
    }
    final InternalCallContext internalContext = internalCallContextFactory.createInternalCallContext(objectId, objectType, context);
    final TagModelDao tag = new TagModelDao(context.getCreatedDate(), tagDefinitionId, objectId, objectType);
    try {
        tagDao.create(tag, internalContext);
    } catch (TagApiException e) {
        // Be lenient here and make the addTag method idempotent
        if (ErrorCode.TAG_ALREADY_EXISTS.getCode() != e.getCode()) {
            throw e;
        }
    }
}
Also used : TagApiException(org.killbill.billing.util.api.TagApiException) TagModelDao(org.killbill.billing.util.tag.dao.TagModelDao) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext)

Aggregations

TagApiException (org.killbill.billing.util.api.TagApiException)8 UUID (java.util.UUID)6 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 Consumes (javax.ws.rs.Consumes)2 DELETE (javax.ws.rs.DELETE)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 LocalDate (org.joda.time.LocalDate)2 ObjectType (org.killbill.billing.ObjectType)2 Account (org.killbill.billing.account.api.Account)2 AccountApiException (org.killbill.billing.account.api.AccountApiException)2 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)2 Invoice (org.killbill.billing.invoice.api.Invoice)2 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)2 BillingEventSet (org.killbill.billing.junction.BillingEventSet)2 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)2 CallContext (org.killbill.billing.util.callcontext.CallContext)2 DescriptiveTag (org.killbill.billing.util.tag.DescriptiveTag)2