github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/db/util_test.go (about)

     1  package db_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/mundipagg/boleto-api/config"
     9  	"github.com/mundipagg/boleto-api/db"
    10  	"github.com/mundipagg/boleto-api/models"
    11  	"github.com/mundipagg/boleto-api/test"
    12  	"go.mongodb.org/mongo-driver/bson"
    13  	"go.mongodb.org/mongo-driver/bson/primitive"
    14  )
    15  
    16  type stubBoletoRequestDb struct {
    17  	test.StubBoletoRequest
    18  }
    19  
    20  func newStubBoletoRequestDb(bank models.BankNumber) *stubBoletoRequestDb {
    21  	expirationDate := time.Now().Add(5 * 24 * time.Hour)
    22  
    23  	base := test.NewStubBoletoRequest(bank)
    24  	s := &stubBoletoRequestDb{
    25  		StubBoletoRequest: *base,
    26  	}
    27  
    28  	s.Agreement = models.Agreement{
    29  		AgreementNumber: 123456,
    30  		Agency:          "1234",
    31  	}
    32  
    33  	s.Title = models.Title{
    34  		ExpireDateTime: expirationDate,
    35  		ExpireDate:     expirationDate.Format("2006-01-02"),
    36  		OurNumber:      12345678901234,
    37  		AmountInCents:  200,
    38  		DocumentNumber: "1234567890A",
    39  		Instructions:   "Campo de instruções -  max 40 caracteres",
    40  		BoletoType:     "OUT",
    41  		BoletoTypeCode: "99",
    42  	}
    43  
    44  	s.Recipient = models.Recipient{
    45  		Document: models.Document{
    46  			Type:   "CNPJ",
    47  			Number: "12123123000112",
    48  		},
    49  	}
    50  
    51  	s.Buyer = models.Buyer{
    52  		Name: "Willian Jadson Bezerra Menezes Tupinambá",
    53  		Document: models.Document{
    54  			Type:   "CPF",
    55  			Number: "12312312312",
    56  		},
    57  		Address: models.Address{
    58  			Street:     "Rua da Assunção de Sá",
    59  			Number:     "123",
    60  			Complement: "Seção A, s 02",
    61  			ZipCode:    "20520051",
    62  			City:       "Belém do Pará",
    63  			District:   "Açaí",
    64  			StateCode:  "PA",
    65  		},
    66  	}
    67  	return s
    68  }
    69  
    70  // deleteBoletoById deleta um boleto no mongoDB
    71  // Usado apenas para fins de teste
    72  func deleteBoletoById(id string) error {
    73  	if id == "" {
    74  		return fmt.Errorf("ID cannot be empty")
    75  	}
    76  
    77  	var filter primitive.M
    78  	if len(id) == 24 {
    79  		d, err := primitive.ObjectIDFromHex(id)
    80  		if err != nil {
    81  			return fmt.Errorf("Error converting string to objectID: %s\n", err)
    82  		}
    83  		filter = bson.M{"_id": d}
    84  	} else {
    85  		filter = bson.M{"id": id}
    86  	}
    87  
    88  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    89  	defer cancel()
    90  
    91  	dbSession, err := db.CreateMongo()
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	d := config.Get().MongoDatabase
    97  	cl := config.Get().MongoBoletoCollection
    98  	collection := dbSession.Database(d).Collection(cl)
    99  
   100  	_, err = collection.DeleteOne(ctx, filter)
   101  
   102  	if err != nil {
   103  		return err
   104  	}
   105  
   106  	return nil
   107  }
   108  
   109  // saveCredential salva uma credencial no mongoDB
   110  // Usado apenas para fins de teste
   111  func saveCredential(credential models.Credentials) error {
   112  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
   113  	defer cancel()
   114  
   115  	dbSession, err := db.CreateMongo()
   116  	if err != nil {
   117  		return err
   118  	}
   119  
   120  	collection := dbSession.Database(config.Get().MongoDatabase).Collection(config.Get().MongoCredentialsCollection)
   121  	_, err = collection.InsertOne(ctx, credential)
   122  
   123  	return err
   124  
   125  }
   126  
   127  // deleteCredentialById deleta uma credencial no mongoDB
   128  // Usado apenas para fins de teste
   129  func deleteCredentialById(id string) error {
   130  	if id == "" {
   131  		return fmt.Errorf("ID cannot be empty")
   132  	}
   133  
   134  	var filter primitive.M
   135  	if len(id) == 24 {
   136  		d, err := primitive.ObjectIDFromHex(id)
   137  		if err != nil {
   138  			return fmt.Errorf("Error converting string to objectID: %s\n", err)
   139  		}
   140  		filter = bson.M{"_id": d}
   141  	} else {
   142  		filter = bson.M{"id": id}
   143  	}
   144  
   145  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
   146  	defer cancel()
   147  
   148  	dbSession, err := db.CreateMongo()
   149  	if err != nil {
   150  		return err
   151  	}
   152  
   153  	collection := dbSession.Database(config.Get().MongoDatabase).Collection(config.Get().MongoCredentialsCollection)
   154  	_, err = collection.DeleteOne(ctx, filter)
   155  
   156  	if err != nil {
   157  		return err
   158  	}
   159  
   160  	return nil
   161  }
   162  
   163  // getUserCredentialByID busca uma credencial pelo ID
   164  // método apenas para fim de teste
   165  func getUserCredentialByID(id string) (models.Credentials, error) {
   166  	result := models.Credentials{}
   167  	if id == "" {
   168  		return result, fmt.Errorf("ID cannot be empty")
   169  	}
   170  
   171  	var filter primitive.M
   172  	if len(id) == 24 {
   173  		d, err := primitive.ObjectIDFromHex(id)
   174  		if err != nil {
   175  			return result, fmt.Errorf("Error converting string to objectID: %s\n", err)
   176  		}
   177  		filter = bson.M{"_id": d}
   178  	} else {
   179  		filter = bson.M{"id": id}
   180  	}
   181  
   182  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
   183  	defer cancel()
   184  
   185  	dbSession, err := db.CreateMongo()
   186  	if err != nil {
   187  		return result, err
   188  	}
   189  
   190  	collection := dbSession.Database(config.Get().MongoDatabase).Collection(config.Get().MongoCredentialsCollection)
   191  	err = collection.FindOne(ctx, filter).Decode(&result)
   192  	if err != nil {
   193  		return result, err
   194  	}
   195  
   196  	return result, nil
   197  }
   198  
   199  func deleteTokenByIssuerBank(issuerBank string) error {
   200  	if issuerBank == "" {
   201  		return fmt.Errorf("issuerBank cannot be empty")
   202  	}
   203  
   204  	filter := bson.M{"issuerbank": issuerBank}
   205  
   206  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
   207  	defer cancel()
   208  
   209  	dbSession, err := db.CreateMongo()
   210  	if err != nil {
   211  		return err
   212  	}
   213  
   214  	collection := dbSession.Database(config.Get().MongoDatabase).Collection(config.Get().MongoTokenCollection)
   215  	_, err = collection.DeleteOne(ctx, filter)
   216  
   217  	if err != nil {
   218  		return err
   219  	}
   220  
   221  	return nil
   222  }