Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRUD user info (LinkFree) #663

Closed
wants to merge 4 commits into from
Closed

Conversation

Cahllagerfeld
Copy link
Member

@Cahllagerfeld Cahllagerfeld commented Oct 8, 2021

closes #662

  • create user

  • update socials

  • update bio

  • Trim issue when updating bio in the argument


@OnCommand({ name: 'profile' })
async handleProfile(message: Message) {
const args = message.content.trim().split(/ +/g);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't look at this sooner, here is what I was thinking

import { ArgNum, ArgRange } from 'discord-nestjs';
import { Expose } from 'class-transformer';
import { IsNumber, Min, IsArray } from 'class-validator';

export class RegistrationDto {
  @ArgRange(() => ({ formPosition: 1, toPosition: 4 }))
  @Expose()
  @IsArray()
  name: string[];

  @ArgNum((last: number) => ({ position: last }))
  @Expose()
  @Type(() => Number)
  @IsNumber()
  @Min(18)
  age: number;
}

https://github.com/fjodor-rybakov/discord-nestjs#ℹ%EF%B8%8F-pipes-transformation-and-validation-

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But also I thought we could split the command into 2 commands - let me try something

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I wasnt aware of argRange and argNum :o

@eddiejaoude
Copy link
Member

eddiejaoude commented Oct 18, 2021

I created a patch file with some changes, let me know what you think?

  • separated out bio and social commands
diff --git a/src/profile/bio.handler.ts b/src/profile/bio.handler.ts
new file mode 100644
index 0000000..ae3defe
--- /dev/null
+++ b/src/profile/bio.handler.ts
@@ -0,0 +1,18 @@
+import { Injectable } from '@nestjs/common';
+import { OnCommand } from 'discord-nestjs';
+import { Message } from 'discord.js';
+import { ProfileService } from './profile.service';
+
+@Injectable()
+export class BioHandler {
+  constructor(private readonly profileService: ProfileService) {}
+
+  @OnCommand({ name: 'bio' })
+  async handleProfile(message: Message) {
+    const args = message.content.trim().split(/ +/g);
+
+    const response = await this.profileService.updateBio(message, args[1]);
+
+    await message.reply(response);
+  }
+}
diff --git a/src/profile/profile.handler.ts b/src/profile/profile.handler.ts
deleted file mode 100644
index 299f031..0000000
--- a/src/profile/profile.handler.ts
+++ /dev/null
@@ -1,42 +0,0 @@
-import { Injectable } from '@nestjs/common';
-import { OnCommand } from 'discord-nestjs';
-import { Message } from 'discord.js';
-import { ProfileService } from './profile.service';
-
-@Injectable()
-export class ProfileHandler {
-  constructor(private readonly profileService: ProfileService) {}
-
-  @OnCommand({ name: 'profile' })
-  async handleProfile(message: Message) {
-    const args = message.content.trim().split(/ +/g);
-
-    let response: string;
-
-    switch (args[1]) {
-      // !profile create
-      case 'create':
-        response = await this.profileService.createUser(message);
-        break;
-
-      // !profile socials twitter https://twitter.com/cahllagerfeld
-      case 'socials':
-        //TODO args [3] with spaces causes issues with trim function
-        response = await this.profileService.updateSocials(
-          message,
-          args[2],
-          args[3],
-        );
-        break;
-
-      case 'bio':
-        response = await this.profileService.updateBio(message, args[2]);
-        break;
-
-      default:
-        return await message.reply('Please specify valid arguments');
-    }
-
-    await message.reply(response);
-  }
-}
diff --git a/src/profile/profile.module.ts b/src/profile/profile.module.ts
index 65f28e7..2ce6b69 100644
--- a/src/profile/profile.module.ts
+++ b/src/profile/profile.module.ts
@@ -1,10 +1,11 @@
 import { HttpModule, Module } from '@nestjs/common';
 import { TokenModule } from '../token/token.module';
-import { ProfileHandler } from './profile.handler';
+import { BioHandler } from './bio.handler';
+import { SocialsHandler } from './socials.handler';
 import { ProfileService } from './profile.service';
 
 @Module({
   imports: [HttpModule, TokenModule],
-  providers: [ProfileService, ProfileHandler],
+  providers: [ProfileService, BioHandler, SocialsHandler],
 })
 export class ProfileModule {}
diff --git a/src/profile/profile.service.ts b/src/profile/profile.service.ts
index 7c51842..e2841cd 100644
--- a/src/profile/profile.service.ts
+++ b/src/profile/profile.service.ts
@@ -12,31 +12,6 @@ export class ProfileService {
     private readonly config: ConfigService,
   ) {}
 
-  public async createUser(message: Message): Promise<string> {
-    const createUserDTO = {
-      author: {
-        platform: 'discord',
-        uid: message.author.id,
-      },
-      bio: '',
-      socials: {},
-    };
-    const token = await this.tokenCache.returnToken(message.guild.id);
-
-    try {
-      const response = await this.http
-        .post(`${this.config.get('API_URL')}/discord`, createUserDTO, {
-          headers: { authorization: `Bearer ${token}` },
-        })
-        .toPromise();
-      console.log(response.data);
-      return 'Profile created successfully';
-    } catch (error) {
-      console.log(error);
-      return 'Profile created not successfully';
-    }
-  }
-
   public async updateSocials(message: Message, platform: string, link: string) {
     let returnMessage: string = null;
     let socialsObject: Socials = {};
@@ -58,8 +33,7 @@ export class ProfileService {
         socialsObject = { discord: message.author.username };
         break;
       default:
-        returnMessage = 'Please provide valid social platform';
-        break;
+        return 'Please provide valid social platform';
     }
 
     const updateUser = {
@@ -67,7 +41,6 @@ export class ProfileService {
         platform: 'discord',
         uid: message.author.id,
       },
-      bio: '',
       socials: socialsObject,
     };
 
@@ -83,7 +56,6 @@ export class ProfileService {
         uid: message.author.id,
       },
       bio: bio,
-      socials: {},
     };
     return await this.performUpdate(updateUser, message);
   }
