private Response Fund(UserPledge pledge, UnixUtcTime through)
        {
            var pledgeAmount = pledge.AmountAt(Clock.UnixUtcNow);

            if (pledgeAmount == 0 || !through.IsAfter(pledge.FundedThrough))
            {
                return(Response.Success());
            }

            var currencyAmount = _settings.RatePerUnit * pledgeAmount;
            var funded         = _accounts.Apply(new TransferRequest(pledge.UserId, _settings.TargetAccount, $"Funded Treasury Pledge - {pledgeAmount} Units", currencyAmount));

            if (!funded.Succeeded)
            {
                return(Response.Errored(ResponseStatus.InvalidState, $"Unable to fund pledge for {pledge.UserId}: {funded.ErrorMessage}"));
            }

            var fundedThrough = pledge.FundedThrough.Plus(_settings.Frequency);

            _eventStore.Commit(new PledgeFundedThrough(pledge.UserId, pledgeAmount, fundedThrough).ToEvent());
            if (through.IsAfter(fundedThrough))
            {
                return(Fund(Get(pledge.UserId), through));
            }

            return(Response.Success());
        }
 public MarkNotCompletedTasksDaily(UnixUtcTime firstExecution, TaskInstances taskInstances)
     : base(firstExecution, TimeSpan.FromDays(1),
            () => taskInstances
            .ActiveItemsDueBefore(Clock.UnixUtcNow.Minus(TimeSpan.FromDays(2)))
            .Select(x => taskInstances.Apply(
                        new MarkTaskNotComplete { Id = x.Id, At = Clock.UnixUtcNow, ApproverUserId = new ServiceUser() })))
 {
 }
Esempio n. 3
0
 public Event(string entityType, string entityId, string name, int version, string jsonPayload, UnixUtcTime occurredAt)
 {
     EntityType  = entityType;
     EntityId    = entityId;
     Name        = name;
     Version     = version;
     JsonPayload = jsonPayload;
     OccurredAt  = occurredAt;
 }
 public void Init()
 {
     Clock.Freeze();
     _now         = Clock.UnixUtcNow;
     _tasks       = new Tasks(new InMemoryEntityStore <TaskRecord>());
     _users       = new Users(new InMemoryEntityStore <UserRecord>());
     _settings    = new AssignmentSettings();
     _assignments = new TaskAssignments(new InMemoryEventStore(), _tasks, _users, _settings);
 }
 public void Init()
 {
     Clock.Freeze();
     _now           = Clock.UnixUtcNow;
     _users         = new SampleUsers();
     _tasks         = new SampleTasks();
     _assignments   = new SampleTaskAssignments(_tasks.Tasks, _users.Users);
     _messages      = new Messages();
     _taskInstances = new TaskInstances(new InMemoryTaskInstanceStore(), _assignments.Assignments, _messages);
 }
Esempio n. 6
0
        public void Init()
        {
            Clock.Freeze();
            _now = Clock.UnixUtcNow;
            var eventStore = new InMemoryEventStore();

            _settings    = new PledgeFundingSettings();
            _accounts    = new Accounts(eventStore);
            _sampleUsers = new SampleUsers();
            _pledges     = new Pledges(eventStore, _sampleUsers.Users, _accounts, _settings);
            _accounts.Apply(new TransactionRequest(_sampleUsers.User1, "Deposit", User1StartBalance));
            User1 = _sampleUsers.User1;
        }
        public void Put(string id, TaskInstanceRecord obj)
        {
            var sql = @"UPDATE HomeTask.TaskInstances SET 
                            Id = @id,
                            Description = @description,
                            Status = @status,	
                            TaskId = @taskId,
                            UserId = @userId,
                            Due = @due,
                            Price = @price,
	                        IsFunded = @isFunded,
                            FundedOn = @fundedOn,
	                        FundedByUserId = @fundedByUserId,
                            UpdatedStatusAt = @updatedStatusAt,
	                        UpdatedStatusByUserId = @updatedStatusByUserId
                        WHERE Id = @id;

                        IF @@ROWCOUNT = 0
                        BEGIN
                            INSERT INTO HomeTask.TaskInstances (Id, Description, Status, TaskId, UserId, Due, Price, 
                                IsFunded, FundedOn, FundedByUserId, UpdatedStatusAt, UpdatedStatusByUserId)
                            VALUES (@id, @description, @status, @taskId, @userId, @due, @price, 
                                @isFunded, @fundedOn, @fundedByUserId, @updatedStatusAt, @updatedStatusByUserId)
                        END";

            _db.Execute(sql, new
            {
                id                    = obj.Id,
                description           = obj.Description,
                status                = obj.Status.ToString(),
                taskId                = obj.TaskId,
                userId                = obj.UserId,
                due                   = UnixUtcTime.ToDateTime(obj.Due),
                price                 = obj.Price,
                isFunded              = obj.IsFunded,
                fundedOn              = UnixUtcTime.ToDateTime(obj.FundedOn),
                fundedByUserId        = obj.FundedByUserId,
                updatedStatusAt       = UnixUtcTime.ToDateTime(obj.UpdatedStatusAt),
                updatedStatusByUserId = obj.UpdatedStatusByUserId
            });
        }
        public void Commit(IEnumerable <Event> events)
        {
            var sql = $@"INSERT INTO {_tableName} (EntityType, EntityId, Name, Version, JsonPayload, OccurredAt)
                values(@entityType, @entityId, @name, @version, @jsonPayload, @occurredAt)";

            var items = events.Select(x => new InsertEvent
            {
                EntityId    = x.EntityId,
                EntityType  = x.EntityType,
                Name        = x.Name,
                Version     = x.Version,
                JsonPayload = x.JsonPayload,
                OccurredAt  = UnixUtcTime.ToDateTime(x.OccurredAt)
            });

            _db.UsingConnection(x =>
            {
                var t = x.BeginTransaction();
                x.Execute(sql, items, t);
                t.Commit();
            });
        }
        public IEnumerable <ProposedTaskInstance> FutureInstancesThrough(UnixUtcTime time)
        {
            var taskDuration = TimeSpan.FromDays((int)_task.Frequency);
            var from         = _task.Frequency == TaskFrequency.Weekly
                ? Clock.UnixUtcNow.Next(_settings.WeekEndDeadline)
                : Clock.UnixUtcNow;

            return(from
                   .StartOfDay()
                   .Every(taskDuration)
                   .Where(x => x.IsAfter(Clock.UnixUtcNow))
                   .Until(time)
                   .Select(due => new ProposedTaskInstance
            {
                TaskDescription = $"{_task.Name}",
                TaskId = _task.Id,
                UserId = _assignedUsers.At(time),
                Price = _rates.GetInstanceRate(_task),
                Start = due.Plus(_settings.TaskInstanceDeadlineUtcOffset).Minus(taskDuration),
                Due = due.Plus(_settings.TaskInstanceDeadlineUtcOffset)
            }));
        }
 public string ToUser(UnixUtcTime time)
 {
     return(_assignedUsers.At(time));
 }
