public async Task persists_successfully()
        {
            BadgeLogRepo repo         = new(CreateTemporaryDatabase());
            string       badgeId      = ObjectId.GenerateNewId().ToString();
            const string badgeLogType = "type";
            const string userId       = "user";
            Instant      timestamp    = Instant.FromUnixTimeSeconds(123);

            // persist to db
            IDictionary <string, object?> data = new Dictionary <string, object?> {
                ["some"] = "data"
            };
            BadgeLog written = await repo.Log(badgeId, badgeLogType, userId, timestamp, data);

            Assert.That(written.BadgeId, Is.EqualTo(badgeId));
            Assert.That(written.BadgeLogType, Is.EqualTo(badgeLogType));
            Assert.That(written.UserId, Is.EqualTo(userId));
            Assert.That(written.Timestamp, Is.EqualTo(timestamp));
            Assert.That(written.AdditionalData, Is.EqualTo(data));
            Assert.NotNull(written.Id);

            // read from db
            List <BadgeLog> allItems = await repo.Collection.Find(FilterDefinition <BadgeLog> .Empty).ToListAsync();

            Assert.That(allItems.Count, Is.EqualTo(1));
            BadgeLog read = allItems[0];

            Assert.That(read, Is.EqualTo(written));
            Assert.That(read.BadgeId, Is.EqualTo(badgeId));
            Assert.That(read.BadgeLogType, Is.EqualTo(badgeLogType));
            Assert.That(read.UserId, Is.EqualTo(userId));
            Assert.That(read.Timestamp, Is.EqualTo(timestamp));
            Assert.That(read.AdditionalData, Is.EqualTo(data));
        }
Exemplo n.º 2
0
        public async Task <BadgeLog> LogWithSession(
            string badgeId, string badgeLogType, string?userId, Instant timestamp,
            IDictionary <string, object?>?additionalData = null,
            IClientSessionHandle?session = null)
        {
            var item = new BadgeLog(string.Empty, badgeId, badgeLogType, userId, timestamp,
                                    additionalData ?? ImmutableDictionary <string, object?> .Empty);

            if (session != null)
            {
                await Collection.InsertOneAsync(session, item);
            }
            else
            {
                await Collection.InsertOneAsync(item);
            }
            Debug.Assert(item.Id.Length > 0, "The MongoDB driver injected a generated ID");
            return(item);
        }
Exemplo n.º 3
0
        public async Task AddBadgeToUserAsync(int badgeTypeId, string employeeId, int organizationId)
        {
            var alreadyExists = await _badgeLogsDbSet
                                .AnyAsync(x => x.BadgeTypeId == badgeTypeId && x.OrganizationId == organizationId && x.EmployeeId == employeeId);

            if (alreadyExists)
            {
                throw new ValidationException(ErrorCodes.DuplicatesIntolerable,
                                              $"Badge type (ID {badgeTypeId}), employee (ID {employeeId}) and organization (ID {organizationId}) relationship already exists");
            }

            var entity = new BadgeLog
            {
                BadgeTypeId    = badgeTypeId,
                EmployeeId     = employeeId,
                OrganizationId = organizationId
            };

            _badgeLogsDbSet.Add(entity);

            await SaveChangesAsync();
        }