@@ -105,6 +77,7 @@ export class ProfileService {
         .toPromise();
       return 'Updated successfully';
     } catch (error) {
+      console.log(error);
       return 'Update failed';
     }
   }
diff --git a/src/profile/socials.handler.ts b/src/profile/socials.handler.ts
new file mode 100644
index 0000000..848648a
--- /dev/null
+++ b/src/profile/socials.handler.ts
@@ -0,0 +1,22 @@
+import { Injectable } from '@nestjs/common';
+import { OnCommand } from 'discord-nestjs';
+import { Message } from 'discord.js';
+import { ProfileService } from './profile.service';
+
+@Injectable()
+export class SocialsHandler {
+  constructor(private readonly profileService: ProfileService) {}
+
+  @OnCommand({ name: 'socials' })
+  async handleProfile(message: Message) {
+    const args = message.content.trim().split(/ +/g);
+
+    const response = await this.profileService.updateSocials(
+      message,
+      args[1],
+      args[2],
+    );
+
+    await message.reply(response);
+  }
+}

@Cahllagerfeld
Copy link
Member Author

I'd postpone this PR until we're sure we want to go with the new slash commands. Otherwise we may have to do/fix the open issues from this twice

@eddiejaoude eddiejaoude changed the title Cahllagerfeld/issue 662 CRUD user info (LinkFree) Nov 3, 2021
@eddiejaoude
Copy link
Member

Closing as going to rewrite to slash commands

@eddiejaoude eddiejaoude closed this Nov 3, 2021
@Cahllagerfeld Cahllagerfeld deleted the cahllagerfeld/issue-662 branch July 1, 2022 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] create profiles for linkfree via discord
2 participants