Commit 8c554571 authored by Guilhem CARRON's avatar Guilhem CARRON
Browse files

Merge branch 'fix/prevent-index-init-to-crash-first-konnector' into 'dev'

fix(accounts): Prevent account doctype index to crash the first konnector init

See merge request web-et-numerique/llle_project/ecolyo!609
parents 6973962d a8fd3132
......@@ -110,10 +110,7 @@ const SplashRoot = ({ fadeTimer = 1000, children }: SplashRootProps) => {
setinitStep,
setinitStepErrors
)
const partnersInfoService = new PartnersInfoService(
client,
setinitStepErrors
)
const partnersInfoService = new PartnersInfoService(client)
const ms = new MigrationService(client, setinitStepErrors)
try {
const migrationsResult: ReleaseNotes = await ms.runMigrations(
......@@ -263,7 +260,9 @@ const SplashRoot = ({ fadeTimer = 1000, children }: SplashRootProps) => {
}
// Check partnersInfo from backoffice
const partnersInfo: PartnersInfo = await partnersInfoService.getPartnersInfo()
const partnersInfo:
| PartnersInfo
| undefined = await partnersInfoService.getPartnersInfo()
// Get last partnersIssueDate
const today = DateTime.local()
......
......@@ -53,58 +53,68 @@ export default class AccountService {
}
public async getAccountByType(type: string): Promise<Account | null> {
const query: QueryDefinition = Q(ACCOUNTS_DOCTYPE)
// eslint-disable-next-line @typescript-eslint/camelcase
.where({ account_type: type })
.indexFields(['account_type'])
const { data: accounts }: QueryResult<Account[]> = await this._client.query(
query
)
if (accounts.length > 1) {
// If several account are found we will used trigger date to select the older
const triggerService = new TriggerService(this._client)
const triggers: (Trigger | null)[] = await Promise.all(
accounts.map((_account: Account) =>
triggerService.getTriggerForAccount(_account)
try {
const query: QueryDefinition = Q(ACCOUNTS_DOCTYPE)
// eslint-disable-next-line @typescript-eslint/camelcase
.where({ account_type: type })
// .indexFields(['account_type'])
const {
data: accounts,
}: QueryResult<Account[]> = await this._client.query(query)
if (accounts.length > 1) {
// If several account are found we will used trigger date to select the older
const triggerService = new TriggerService(this._client)
const triggers: (Trigger | null)[] = await Promise.all(
accounts.map((_account: Account) =>
triggerService.getTriggerForAccount(_account)
)
)
)
// Keep the accountid which have the older trigger
let olderDate: DateTime = DateTime.now()
let olderAccountId: string | null = null
for (const _trigger of triggers) {
if (
_trigger &&
_trigger.cozyMetadata &&
_trigger.cozyMetadata.createdAt &&
DateTime.fromISO(_trigger.cozyMetadata.createdAt, {
zone: 'utc',
}) < olderDate
) {
olderDate = DateTime.fromISO(_trigger.cozyMetadata.createdAt, {
zone: 'utc',
})
olderAccountId = _trigger.message.account
// Keep the accountid which have the older trigger
let olderDate: DateTime = DateTime.now()
let olderAccountId: string | null = null
for (const _trigger of triggers) {
if (
_trigger &&
_trigger.cozyMetadata &&
_trigger.cozyMetadata.createdAt &&
DateTime.fromISO(_trigger.cozyMetadata.createdAt, {
zone: 'utc',
}) < olderDate
) {
olderDate = DateTime.fromISO(_trigger.cozyMetadata.createdAt, {
zone: 'utc',
})
olderAccountId = _trigger.message.account
}
}
// Retrieve the accountId and return it
const filteredAccounts: Account[] = accounts.filter(
_account => _account._id === olderAccountId
)
return filteredAccounts[0] ? filteredAccounts[0] : null
} else {
return accounts[0] ? accounts[0] : null
}
// Retrieve the accountId and return it
const filteredAccounts: Account[] = accounts.filter(
_account => _account._id === olderAccountId
)
return filteredAccounts[0] ? filteredAccounts[0] : null
} else {
return accounts[0] ? accounts[0] : null
} catch (err) {
console.error(`Error GetAccountByType: ${err}`)
return null
}
}
public async getAccountsByType(type: string): Promise<Account[]> {
const query: QueryDefinition = Q(ACCOUNTS_DOCTYPE)
// eslint-disable-next-line @typescript-eslint/camelcase
.where({ account_type: type })
.indexFields(['account_type'])
const { data: accounts }: QueryResult<Account[]> = await this._client.query(
query
)
return accounts
try {
const query: QueryDefinition = Q(ACCOUNTS_DOCTYPE)
// eslint-disable-next-line @typescript-eslint/camelcase
.where({ account_type: type })
// .indexFields(['account_type'])
const {
data: accounts,
}: QueryResult<Account[]> = await this._client.query(query)
return accounts
} catch (err) {
console.error(`Error: GetAccountsByType: ${err}`)
return []
}
}
public async updateAccount(account: Account): Promise<Account> {
......@@ -131,7 +141,7 @@ export default class AccountService {
const query: QueryDefinition = Q(ACCOUNTS_DOCTYPE)
// eslint-disable-next-line @typescript-eslint/camelcase
.where({ account_type: 'index' })
.indexFields(['account_type'])
// .indexFields(['account_type'])
.limitBy(1)
const { data: result }: QueryResult<[]> = await this._client.query(query)
return result
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment