Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/courses/services/course.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ChannelType, Client, Guild, GuildChannel } from 'discord.js';
import { logger } from '../../config/logger';
import { authService } from '../../services/auth.service';

interface GuildData {
uuid: string;
Expand Down Expand Up @@ -61,14 +62,14 @@ export class CourseService {

private async ensureGuild(): Promise<void> {
try {
const headers = await authService.getAuthHeaders();

const response = await fetch(`${this.apiUrl}/guilds/${this.SIMPLON_GUILD.uuid}`);

if (!response.ok) {
const createResponse = await fetch(`${this.apiUrl}/guilds`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
headers,
body: JSON.stringify(this.SIMPLON_GUILD),
});

Expand All @@ -86,6 +87,8 @@ export class CourseService {

private async ensureTemplateCategory(): Promise<void> {
try {
const headers = await authService.getAuthHeaders();

logger.debug({
categoryData: this.TEMPLATE_CATEGORY,
endpoint: `${this.apiUrl}/categories/${this.TEMPLATE_CATEGORY.uuid}`
Expand All @@ -97,9 +100,7 @@ export class CourseService {
if (!response.ok || !categoryData || !categoryData.uuid) {
const createResponse = await fetch(`${this.apiUrl}/categories`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
headers,
body: JSON.stringify(this.TEMPLATE_CATEGORY),
});

Expand Down Expand Up @@ -143,6 +144,7 @@ export class CourseService {
try {
await this.ensureGuild();
await this.ensureTemplateCategory();
const headers = await authService.getAuthHeaders();

const guildId = process.env.GUILD_ID;
if (!guildId) {
Expand Down Expand Up @@ -188,9 +190,7 @@ export class CourseService {

const roleResponse = await fetch(`${this.apiUrl}/roles`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
headers,
body: JSON.stringify({
name: role.name,
uuidGuild: guildId,
Expand Down Expand Up @@ -222,9 +222,7 @@ export class CourseService {

const response = await fetch(`${this.apiUrl}/courses`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
headers,
body: JSON.stringify(courseData),
});

Expand Down