public async Task PrepareSync() { using (ApplicationDbContext dbContext = ApplicationDbContext.Create()) { this.DropboxModel = dbContext.DropboxAccounts.Where(d => d.UID == this.UserId).FirstOrDefault(); if (this.DropboxModel != null) { string accountId = DropboxModel.AspNetUID; this.ExactOnlineModel = dbContext.ExactOnlineAccounts.Where(d => d.AspNetUID == accountId).FirstOrDefault(); if (this.ExactOnlineModel != null) { try { TimeSpan time = this.ExactOnlineModel.AccessTokenExpirationUtc.Subtract(DateTime.UtcNow); if (time.Minutes < 1) { OAuth2ResponseExact response = await new ExactOnlineService().RefreshTokenAsync(this.ExactOnlineModel.RefreshToken, this.ExactOnlineModel.AccessToken); if (response != null) { this.ExactOnlineModel.AccessTokenExpirationUtc = DateTime.UtcNow.Add(new TimeSpan(0, 0, (response.ExpiresIn - 120))); this.ExactOnlineModel.AccessToken = response.AccessToken; this.ExactOnlineModel.TokenType = response.TokenType; this.ExactOnlineModel.ExpiresIn = response.ExpiresIn; this.ExactOnlineModel.RefreshToken = response.RefreshToken; dbContext.Entry(this.ExactOnlineModel).State = EntityState.Modified; await dbContext.SaveChangesAsync(); } } } catch (Exception ex) { AuditLogService.Log(Level.Error, EventType.Exception, EventAction.PrepareSyncAction, String.Format("UID: {0} - {1}", this.UserId, ex.Message)); } } else { await AuditLogService.LogAsync(Level.Error, EventType.Exception, EventAction.PrepareSyncAction, String.Format("UID: {0} - ExactOnline Integration Settings Not Found", this.UserId)); } } else { await AuditLogService.LogAsync(Level.Error, EventType.Exception, EventAction.PrepareSyncAction, String.Format("UID: {0} - Dropbox Integration Settings Not Found", this.UserId)); } } }
public ExactSyncService(DropboxAccountModel dropboxModel = null, ExactOnlineAccountModel exactOnlineModel = null) { this.DropboxModel = dropboxModel; this.ExactOnlineModel = exactOnlineModel; }
public void PostSync() { if (this.DropboxModel != null) { this.DropboxModel = null; } if (this.ExactOnlineModel != null) { this.ExactOnlineModel = null; } }