diff --git a/src/config/settings.go b/src/config/settings.go index 58461e0..7581678 100644 --- a/src/config/settings.go +++ b/src/config/settings.go @@ -25,4 +25,6 @@ var ( WhatsappWebhook string WhatsappSettingMaxFileSize int64 = 50000000 // 50MB WhatsappSettingMaxVideoSize int64 = 100000000 // 100MB + WhatsappTypeUser = "@s.whatsapp.net" + WhatsappTypeGroup = "@g.us" ) diff --git a/src/internal/rest/group.go b/src/internal/rest/group.go index 90a0788..9edb2d7 100644 --- a/src/internal/rest/group.go +++ b/src/internal/rest/group.go @@ -1,6 +1,7 @@ package rest import ( + "fmt" domainGroup "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/group" "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/utils" "github.com/gofiber/fiber/v2" @@ -62,7 +63,7 @@ func (controller *Group) CreateGroup(c *fiber.Ctx) error { return c.JSON(utils.ResponseData{ Status: 200, Code: "SUCCESS", - Message: "Success created group", + Message: fmt.Sprintf("Success created group with id %s", groupID), Results: map[string]string{ "group_id": groupID, }, diff --git a/src/pkg/whatsapp/whatsapp.go b/src/pkg/whatsapp/whatsapp.go index 6d1f19b..333dcd7 100644 --- a/src/pkg/whatsapp/whatsapp.go +++ b/src/pkg/whatsapp/whatsapp.go @@ -55,9 +55,9 @@ type evtMessage struct { func SanitizePhone(phone *string) { if phone != nil && len(*phone) > 0 && !strings.Contains(*phone, "@") { if len(*phone) <= 15 { - *phone = fmt.Sprintf("%s@s.whatsapp.net", *phone) + *phone = fmt.Sprintf("%s%s", *phone, config.WhatsappTypeUser) } else { - *phone = fmt.Sprintf("%s@g.us", *phone) + *phone = fmt.Sprintf("%s%s", *phone, config.WhatsappTypeGroup) } } } diff --git a/src/services/group.go b/src/services/group.go index d9348e7..c386639 100644 --- a/src/services/group.go +++ b/src/services/group.go @@ -2,6 +2,7 @@ package services import ( "context" + "github.com/aldinokemal/go-whatsapp-web-multidevice/config" domainGroup "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/group" pkgError "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/error" "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/whatsapp" @@ -54,9 +55,15 @@ func (service groupService) CreateGroup(ctx context.Context, request domainGroup var participantsJID []types.JID for _, participant := range request.Participants { - if !whatsapp.IsOnWhatsapp(service.WaCli, participant) { + formattedParticipant := participant + config.WhatsappTypeUser + + if !whatsapp.IsOnWhatsapp(service.WaCli, formattedParticipant) { return "", pkgError.ErrUserNotRegistered } + + if participantJID, err := types.ParseJID(formattedParticipant); err == nil { + participantsJID = append(participantsJID, participantJID) + } } groupConfig := whatsmeow.ReqCreateGroup{ diff --git a/src/validations/group_validation.go b/src/validations/group_validation.go index fe3a0b6..69742e5 100644 --- a/src/validations/group_validation.go +++ b/src/validations/group_validation.go @@ -34,6 +34,8 @@ func ValidateLeaveGroup(ctx context.Context, request domainGroup.LeaveGroupReque func ValidateCreateGroup(ctx context.Context, request domainGroup.CreateGroupRequest) error { err := validation.ValidateStructWithContext(ctx, &request, validation.Field(&request.Title, validation.Required), + validation.Field(&request.Participants, validation.Required), + validation.Field(&request.Participants, validation.Each(validation.Required)), ) if err != nil { diff --git a/src/views/components/GroupCreate.js b/src/views/components/GroupCreate.js new file mode 100644 index 0000000..41c920d --- /dev/null +++ b/src/views/components/GroupCreate.js @@ -0,0 +1,114 @@ +export default { + name: 'CreateGroup', + data() { + return { + loading: false, + title: '', + participants: ['', ''], + } + }, + methods: { + openModal() { + $('#modalGroupCreate').modal({ + onApprove: function () { + return false; + } + }).modal('show'); + }, + handleAddParticipant() { + this.participants.push('') + }, + handleDeleteParticipant(index) { + this.participants.splice(index, 1) + }, + async handleSubmit() { + try { + let response = await this.submitApi() + showSuccessInfo(response) + $('#modalGroupCreate').modal('hide'); + } catch (err) { + showErrorInfo(err) + } + }, + async submitApi() { + this.loading = true; + try { + let response = await window.http.post(`/group`, { + title: this.title, + // convert participant become list of string + participants: this.participants.filter(participant => participant !== '').map(participant => `${participant}`) + }) + this.handleReset(); + this.groups = response.data.results.data; + return response.data.message; + } catch (error) { + if (error.response) { + throw new Error(error.response.data.message); + } + throw new Error(error.message); + } finally { + this.loading = false; + } + }, + handleReset() { + this.title = ''; + this.participants = ['', '']; + }, + }, + template: ` +