コード例 #1
0
        public async Task LogSettlement(long userId, DaoSettlement daoSettlement)
        {
            dynamic historyEntry = new ExpandoObject();

            // Money settled
            historyEntry.Money = daoSettlement.Amount;

            // Direction of settlement:
            historyEntry.From = daoSettlement.From;
            historyEntry.To   = daoSettlement.To;

            // Log
            await LogHistory(userId, daoSettlement.GroupId, new long[] { daoSettlement.From, daoSettlement.To }, DaoLogType.Type.CREATE, DaoLogSubType.Type.SETTLEMENT, historyEntry);
        }
コード例 #2
0
        public async Task DebtSettlement(long userId, long debtorId, long lenderId, long groupId)
        {
            if (userId != debtorId && userId != lenderId)
            {
                throw new ResourceForbiddenException("user_not_in_transaction");
            }

            if (!(await GroupService.GetGroupOfUser(userId, groupId))
                .Members.Any(x => (userId == lenderId && x.UserId == debtorId) ||
                             (userId == debtorId && x.UserId == lenderId)))
            {
                throw new ResourceForbiddenException("lender_or_debtor_not_member");
            }

            var optDeb = Context.OptimizedDebt
                         .FirstOrDefault(x => x.GroupId == groupId && x.UserOwesId == debtorId && x.UserOwedId == lenderId);

            if (optDeb == null)
            {
                throw new ResourceGoneException("debt");
            }

            if (optDeb.OweAmount == 0)
            {
                throw new BusinessException("debt_already_payed");
            }

            DaoSettlement settlement = new DaoSettlement()
            {
                GroupId = groupId,
                From    = debtorId,
                To      = lenderId,
                Amount  = optDeb.OweAmount
            };

            await Context.Settlements.AddAsync(settlement);

            await OptimizedService.OptimizeForSettling(groupId, lenderId, debtorId);

            await HistoryService.LogSettlement(userId, settlement);

            await Context.SaveChangesAsync();
        }
コード例 #3
0
ファイル: GroupService.cs プロジェクト: hrvthp/mshare
        public async Task DebtSettlement(long userId, long lenderId, long groupId)
        {
            var group = _context.Groups.SingleOrDefault(s => s.Id == groupId);

            if (group == null)
            {
                throw new Exceptions.ResourceNotFoundException("group_not_found");
            }

            if (group.Members == null)
            {
                group.Members = new List <DaoUsersGroupsMap>();
            }

            var member = _context.UsersGroupsMap.FirstOrDefault(x => x.UserId == userId && x.GroupId == groupId);

            if (member == null)
            {
                throw new Exceptions.ResourceForbiddenException("debter_not_group_member");
            }

            member = _context.UsersGroupsMap.FirstOrDefault(x => x.UserId == lenderId && x.GroupId == groupId);

            if (member == null)
            {
                throw new Exceptions.ResourceForbiddenException("lender_not_group_member");
            }

            using (var transaction = _context.Database.BeginTransaction()) {
                try {
                    // Log the previous spending here
                    var spendings = await _spendingService.GetSpendingsForGroup(groupId);

                    await _loggingService.LogForGroup(userId, groupId, spendings);

                    var optDeb = _context.OptimizedDebt.FirstOrDefault(x => x.GroupId == groupId && x.UserOwesId == userId && x.UserOwedId == lenderId);

                    if (optDeb == null)
                    {
                        throw new Exceptions.ResourceGoneException("debt_gone");
                    }

                    if (optDeb.OweAmount == 0)
                    {
                        throw new Exceptions.BusinessException("debt_already_payed");
                    }

                    DaoSettlement settlement = new DaoSettlement()
                    {
                        GroupId = groupId,
                        From    = userId,
                        To      = lenderId,
                        Amount  = optDeb.OweAmount
                    };

                    await _context.Settlements.AddAsync(settlement);

                    if (await _context.SaveChangesAsync() != 1)
                    {
                        throw new Exceptions.DatabaseException("debt_not_settled");
                    }

                    transaction.Commit();
                } catch {
                    transaction.Rollback();
                    throw;
                }
            }
        }