Esempio n. 11
0
 public AssignTaskRequest(string taskId, string userId, UnixUtcTime assignmentStart)
 {
     TaskId   = taskId;
     UserId   = userId;
     StartsAt = assignmentStart;
 }
Esempio n. 12
0
 public IEnumerable <TaskInstanceRecord> ActiveItemsDueBefore(UnixUtcTime time)
 {
     return(_store.GetAll().Where(x => x.Status == TaskInstanceStatus.Scheduled && x.Due.IsBefore(time)));
 }
Esempio n. 13
0
 public string At(UnixUtcTime time)
 {
     return(_assignedUsers.Where(x => !x.Key.IsAfter(time))
            .Select(x => x.Value)
            .LastOrDefault(new DefaultUser()));
 }
Esempio n. 14
0
 public int AmountAt(UnixUtcTime time)
 {
     return(_pledgeAmounts.At(time));
 }
 public ScheduleWorkItemsThrough(UnixUtcTime through)
 {
     Through = through;
 }
Esempio n. 16
0
 public int GetInstanceRate(UserPledge pledge, UnixUtcTime time)
 {
     return(pledge.AmountAt(time) * _usd);
 }
Esempio n. 17
0
 public FundPledgesDaily(UnixUtcTime firstExecution, Pledges pledges)
     : base(firstExecution, TimeSpan.FromDays(1),
            () => pledges.Apply(new FundPledges(Clock.UnixUtcNow.Plus(TimeSpan.FromDays(1)))))
 {
 }
Esempio n. 18
0
 public SetPledge(string userId, int amount, UnixUtcTime startsAt)
 {
     UserId   = userId;
     Amount   = amount;
     StartsAt = startsAt;
 }
 public static Response IsNotPast(this UnixUtcTime time)
 {
     return(time.IsBefore(Clock.UnixUtcNow)
         ? Response.Errored(ResponseStatus.InvalidState, "Cannot occur at past point in time.")
         : Response.Success());
 }
 public FundPledges(UnixUtcTime through)
 {
     Through = through;
 }
        public static TaskInstanceRecord New(string taskId, string taskDescription, string userId, UnixUtcTime due, int price)
        {
            return(new TaskInstanceRecord
            {
                Status = TaskInstanceStatus.Scheduled,
                Description = taskDescription,
                TaskId = taskId,
                UserId = userId,
                Due = due,
                Price = price,

                IsFunded = false,
                FundedOn = new UnixUtcTime(0),
                FundedByUserId = new DefaultUser(),

                UpdatedStatusByUserId = new DefaultUser(),
                UpdatedStatusAt = new UnixUtcTime(0),
            });
        }
 public UnixUtcRange(UnixUtcTime start, UnixUtcTime end)
 {
     Start = start;
     End   = end;
 }
Esempio n. 23
0
 public ScheduleTasksDaily(UnixUtcTime firstExecution, TaskInstances taskInstances)
     : base(firstExecution, TimeSpan.FromDays(1),
            () => taskInstances.Apply(new ScheduleWorkItemsThrough(Clock.UnixUtcNow.Plus(TimeSpan.FromDays(1)))))
 {
 }