DocSpace-client/common/ASC.VoipService/Dao/VoipDao.cs

365 lines
13 KiB
C#
Raw Normal View History

2019-06-06 14:41:16 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
2019-12-11 14:30:28 +00:00
2019-09-09 12:56:33 +00:00
using ASC.Core;
2019-09-19 15:55:44 +00:00
using ASC.Core.Common;
2019-06-06 14:41:16 +00:00
using ASC.Core.Common.Configuration;
2019-12-11 14:30:28 +00:00
using ASC.Core.Common.EF;
using ASC.Core.Common.EF.Context;
using ASC.Core.Common.EF.Model;
2019-08-15 12:04:42 +00:00
using ASC.Core.Tenants;
using ASC.VoipService.Twilio;
2019-06-06 14:41:16 +00:00
namespace ASC.VoipService.Dao
{
public class VoipDao : AbstractDao
{
2019-12-11 14:30:28 +00:00
public VoipDao(int tenantID, DbContextManager<VoipDbContext> dbOptions, AuthContext authContext, TenantUtil tenantUtil, SecurityContext securityContext, BaseCommonLinkUtility baseCommonLinkUtility)
2019-10-21 13:50:20 +00:00
: base(dbOptions, tenantID)
2019-06-06 14:41:16 +00:00
{
2019-09-09 12:56:33 +00:00
AuthContext = authContext;
2019-09-17 12:42:32 +00:00
TenantUtil = tenantUtil;
SecurityContext = securityContext;
2019-09-19 15:55:44 +00:00
BaseCommonLinkUtility = baseCommonLinkUtility;
2019-06-06 14:41:16 +00:00
}
public virtual VoipPhone SaveOrUpdateNumber(VoipPhone phone)
{
if (!string.IsNullOrEmpty(phone.Number))
{
phone.Number = phone.Number.TrimStart('+');
}
2019-12-11 14:30:28 +00:00
var voipNumber = new VoipNumber
2019-06-06 14:41:16 +00:00
{
2019-12-11 14:30:28 +00:00
Id = phone.Id,
Number = phone.Number,
Alias = phone.Alias,
Settings = phone.Settings.ToString(),
TenantId = TenantID
};
2019-06-06 14:41:16 +00:00
2019-12-11 14:30:28 +00:00
VoipDbContext.VoipNumbers.Add(voipNumber);
VoipDbContext.SaveChanges();
2019-06-06 14:41:16 +00:00
return phone;
}
public virtual void DeleteNumber(string phoneId = "")
{
2019-12-11 14:30:28 +00:00
var number = VoipDbContext.VoipNumbers.Where(r => r.Id == phoneId && r.TenantId == TenantID).FirstOrDefault();
VoipDbContext.VoipNumbers.Remove(number);
VoipDbContext.SaveChanges();
2019-06-06 14:41:16 +00:00
}
2019-12-11 14:30:28 +00:00
public virtual IEnumerable<VoipPhone> GetNumbers(params string[] ids)
2019-06-06 14:41:16 +00:00
{
2019-12-11 14:30:28 +00:00
var numbers = VoipDbContext.VoipNumbers.Where(r => r.TenantId == TenantID);
2019-06-06 14:41:16 +00:00
2019-08-15 15:08:40 +00:00
if (ids.Any())
{
2019-12-11 14:30:28 +00:00
numbers = numbers.Where(r => ids.Any(a => a == r.Number || a == r.Id));
2019-06-06 14:41:16 +00:00
}
2019-08-15 15:08:40 +00:00
2019-12-11 14:30:28 +00:00
return numbers.ToList().ConvertAll(ToPhone);
2019-06-06 14:41:16 +00:00
}
public VoipPhone GetNumber(string id)
{
return GetNumbers(id.TrimStart('+')).FirstOrDefault();
}
public virtual VoipPhone GetCurrentNumber()
{
return GetNumbers().FirstOrDefault(r => r.Caller != null);
}
public VoipCall SaveOrUpdateCall(VoipCall call)
{
2019-12-11 14:30:28 +00:00
var voipCall = new DbVoipCall
2019-06-06 14:41:16 +00:00
{
2019-12-11 14:30:28 +00:00
TenantId = TenantID,
Id = call.Id,
NumberFrom = call.From,
NumberTo = call.To,
ContactId = call.ContactId
};
2019-06-06 14:41:16 +00:00
2019-12-11 14:30:28 +00:00
using (var db = GetDb())
{
2019-06-06 14:41:16 +00:00
if (!string.IsNullOrEmpty(call.ParentID))
{
2019-12-11 14:30:28 +00:00
voipCall.ParentCallId = call.ParentID;
2019-06-06 14:41:16 +00:00
}
if (call.Status.HasValue)
{
2019-12-11 14:30:28 +00:00
voipCall.Status = (int)call.Status.Value;
2019-06-06 14:41:16 +00:00
}
if (!call.AnsweredBy.Equals(Guid.Empty))
{
2019-12-11 14:30:28 +00:00
voipCall.AnsweredBy = call.AnsweredBy;
2019-06-06 14:41:16 +00:00
}
if (call.DialDate == DateTime.MinValue)
{
call.DialDate = DateTime.UtcNow;
}
2019-12-11 14:30:28 +00:00
voipCall.DialDate = TenantUtil.DateTimeToUtc(call.DialDate);
2019-06-06 14:41:16 +00:00
if (call.DialDuration > 0)
{
2019-12-11 14:30:28 +00:00
voipCall.DialDuration = call.DialDuration;
2019-06-06 14:41:16 +00:00
}
2019-12-11 14:30:28 +00:00
2019-08-15 13:16:39 +00:00
if (call.Price > decimal.Zero)
2019-06-06 14:41:16 +00:00
{
2019-12-11 14:30:28 +00:00
voipCall.Price = call.Price;
2019-06-06 14:41:16 +00:00
}
if (call.VoipRecord != null)
{
if (!string.IsNullOrEmpty(call.VoipRecord.Id))
{
2019-12-11 14:30:28 +00:00
voipCall.RecordSid = call.VoipRecord.Id;
2019-06-06 14:41:16 +00:00
}
2019-12-11 14:30:28 +00:00
2019-06-06 14:41:16 +00:00
if (!string.IsNullOrEmpty(call.VoipRecord.Uri))
{
2019-12-11 14:30:28 +00:00
voipCall.RecordUrl = call.VoipRecord.Uri;
2019-06-06 14:41:16 +00:00
}
if (call.VoipRecord.Duration != 0)
{
2019-12-11 14:30:28 +00:00
voipCall.RecordDuration = call.VoipRecord.Duration;
2019-06-06 14:41:16 +00:00
}
2019-08-15 13:00:20 +00:00
if (call.VoipRecord.Price != default)
2019-06-06 14:41:16 +00:00
{
2019-12-11 14:30:28 +00:00
voipCall.RecordPrice = call.VoipRecord.Price;
2019-06-06 14:41:16 +00:00
}
}
2019-12-11 14:30:28 +00:00
VoipDbContext.VoipCalls.Add(voipCall);
VoipDbContext.SaveChanges();
2019-06-06 14:41:16 +00:00
}
return call;
}
public IEnumerable<VoipCall> GetCalls(VoipCallFilter filter)
{
2019-08-15 15:08:40 +00:00
var query = GetCallsQuery(filter);
2019-06-06 14:41:16 +00:00
2019-08-15 15:08:40 +00:00
if (filter.SortByColumn != null)
{
query.OrderBy(filter.SortByColumn, filter.SortOrder);
}
2019-06-06 14:41:16 +00:00
2019-12-11 14:30:28 +00:00
query = query.Skip((int)filter.Offset);
query = query.Take((int)filter.Max * 3);
2019-06-06 14:41:16 +00:00
2019-12-11 14:30:28 +00:00
var calls = query.ToList().ConvertAll(ToCall);
2019-06-06 14:41:16 +00:00
2019-08-15 15:08:40 +00:00
calls = calls.GroupJoin(calls, call => call.Id, h => h.ParentID, (call, h) =>
{
call.ChildCalls.AddRange(h);
return call;
}).Where(r => string.IsNullOrEmpty(r.ParentID)).ToList();
2019-06-06 14:41:16 +00:00
2019-08-15 15:08:40 +00:00
return calls;
2019-06-06 14:41:16 +00:00
}
public VoipCall GetCall(string id)
{
2019-08-15 12:04:42 +00:00
return GetCalls(new VoipCallFilter { Id = id }).FirstOrDefault();
2019-06-06 14:41:16 +00:00
}
public int GetCallsCount(VoipCallFilter filter)
{
2019-12-11 14:30:28 +00:00
return GetCallsQuery(filter).Where(r => r.DbVoipCall.ParentCallId == "").Count();
2019-06-06 14:41:16 +00:00
}
public IEnumerable<VoipCall> GetMissedCalls(Guid agent, long count = 0, DateTime? from = null)
{
2019-08-15 15:08:40 +00:00
using var db = GetDb();
var query = GetCallsQuery(new VoipCallFilter { Agent = agent, SortBy = "date", SortOrder = true, Type = "missed" });
2019-06-06 14:41:16 +00:00
2019-08-15 15:08:40 +00:00
if (from.HasValue)
{
2019-12-11 14:30:28 +00:00
query = query.Where(r => r.DbVoipCall.DialDate >= TenantUtil.DateTimeFromUtc(from.Value));
2019-08-15 15:08:40 +00:00
}
2019-06-06 14:41:16 +00:00
2019-08-15 15:08:40 +00:00
if (count != 0)
{
2019-12-11 14:30:28 +00:00
query = query.Take((int)count);
2019-08-15 15:08:40 +00:00
}
2019-06-06 14:41:16 +00:00
2019-12-11 14:30:28 +00:00
var a = query.Select(ca => new
{
dbVoipCall = ca,
tmpDate = VoipDbContext.VoipCalls
.Where(tmp => tmp.TenantId == ca.DbVoipCall.TenantId)
.Where(tmp => tmp.NumberFrom == ca.DbVoipCall.NumberFrom || tmp.NumberTo == ca.DbVoipCall.NumberFrom)
.Where(tmp => tmp.Status <= (int)VoipCallStatus.Missed)
.Max(tmp => tmp.DialDate)
}).Where(r => r.dbVoipCall.DbVoipCall.DialDate >= r.tmpDate || r.tmpDate == default);
return a.ToList().ConvertAll(r => ToCall(r.dbVoipCall));
2019-06-06 14:41:16 +00:00
}
2019-12-11 14:30:28 +00:00
private IQueryable<CallContact> GetCallsQuery(VoipCallFilter filter)
2019-06-06 14:41:16 +00:00
{
2019-12-11 14:30:28 +00:00
var q = VoipDbContext.VoipCalls
.Where(r => r.TenantId == TenantID);
2019-06-06 14:41:16 +00:00
if (!string.IsNullOrEmpty(filter.Id))
{
2019-12-11 14:30:28 +00:00
q = q.Where(r => r.Id == filter.Id || r.ParentCallId == filter.Id);
2019-06-06 14:41:16 +00:00
}
if (filter.ContactID.HasValue)
{
2019-12-11 14:30:28 +00:00
q = q.Where(r => r.ContactId == filter.ContactID.Value);
2019-06-06 14:41:16 +00:00
}
if (!string.IsNullOrWhiteSpace(filter.SearchText))
{
2019-12-11 14:30:28 +00:00
q = q.Where(r => r.Id.StartsWith(filter.SearchText));
2019-06-06 14:41:16 +00:00
}
if (filter.TypeStatus.HasValue)
{
2019-12-11 14:30:28 +00:00
q = q.Where(r => r.Status == filter.TypeStatus.Value);
2019-06-06 14:41:16 +00:00
}
if (filter.FromDate.HasValue)
{
2019-12-11 14:30:28 +00:00
q = q.Where(r => r.DialDate >= filter.FromDate.Value);
2019-06-06 14:41:16 +00:00
}
if (filter.ToDate.HasValue)
{
2019-12-11 14:30:28 +00:00
q = q.Where(r => r.DialDate <= filter.ToDate.Value);
2019-06-06 14:41:16 +00:00
}
if (filter.Agent.HasValue)
{
2019-12-11 14:30:28 +00:00
q = q.Where(r => r.AnsweredBy == filter.Agent.Value);
2019-06-06 14:41:16 +00:00
}
2019-12-11 14:30:28 +00:00
return q
.GroupBy(r => r.Id, r => r)
.Join(
VoipDbContext.CrmContact.DefaultIfEmpty(),
r => r.FirstOrDefault().ContactId,
c => c.Id,
(call, contact) => new CallContact { DbVoipCall = call.FirstOrDefault(), CrmContact = contact })
;
}
class CallContact
{
public DbVoipCall DbVoipCall { get; set; }
public CrmContact CrmContact { get; set; }
2019-06-06 14:41:16 +00:00
}
#region Converters
2019-12-11 14:30:28 +00:00
private VoipPhone ToPhone(VoipNumber r)
2019-06-06 14:41:16 +00:00
{
2019-09-20 15:53:27 +00:00
return GetProvider(AuthContext, TenantUtil, SecurityContext, BaseCommonLinkUtility).GetPhone(r);
2019-06-06 14:41:16 +00:00
}
2019-12-11 14:30:28 +00:00
private VoipCall ToCall(CallContact dbVoipCall)
2019-06-06 14:41:16 +00:00
{
var call = new VoipCall
2019-08-15 12:04:42 +00:00
{
2019-12-11 14:30:28 +00:00
Id = dbVoipCall.DbVoipCall.Id,
ParentID = dbVoipCall.DbVoipCall.ParentCallId,
From = dbVoipCall.DbVoipCall.NumberFrom,
To = dbVoipCall.DbVoipCall.NumberTo,
AnsweredBy = dbVoipCall.DbVoipCall.AnsweredBy,
DialDate = TenantUtil.DateTimeFromUtc(dbVoipCall.DbVoipCall.DialDate),
DialDuration = dbVoipCall.DbVoipCall.DialDuration,
Price = dbVoipCall.DbVoipCall.Price,
Status = (VoipCallStatus)dbVoipCall.DbVoipCall.Status,
2019-08-15 12:04:42 +00:00
VoipRecord = new VoipRecord
2019-06-06 14:41:16 +00:00
{
2019-12-11 14:30:28 +00:00
Id = dbVoipCall.DbVoipCall.RecordSid,
Uri = dbVoipCall.DbVoipCall.RecordUrl,
Duration = dbVoipCall.DbVoipCall.RecordDuration,
Price = dbVoipCall.DbVoipCall.RecordPrice
2019-08-15 12:04:42 +00:00
},
2019-12-11 14:30:28 +00:00
ContactId = dbVoipCall.CrmContact.Id,
ContactIsCompany = dbVoipCall.CrmContact.IsCompany,
2019-08-15 12:04:42 +00:00
};
2019-06-06 14:41:16 +00:00
if (call.ContactId != 0)
{
call.ContactTitle = call.ContactIsCompany
2019-12-11 14:30:28 +00:00
? dbVoipCall.CrmContact.CompanyName
: dbVoipCall.CrmContact.FirstName == null || dbVoipCall.CrmContact.LastName == null ? null : string.Format("{0} {1}", dbVoipCall.CrmContact.FirstName, dbVoipCall.CrmContact.LastName);
2019-06-06 14:41:16 +00:00
}
return call;
}
public static Consumer Consumer
{
get { return ConsumerFactory.GetByName("twilio"); }
}
2019-09-20 15:53:27 +00:00
public static TwilioProvider GetProvider(AuthContext authContext, TenantUtil tenantUtil, SecurityContext securityContext, BaseCommonLinkUtility baseCommonLinkUtility)
2019-06-06 14:41:16 +00:00
{
2019-09-20 15:53:27 +00:00
return new TwilioProvider(Consumer["twilioAccountSid"], Consumer["twilioAuthToken"], authContext, tenantUtil, securityContext, baseCommonLinkUtility);
2019-06-06 14:41:16 +00:00
}
public static bool ConfigSettingsExist
{
get
{
return !string.IsNullOrEmpty(Consumer["twilioAccountSid"]) &&
!string.IsNullOrEmpty(Consumer["twilioAuthToken"]);
}
}
2019-09-09 12:56:33 +00:00
public AuthContext AuthContext { get; }
2019-09-17 12:42:32 +00:00
public TenantUtil TenantUtil { get; }
public SecurityContext SecurityContext { get; }
public TenantManager TenantManager { get; }
2019-09-19 15:55:44 +00:00
public BaseCommonLinkUtility BaseCommonLinkUtility { get; }
2019-09-09 12:56:33 +00:00
2019-06-06 14:41:16 +00:00
#endregion
}
}