public async Task Handle(GiveTriggerCommand command)
        {
            var giverId       = new PersonIdentifier(command.GiverId);
            var receiverId    = new PersonIdentifier(command.ReceiverId);
            var giverTreasury = await _treasuryRepository.GetByPersonIdentifier(giverId);

            var receiverTreasury = await _treasuryRepository.GetByPersonIdentifier(receiverId);

            var trigger = new Trigger(
                Description: command.Description,
                Money: new Money(command.Amount, (Currency)Enum.Parse(typeof(Currency), command.Currency)),
                Giver: giverId,
                Receiver: receiverId,
                MoneyType: (TreasuryMoneyType)Enum.Parse(typeof(TreasuryMoneyType), command.MoneyType));

            if (!giverTreasury.CanPayTrigger(trigger))
            {
                throw new DomainException($"{giverId.Id} cannot pay trigger");
            }
            giverTreasury.PayTrigger(trigger);
            receiverTreasury.ReceiveTrigger(trigger);

            await _treasuryRepository.Update(giverTreasury);

            await _treasuryRepository.Update(receiverTreasury);

            await _triggerRepository.Save(trigger);
        }
Exemplo n.º 2
0
        public async Task Handle(SetupAllCompanyTriggerOrderCommand command)
        {
            var companyId  = new CompanyIdentifier(command.CompanyId);
            var currency   = (Currency)Enum.Parse(typeof(Currency), command.Currency);
            var treasuries = await _treasuryRepository.GetTreasuriesByCompany(companyId);

            var company = await _companyRepository.GetCompanyById(companyId);

            var triggerOrder = new TriggerOrder(
                description: command.Description,
                companyIdentifier: companyId,
                currency: currency);
            var moneyPerPerson = new Money(Decimal.Round(command.Amount / treasuries.Count, 2), currency);

            foreach (var treasury in treasuries)
            {
                var trigger = new Trigger(
                    Description: $"Part of \"{command.Description}\" order",
                    Money: moneyPerPerson,
                    Giver: company.BigBoss,
                    Receiver: treasury.PersonIdentifier,
                    TreasuryMoneyType.Giving);
                triggerOrder.AddTrigger(trigger);
                treasury.ReceiveTrigger(trigger);
            }
            if (!company.CanPay(triggerOrder.Billing))
            {
                throw new DomainException($"{company.Identifier.Id} cannot pay for trigger order");
            }
            company.Pay(triggerOrder.Billing);

            await _companyRepository.Update(company);

            await _triggerOrderRepository.Save(triggerOrder);

            foreach (var treasury in treasuries)
            {
                await _treasuryRepository.Update(treasury);
            }
        }