Skip to content

Commit bd752b3

Browse files
committed
sections repo added
1 parent f61c738 commit bd752b3

1 file changed

Lines changed: 90 additions & 1 deletion

File tree

src/utils/arc/section.ts

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import type { Section, SectionReference } from '../../types';
1+
import assert from 'node:assert';
2+
import type { ArcAPIType } from '../../api';
3+
import type { Section, SectionReference, SetSectionPayload } from '../../types';
24
import { reference } from './ans';
35

46
export type NavigationTreeNode<T = unknown> = {
@@ -130,3 +132,90 @@ export const removeDuplicates = <T extends Section | SectionReference>(sections:
130132

131133
return [...map.values()];
132134
};
135+
136+
export class SectionsRepository {
137+
public sectionsByWebsite: Record<string, Section[]> = {};
138+
public websitesAreLoaded = false;
139+
140+
constructor(protected arc: ArcAPIType) {}
141+
142+
async put(ans: SetSectionPayload) {
143+
await this.arc.Site.putSection(ans);
144+
const created = await this.arc.Site.getSection(ans._id, ans.website);
145+
this.save(created);
146+
}
147+
148+
async loadWebsite(website: string) {
149+
const sections: Section[] = [];
150+
let next = true;
151+
let offset = 0;
152+
153+
while (next) {
154+
const migrated = await this.arc.Site.getSections({ website, offset }).catch((_) => {
155+
return { q_results: [] };
156+
});
157+
if (migrated.q_results.length) {
158+
sections.push(...migrated.q_results);
159+
offset += migrated.q_results.length;
160+
} else {
161+
next = false;
162+
}
163+
}
164+
165+
return sections;
166+
}
167+
168+
async loadWebsites(websites: string[]) {
169+
for (const website of websites) {
170+
this.sectionsByWebsite[website] = await this.loadWebsite(website);
171+
}
172+
173+
this.websitesAreLoaded = true;
174+
}
175+
176+
save(section: Section) {
177+
const website = section._website;
178+
179+
assert.ok(website, 'Section must have a website');
180+
181+
this.sectionsByWebsite[website] = this.sectionsByWebsite[website] || [];
182+
183+
if (!this.sectionsByWebsite[website].find((s) => s._id === section._id)) {
184+
this.sectionsByWebsite[website].push(section);
185+
}
186+
}
187+
188+
getById(id: string, website: string) {
189+
this.ensureWebsitesLoaded();
190+
191+
const section = this.sectionsByWebsite[website]?.find((s) => s._id === id);
192+
return section;
193+
}
194+
195+
getByWebsite(website: string) {
196+
this.ensureWebsitesLoaded();
197+
198+
return this.sectionsByWebsite[website];
199+
}
200+
201+
getParentSections(section: Section) {
202+
this.ensureWebsitesLoaded();
203+
204+
const parents: Section[] = [];
205+
let current = section;
206+
207+
while (current.parent?.default && current.parent.default !== '/') {
208+
const parent = this.getById(current.parent.default, section._website!);
209+
if (!parent) break;
210+
211+
parents.push(parent);
212+
current = parent;
213+
}
214+
215+
return parents;
216+
}
217+
218+
protected ensureWebsitesLoaded() {
219+
assert.ok(this.websitesAreLoaded, 'call .loadWebsites() first');
220+
}
221+
}

0 commit comments

Comments
 (0)