Search in sources :

Example 6 with TokenType

use of org.collectiveone.modules.tokens.TokenType in project CollectiveOneWebapp by CollectiveOne.

the class InitiativeService method createNewToken.

@Transactional
public PostResult createNewToken(UUID initiativeId, AssetsDto tokenDto, UUID userId) {
    Initiative initiative = initiativeRepository.findById(initiativeId);
    TokenType token = tokenService.create(tokenDto.getAssetName(), "T");
    initiative.getTokenTypes().add(token);
    initiative = initiativeRepository.save(initiative);
    tokenService.mintToHolder(token.getId(), initiative.getId(), tokenDto.getOwnedByThisHolder(), TokenHolderType.INITIATIVE);
    AppUser orderedBy = appUserRepository.findByC1Id(userId);
    TokenMint mint = new TokenMint();
    mint.setToken(token);
    mint.setOrderedBy(orderedBy);
    mint.setToHolder(initiativeId);
    mint.setMotive("Token creation.");
    mint.setNotes("");
    mint.setValue(tokenDto.getOwnedByThisHolder());
    mint = tokenMintRespository.save(mint);
    activityService.newTokenCreated(initiative, orderedBy, token, mint);
    return new PostResult("success", "initiative created and tokens created", initiative.getId().toString());
}
Also used : PostResult(org.collectiveone.common.dto.PostResult) TokenType(org.collectiveone.modules.tokens.TokenType) TokenMint(org.collectiveone.modules.tokens.TokenMint) AppUser(org.collectiveone.modules.users.AppUser) Transactional(javax.transaction.Transactional)

Example 7 with TokenType

use of org.collectiveone.modules.tokens.TokenType in project CollectiveOneWebapp by CollectiveOne.

the class AssignationService method createAssignation.

public PostResult createAssignation(UUID initiativeId, AssignationDto assignationDto, UUID creatorId) {
    Initiative initiative = initiativeRepository.findById(initiativeId);
    Assignation assignation = new Assignation();
    assignation.setType(AssignationType.valueOf(assignationDto.getType()));
    assignation.setMotive(assignationDto.getMotive());
    assignation.setNotes(assignationDto.getNotes());
    assignation.setCreator(appUserRepository.findByC1Id(creatorId));
    assignation.setCreationDate(new Timestamp(System.currentTimeMillis()));
    assignation.setState(AssignationState.valueOf(assignationDto.getConfig().getStartState()));
    assignation.setInitiative(initiative);
    assignation.getAlsoInInitiatives().addAll(initiativeService.getParentGenealogyInitiatives(initiative.getId()));
    AssignationConfig config = new AssignationConfig();
    config.setSelfBiasVisible(Boolean.valueOf(assignationDto.getConfig().getSelfBiasVisible()));
    config.setEvaluationsVisible(Boolean.valueOf(assignationDto.getConfig().getEvaluationsVisible()));
    config.setDurationDays(assignationDto.getConfig().getMaxDuration());
    if (assignation.getState() == AssignationState.OPEN) {
        config.setMinClosureDate(new Timestamp(System.currentTimeMillis()));
        config.setMaxClosureDate(new Timestamp(System.currentTimeMillis() + assignationDto.getConfig().getMaxDuration() * DAYS_TO_MS));
    }
    config = assignationConfigRepository.save(config);
    assignation.setConfig(config);
    assignation = assignationRepository.save(assignation);
    for (ReceiverDto receiverDto : assignationDto.getReceivers()) {
        Receiver receiver = new Receiver();
        receiver.setUser(appUserRepository.findByC1Id(UUID.fromString(receiverDto.getUser().getC1Id())));
        receiver.setAssignation(assignation);
        receiver.setAssignedPercent(receiverDto.getPercent());
        receiver.setState(ReceiverState.PENDING);
        if (receiverDto.getIsDonor()) {
            receiver.setType(ReceiverType.DONOR);
        } else {
            receiver.setType(ReceiverType.NORMAL);
        }
        receiver = receiverRepository.save(receiver);
        assignation.getReceivers().add(receiver);
    }
    for (BillDto billDto : assignationDto.getAssets()) {
        TokenType tokenType = tokenService.getTokenType(UUID.fromString(billDto.getAssetId()));
        Bill bill = new Bill();
        bill.setAssignation(assignation);
        bill.setTokenType(tokenType);
        bill.setValue(billDto.getValue());
        bill = billRepository.save(bill);
        assignation.getBills().add(bill);
    }
    switch(assignation.getType()) {
        case DIRECT:
            for (Bill bill : assignation.getBills()) {
                for (Receiver receiver : assignation.getReceivers()) {
                    double value = bill.getValue() * receiver.getAssignedPercent() / 100.0;
                    PostResult result = tokenTransferService.transferFromInitiativeToUser(assignation.getInitiative().getId(), receiver.getUser().getC1Id(), bill.getTokenType().getId(), value);
                    if (result.getResult().equals("success")) {
                        receiver.setState(ReceiverState.RECEIVED);
                        receiver.setTransfer(memberTransferRepository.findById(UUID.fromString(result.getElementId())));
                        receiverRepository.save(receiver);
                    }
                }
            }
            assignation.setState(AssignationState.DONE);
            assignationRepository.save(assignation);
            activityService.directAssignationCreated(assignation, appUserRepository.findByC1Id(creatorId));
            break;
        case PEER_REVIEWED:
            for (EvaluatorDto evaluatorDto : assignationDto.getEvaluators()) {
                Evaluator evaluator = new Evaluator();
                evaluator.setUser(appUserRepository.findByC1Id(UUID.fromString(evaluatorDto.getUser().getC1Id())));
                evaluator.setAssignation(assignation);
                evaluator.setState(EvaluatorState.PENDING);
                /* Weight logic TBD */
                evaluator.setWeight(1.0);
                evaluator = evaluatorRepository.save(evaluator);
                /* Create the grades of all evaluators already */
                for (Receiver receiver : assignation.getReceivers()) {
                    EvaluationGrade grade = new EvaluationGrade();
                    grade.setAssignation(assignation);
                    grade.setEvaluator(evaluator);
                    grade.setReceiver(receiver);
                    grade.setPercent(0.0);
                    grade.setType(EvaluationGradeType.SET);
                    grade.setState(EvaluationGradeState.PENDING);
                    grade = evaluationGradeRepository.save(grade);
                    evaluator.getGrades().add(grade);
                }
                assignation.getEvaluators().add(evaluator);
            }
            activityService.peerReviewedAssignationCreated(assignation, appUserRepository.findByC1Id(creatorId));
            break;
    }
    return new PostResult("success", "success", assignation.getId().toString());
}
Also used : ReceiverDto(org.collectiveone.modules.assignations.dto.ReceiverDto) PeerReviewedAssignation(org.collectiveone.modules.assignations.evaluationlogic.PeerReviewedAssignation) Initiative(org.collectiveone.modules.initiatives.Initiative) Timestamp(java.sql.Timestamp) PostResult(org.collectiveone.common.dto.PostResult) TokenType(org.collectiveone.modules.tokens.TokenType) BillDto(org.collectiveone.modules.assignations.dto.BillDto) EvaluatorDto(org.collectiveone.modules.assignations.dto.EvaluatorDto)

Aggregations

TokenType (org.collectiveone.modules.tokens.TokenType)7 Transactional (javax.transaction.Transactional)3 PostResult (org.collectiveone.common.dto.PostResult)3 TokenMint (org.collectiveone.modules.tokens.TokenMint)3 Timestamp (java.sql.Timestamp)2 ArrayList (java.util.ArrayList)2 Initiative (org.collectiveone.modules.initiatives.Initiative)2 InitiativeDto (org.collectiveone.modules.initiatives.dto.InitiativeDto)2 InitiativeTransfer (org.collectiveone.modules.tokens.InitiativeTransfer)2 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Assignation (org.collectiveone.modules.assignations.Assignation)1 Evaluator (org.collectiveone.modules.assignations.Evaluator)1 Receiver (org.collectiveone.modules.assignations.Receiver)1 BillDto (org.collectiveone.modules.assignations.dto.BillDto)1 EvaluatorDto (org.collectiveone.modules.assignations.dto.EvaluatorDto)1 ReceiverDto (org.collectiveone.modules.assignations.dto.ReceiverDto)1 PeerReviewedAssignation (org.collectiveone.modules.assignations.evaluationlogic.PeerReviewedAssignation)1 DecisionMaker (org.collectiveone.modules.governance.DecisionMaker)1 InitiativeMembersDto (org.collectiveone.modules.initiatives.dto.InitiativeMembersDto)1